1、C+语言程序设计课后答案第 二 章 C+简单程序设计2-10 执行完下列语句后,a、b、c三个变量的值为多少?a = 30;b = a+;c = +a;解: a:32 ; b:30 ; c:32;2-13 写一条for语句,计数条件为n从100到200,步长为2;然后用while和dowhile语句完成同样的循环。解: for循环:for (int n = 100; n = 200; n += 2); while循环:int x = 100;while (n = 200)n += 2; dowhile循环:int n = 100;don += 2; while(n = 200);2-17 修改
2、下面这个程序中的错误,改正后它的运行结果是什么?#include void main()int iint j;i = 10; /* 给i赋值j = 20; /* 给j赋值 */cout i + j = i + j; /* 输出结果 */return 0;解: 改正:#include int main()int i;int j;i = 10; 09:;?ABCDEFGHIJKLMNOP_QRSTUVWXYZabcdefghijklmnopqrstuvwxyzs2-21 运行下面的程序,观察其输出,与你的设想是否相同?#include int main()unsigned int x;unsign
3、ed int y = 100;unsigned int z = 50;x= y - z;cout Difference is: x;x = z - y;cout nNow difference is: x endl;return 0;解: 程序运行输出:Difference is: 50Now difference is: 46注意,第二行的输出并非 -50,注意x、y、z的数据类型。2-22 运行下面的程序,观察其输出,体会i+与+i的差别。#include int main()int myAge = 39; n;cout You are: yourAge years oldn;myAge+
4、; .n;cout I am: myAge years old.n;cout You are: yourAge years oldn;cout Another year passesn;cout I am: myAge+ years old.n;cout You are: +yourAge years oldn;cout Lets print it again.n;cout I am: myAge years old.n;cout You are: yourAge years oldn;return 0;解: 程序运行输出:I am 39 years oldYou are 39 years o
5、ldOne year passesI am 40 years oldYou are 40 years oldAnother year passesI am 40 years oldYou are 41 years oldLets print it againI am 41 years oldYou are 41 years old2-28 编写一个完整的程序,实现功能:向用户提问现在正在下雨吗?,提示用户输入Y或N。若输入为Y,显示现在正在下雨。; 若输入为N,显示现在没有下雨。;否则继续提问现在正在下雨吗?解: 源程序:#include #include void main()char fl
6、ag;while(1)cout flag;if ( toupper(flag) = Y)cout 现在正在下雨。;break;if ( toupper(flag) = N)cout 现在没有下雨。;break;程序运行输出:现在正在下雨吗?(Yes or No):x现在正在下雨吗?(Yes or No):l现在正在下雨吗?(Yes or No):q现在正在下雨吗?(Yes or No):n现在没有下雨。或:现在正在下雨吗?(Yes or No):y现在正在下雨。2-29 编写一个完整的程序,运行时向用户提问你考试考了多少分?(0100),接收输入后判断其等级,显示出来。规则如下:解: #inc
7、lude void main()int i,score;cout score;if (score100 | score0)cout 分数值必须在0到100之间!;elsei = score/10;switch (i)case 10:case 9:cout 你的成绩为优!;break;case 8:cout 你的成绩为良!;break;case 7:case 6:cout 你的成绩为中!;break;default:cout 你的成绩为差!; 程序运行输出:你考试考了多少分?(0100):85你的成绩为良!2-31 用穷举法找出1100间的质数,显示出来。分别使用while,do-while,f
8、or循环语句实现。解: 源程序: 使用while循环语句:#include #include void main()int i,j,k,flag;i = 2;while(i = 100)flag = 1;k = sqrt(i);j = 2;while (j = k)if(i%j = 0)flag = 0;break;j+;if (flag)cout i 是质数. endl;i+; 使用dowhile循环语句:#include #include void main()int i,j,k,flag;i = 2;doflag = 1;k = sqrt(i);j = 2;doif(i%j = 0)fl
9、ag = 0;break;j+;while (j = k);if (flag)cout i 是质数. endl;i+;while(i = 100); 使用for循环语句:#include #include void main()int i,j,k,flag;for(i = 2; i = 100; i+)flag = 1;k = sqrt(i);for (j = 2; j = k; j+)if(i%j = 0)flag = 0;break;if (flag)cout i 是质数. one;cout two;answer = Divider(one, two);if (answer -1)cout
10、 Answer: answer;elsecout Error, cant divide by zero!;return 0;程序运行输出:Enter two numbers.Number one:8Number two:2Answer: 43-8 编写函数把华氏温度转换为摄氏温度,公式为:C = (F - 32) * 5/9; 在主程序中提示用户输入一个华氏温度,转化后输出相应的摄氏温度。解: 源程序见实验指导部分实验三3-10 编写函数求两个整数的最大公约数和最小公倍数。解: 源程序:#include #include int fn1(int i,int j); void PrintName
11、Of(); ;Document:Document( char *name )Name = new char strlen( name ) + 1 ;strcpy( Name, name );void Document:PrintNameOf()cout Name endl;class Book : public Documentpublic:Book( char *name, long pagecount );void PrintNameOf();private:long PageCount;Book:Book( char *name, long pagecount ):Document(name)PageCount = pagecount;void Book:PrintNameOf()cout Name of book: ;Document:PrintNameOf();void main()Document a(Document1);Book b(Book1,100);();程序运行输出:Name of book: Book17-9 定义基类Base,
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1