ImageVerifierCode 换一换
格式:DOCX , 页数:38 ,大小:23.98KB ,
资源ID:5061715      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/5061715.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C++语言程序设计课后答案.docx)为本站会员(b****3)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

C++语言程序设计课后答案.docx

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; / 给i赋值j = 20; /* 给j赋值 */cout i + j = i + j; /* 输出结果 */return 0;程序运行输出:i + j = 302-18 编写一个程序,运行时提示输入一个数字,再把这个数字显示出来。解: 源程序:#inclu

3、de int main()int i;cout i;cout 您输入一个数字是 i endl;return 0;程序运行输出:请输入一个数字:5您输入一个数字是52-20 打印ASCII码为32127的字符。解: #include int main()for (int i = 32; i128; i+)cout (char) i;return 0;程序运行输出:!#$%G()*+,./0123456789:;?ABCDEFGHIJKLMNOP_QRSTUVWXYZabcdefghijklmnopqrstuvwxyzs2-21 运行下面的程序,观察其输出,与你的设想是否相同?#include i

4、nt main()unsigned int x;unsigned 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: 4294967246注意,第二行的输出并非 -50,注意x、y、z的数据类型。2-22 运行下面的程序,观察其输出,体会i+与+i的差别。#include int main()int myAge = 39; / i

5、nitialize two integersint yourAge = 39;cout I am: myAge years old.n;cout You are: yourAge years oldn;myAge+; / postfix increment+yourAge; / prefix incrementcout One year passes.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;cou

6、t 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 oldOne year passesI am 40 years oldYou are 40 years oldAnother year passesI am 40 years oldYou are 41 years oldLets print it ag

7、ainI am 41 years oldYou are 41 years old2-28 编写一个完整的程序,实现功能:向用户提问现在正在下雨吗?,提示用户输入Y或N。若输入为Y,显示现在正在下雨。; 若输入为N,显示现在没有下雨。;否则继续提问现在正在下雨吗?解: 源程序:#include #include void main()char flag;while(1)cout flag;if ( toupper(flag) = Y)cout 现在正在下雨。;break;if ( toupper(flag) = N)cout 现在没有下雨。;break;程序运行输出:现在正在下雨吗?(Yes o

8、r No):x现在正在下雨吗?(Yes or No):l现在正在下雨吗?(Yes or No):q现在正在下雨吗?(Yes or No):n现在没有下雨。或:现在正在下雨吗?(Yes or No):y现在正在下雨。2-29 编写一个完整的程序,运行时向用户提问你考试考了多少分?(0100),接收输入后判断其等级,显示出来。规则如下:解: #include void main()int i,score;cout score;if (score100 | score0)cout 分数值必须在0到100之间!;elsei = score/10;switch (i)case 10:case 9:cou

9、t 你的成绩为优!;break;case 8:cout 你的成绩为良!;break;case 7:case 6:cout 你的成绩为中!;break;default:cout 你的成绩为差!; 程序运行输出:你考试考了多少分?(0100):85你的成绩为良!2-31 用穷举法找出1100间的质数,显示出来。分别使用while,do-while,for循环语句实现。解: 源程序: 使用while循环语句:#include #include void main()int i,j,k,flag;i = 2;while(i = 100)flag = 1;k = sqrt(i);j = 2;while

10、(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)flag = 0;break;j+;while (j = k);if (flag)cout i 是质数. endl;i+;while(i = 100); 使用for循环语句:#include #include void main()int i,j,k,fl

11、ag;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 是质数. endl;程序运行输出:2是质数.3是质数.5是质数.7是质数.11是质数.13是质数.17是质数.19是质数.23是质数.29是质数.31是质数.37是质数.41是质数.43是质数.47是质数.53是质数.59是质数.61是质数.67是质数.71是质数.73是质数.79是质数.83是质数.89是质数.97是质数. 2-33 定义一个表示时间的结构体,可以精确表示年

12、、月、日、小时、分、秒;提示用户输入年、月、日、小时、分、秒的值,然后完整地显示出来。 解: 源程序见实验指导部分实验二2-34 在程序中定义一个整型变量,赋以1100的值,要求用户猜这个数,比较两个数的大小,把结果提示给用户,直到猜对为止。分别使用while、dowhile语句实现循环。解: /使用while语句#include void main() int n = 18;int m = 0;while(m != n) cout m;if (n m)cout 你猜的值太小了! endl;else if (n m)cout 你猜的值太大了! endl;elsecout 你猜对了! endl;

13、/使用dowhile语句#include void main() int n = 18;int m = 0;docout m;if (n m)cout 你猜的值太小了! endl;else if (n m)cout 你猜的值太大了! endl;elsecout 你猜对了! endl;while(n != m); 程序运行输出:请猜这个数的值为多少?(0100):50你猜的值太大了!请猜这个数的值为多少?(0100):25你猜的值太大了! 请猜这个数的值为多少?(0100):10你猜的值太小了!请猜这个数的值为多少?(0100):15你猜的值太小了!请猜这个数的值为多少?(0100):18你猜对

14、了! 第三章 函数3-2 观察下面程序的运行输出,与你设想的有何不同?仔细体会引用的用法。源程序:#include int main()int intOne;int &rSomeRef = intOne;intOne = 5;cout intOne:tt intOne endl;cout rSomeRef:t rSomeRef endl;int intTwo = 8;rSomeRef = intTwo; / not what you think!cout nintOne:tt intOne endl;cout intTwo:tt intTwo endl;cout rSomeRef:t rSom

15、eRef endl;return 0;程序运行输出:intOne: 5rSomeRef: 5intOne: 8intTwo: 8rSomeRef: 83-7 编写函数,参数为两个unsigned short int型数,返回值为第一个参数除以第二个参数的结果,数据类型为short int;如果第二个参数为0,则返回值为-1。在主程序中实现输入输出。解: 源程序:#include short int Divider(unsigned short int a, unsigned short int b)if (b = 0)return -1;elsereturn a/b;typedef unsig

16、ned short int USHORT;typedef unsigned long int ULONG;int main()USHORT one, two;short int answer;cout one;cout two;answer = Divider(one, two);if (answer -1)cout Answer: answer;elsecout Error, cant divide by zero!;return 0;程序运行输出:Enter two numbers.Number one:8Number two:2Answer: 43-8 编写函数把华氏温度转换为摄氏温度,

17、公式为:C = (F - 32) * 5/9; 在主程序中提示用户输入一个华氏温度,转化后输出相应的摄氏温度。解: 源程序见实验指导部分实验三3-10 编写函数求两个整数的最大公约数和最小公倍数。解: 源程序:#include #include int fn1(int i,int j); /求最大公约数的函数void main()int i,j,x,y;cout i ;cout j ;x = fn1(i,j);y = i * j / x;cout i 和 j 的最大公约数是: x endl;cout i 和 j 的最小公倍数是: y endl;int fn1(int i, int j)int

18、temp;if (i j)temp = i;i = j;j = i;while(j != 0)temp = i % j;i = j;j = temp;return i;程序运行输出:请输入一个正整数:120请输入另一个正整数:72120和72的最大公约数是:24120和72的最小公倍数是:3603-12 在主程序中提示输入整数n,编写函数用递归的方法求1 + 2 + + n的值。解: #include #include int fn1(int i);void main()int i;cout i ;cout 从1累加到 i 的和为: fn1(i) 2; fib(1) = fib(2) = 1;

19、观察递归调用的过程。解: 源程序见实验指导部分实验三3-15 用递归的方法编写函数求n阶勒让德多项式的值,在主程序中实现输入、输出;解: #include float p(int n, int x);void main()int n,x;cout n;cout x;cout n = n endl;cout x = x endl;cout P n ( x ) = p(n,x) endl;float p(int n, int x)if (n = 0)return 1;else if (n = 1)return x;elsereturn (2*n-1)*x*p(n-1,x) - (n-1)*p(n-

20、2,x) /n ;程序运行输出:请输入正整数n:1请输入正整数x:2n = 1x = 2P1(2) = 2请输入正整数n:3请输入正整数x:4n = 3x = 4P3(4) = 154 第 四 章 类4-9 设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角与右上角两个点的坐标,能计算矩形的面积。解: 源程序:#include class Rectanglepublic:Rectangle (int top, int left, int bottom, int right);Rectangle () int GetTop() const return itsTop; int G

21、etLeft() const return itsLeft; int GetBottom() const return itsBottom; int GetRight() const return itsRight; void SetTop(int top) itsTop = top; void SetLeft (int left) itsLeft = left; void SetBottom (int bottom) itsBottom = bottom; void SetRight (int right) itsRight = right; int GetArea() const;priv

22、ate:int itsTop;int itsLeft;int itsBottom;int itsRight;Rectangle:Rectangle(int top, int left, int bottom, int right)itsTop = top;itsLeft = left;itsBottom = bottom;itsRight = right;int Rectangle:GetArea() constint Width = itsRight-itsLeft;int Height = itsTop - itsBottom;return (Width * Height);int mai

23、n()Rectangle MyRectangle (100, 20, 50, 80 );int Area = MyRectangle.GetArea();cout Area: Area n;return 0;程序运行输出:Area: 3000Upper Left X Coordinate: 204-11 定义一个矩形类,有长、宽两个属性,有成员函数计算矩形的面积 解: #include class Rectanglepublic:Rectangle(float len, float width)Length = len;Width = width;Rectangle();float GetArea() return Length * Width; float GetLength() return Length; float GetWidth() return Width; private:float Length;float Width;void main()float length, width;cout length;cout width;Rectangle r(length, width);cout 长为 length 宽为 width 的矩形的面积为: r.GetArea () endl;程序运行输出:请输入矩形的长度:5请输入矩形的宽度:4长为5

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1