




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、第二章C+简单程序设计return 0;程序运行输出:2-10 执行完下列语句后, a、b、 c三个Difference is: 50变量的值为多少?程序运行输出:Now difference is: 4294967246a = 30;i + j = 30注意,第二行的输出并非 -50 ,注意 x、b = a+;2-18 编写一个程序, 运行时提示输入一y、z 的数据类型。c = +a;个数字,再把这个数字显示出来。2-22 运行下面的程序,观察其输出,体a: 32 ; b :30 ; c : 32;解:会i+与+i的差别。2-13 写一条 for 语句,计数条件为n从源程序:#include
2、 100 到 200,步长为 2;然后用 while 和#include int main()dowhile语句完成同样的循环。int main()解:int myAge = 39; / initializetwofor 循环:int i;integersfor (int n = 100; n = 200; n += 2);cout i;cout I am: myAge yearsint x = 100;cout 您输入一个数字是 i old.n;while (n = 200)endl;cout You are: yourAge n += 2;return 0;years oldn;dowhi
3、le循环:myAge+; / postfix incrementint n = 100;程序运行输出:+yourAge; / prefix incrementdo2-20 打印 ASCII 码为 32127 的字符。cout One year passes.n;#include cout I am: myAge yearsn += 2;int main()old.n; while(n = 200);cout You are: yourAge 2-17 修改下面这个程序中的错误,改正for (int i = 32; i128; i+)years oldn;后它的运行结果是什么?cout (cha
4、r) i;cout Another year passesn;#include return 0;cout I am: myAge+ yearsvoid main()old.n;int i程序运行输出:cout You are: +yourAge int j;!#$%G()*+ , ./0123456789:;?ABC years oldn;i = 10; /*给 i 赋值DEFGHIJKLMNOP_QRSTUVWXYZAabcdecout Lets print it again.n;j = 20; /*给 j 赋值 */fghijklmnopqrstuvwxyzscout I am: myA
5、ge yearscout i + j = i + j; /*输出2-21 运行下面的程序,观察其输出,与old.n;结果 */你的设想是否相同?cout You are: yourAge return 0;#include years oldn;int main()return 0;解:改正:unsigned int x;解:#include unsigned int y = 100;程序运行输出:int main()unsigned int z = 50;I am 39 years oldx= y - z;You are 39 years oldint i;cout Difference i
6、s: x;One year passesint j;x = z - y;I am 40 years oldi = 10; /给 i 赋值cout nNow difference is: xYou are 40 years oldj = 20; /*给 j 赋值 */endl;Another year passescout i + j = i + j; /*输return 0;I am 40 years old出结果 */You are 41 years oldLets print it again#include I am 41 years oldvoid main()flag = 1;You
7、 are 41 years oldk = sqrt(i);2-28 编写一个完整的程序,实现功能:int i,score;j = 2;向用户提问 现在正在下雨吗? ,提示cout 你 考 试 考 了 多 少while (j score;if(i%j = 0)在没有下雨。 ;否则继续提问 现在正if (score100 | score0)在下雨吗? cout 分数值必须在 0到 100之间!;flag = 0;源程序:elsebreak;#include #include i = score/10;j+;void main()switch (i)if (flag)char flag;case 1
8、0:cout i 是质数 . endl;while(1)case 9:i+;cout 你的成绩为优! ;cout flag;cout 你的成绩为良! ;#include if ( toupper(flag) = Y)break;#include case 7:void main()cout 现在正在下雨。 ;case 6:break;cout 你的成绩为中! ;int i,j,k,flag;break;i = 2;if ( toupper(flag) = N)default:docout 你的成绩为差! ;flag = 1;cout 现在没有下雨。 ;k = sqrt(i);break;j =
9、 2;do程序运行输出:if(i%j = 0)你考试考了多少分 ?(0100) :85程序运行输出:你的成绩为良!flag = 0;现在正在下雨吗? (Yes or No):x2-31 用穷举法找出 1100 间的质数,显break;现在正在下雨吗? (Yes or No):l示出来。 分别使用 while ,do-while ,for现在正在下雨吗? (Yes or No):q循环语句实现。j+;现在正在下雨吗? (Yes or No):n解:while (j = k);现在没有下雨。源程序:if (flag)或:使用 while 循环语句:cout i 是质数 . endl;现在正在下雨吗
10、? (Yes or No):y#include i+;现在正在下雨。#include while(i = 100);2-29 编写一个完整的程序, 运行时向用void main()户提问你考试考了多少分? (0100) ,使用 for 循环语句:接收输入后判断其等级,显示出来。规int i,j,k,flag;#include 则如下:i = 2;#include 解:while(i = 100)void main()提示用户输入年、月、日、小时、分、请猜这个数的值为多少?(0100):50int i,j,k,flag;秒的值,然后完整地显示出来。你猜的值太大了!for(i = 2; i = 1
11、00; i+)解:请猜这个数的值为多少?(0100):25源程序见 实验指导 部分实验二你猜的值太大了!flag = 1;2-34 在程序中定义一个整型变量, 赋以请猜这个数的值为多少?(0100):10k = sqrt(i);1100 的值,要求用户猜这个数, 比较两你猜的值太小了!for (j = 2; j = k; j+)个数的大小,把结果提示给用户,直到请猜这个数的值为多少?(0100):15猜对为止。分别使用while、dowhile你猜的值太小了!if(i%j = 0)语句实现循环。请猜这个数的值为多少?(0100):18解:你猜对了!flag = 0;/ 使用 while 语句第
12、三章 函数break;#include 3-2 观察下面程序的运行输出,与你设void main() 想的有何不同?仔细体会引用的用法。int n = 18;源程序:if (flag)int m = 0;#include cout i 是质数 . endl;while(m != n)int main() cout m;intOne = 5;3是质数.if (n m)cout intOne:tt intOne 5是质数.cout 你猜的值太小了! endl;endl;7 是质数 .else if (n m)cout rSomeRef:t rSomeRef 11是质数 .cout 你猜的值太大了!
13、 endl;endl;13 是质数 .elseint intTwo = 8;17 是质数 .cout 你猜对了! endl;rSomeRef = intTwo; / not what you19 是质数 .think!23 是质数 .cout nintOne:tt intOne 29 是质数 ./ 使用 do while 语句endl;31 是质数 .#include cout intTwo:tt intTwo 37 是质数 .void main() endl;41是质数 .int n = 18;cout rSomeRef:t rSomeRef 43 是质数 .int m = 0;endl;4
14、7 是质数 .doreturn 0;53 是质数 .cout m;intOne: 567 是质数 .if (n m)rSomeRef: 571 是质数 .cout 你猜的值太小了! endl;intOne: 873 是质数 .else if (n m)intTwo: 879 是质数 .cout 你猜的值太大了! endl;rSomeRef: 883 是质数 .else3-7 编写函数,参数为两个unsigned89 是质数 .cout 你猜对了! endl;short int 型数,返回值为第一个参数除97 是质数 .while(n !=m);以第二个参数的结果, 数据类型为 short2-3
15、3 定义一个表示时间的结构体, 可以int ;如果第二个参数为0,则返回值为精确表示年、月、日、小时、分、秒;程序运行输出:-1 。在主程序中实现输入输出。解:int fn1(int i,int j); / 求最大公约源程序:数的函数int i;#include void main()cout i ;a, unsigned short int b)int i,j,x,y;cout 从 1 累加到 i 的和为:cout 请输入一个正整数: ; fn1(i) i ;return -1;cout j ;return a/b;x = fn1(i,j);if (i = 1)y = i * j / x;r
16、eturn 1;typedef unsigned short int USHORT;cout i 和 j 的最大公elsetypedef unsigned long int ULONG;约数是: x endl;return i + fn1(i -1);int main()cout i 和 j 的最小公倍数是: y endl;程序运行输出:USHORT one, two;请输入一个正整数: 100short int answer;int fn1(int i, int j)从1累加到 100的和为: 5050cout one;if (i 2;cout two;temp = i;过程。answer
17、= Divider(one, two);i = j;解:if (answer -1)j = i;源程序见 实验指导 部分实验三cout Answer: answer;3-15用递归的方法编写函数求n阶勒让elsewhile(j != 0)德多项式的值,在主程序中实现输入、cout Error, cant divide by输出;zero!;temp = i % j;解:return 0;i = j;#include j = temp;float p(int n, int x);程序运行输出:void main()Enter two numbers.return i;Number one:8in
18、t n,x;Number two:2程序运行输出:cout n;3-8 编写函数把华氏温度转换为摄氏温请输入另一个正整数: 72cout x;主程序中提示用户输入一个华氏温度,120 和 72 的最小公倍数是: 360cout n = n endl;转化后输出相应的摄氏温度。3-12在主程序中提示输入整数n,编写cout x = x endl;解:函数用递归的方法求 1 +2 +n的cout P n ( x ) =源程序见 实验指导 部分实验三值。 p(n,x) endl;3-10 编写函数求两个整数的最大公约解:数和最小公倍数。#include float p(int n, int x)源程
19、序:#include #include int fn1(int i);if (n = 0)#include void main()return 1;else if (n = 1) return x;elsereturn (2*n-1)*x*p(n-1,x) - (n-1)*p(n-2,x) /n ; 程序运行输出: 请输入正整数 n: 1 请输入正整数 x: 2 n = 1x = 2 P1(2) = 2 请输入正整数 n: 3 请输入正整数 x: 4 n = 3x = 4 P3(4) = 154第 四章 类4-9 设计并测试一个名为 Rectangle 的 矩形类,其属性为矩形的左下角与右上
20、角两个点的坐标,能计算矩形的面积。 源程序:#include class Rectanglepublic:Rectangle (int top, intleft, intbottom, int right);Rectangle () int GetTop() const returnitsTop; int GetLeft()constreturnitsLeft; int GetBottom()constreturnitsBottom; int GetRight()constreturnitsRight; void SetTop(int top) itsTop= top; void SetLef
21、t (int left) itsLeft =left; void SetBottom(intbottom) itsBottom = bottom; void SetRight (intright)itsRight= right; int GetArea() const; private:int itsTop;int itsLeft; int itsBottom; int itsRight;Rectangle:Rectangle(int top, int left, int bottom, int right) itsTop = top; itsLeft = left; itsBottom =
22、bottom; itsRight = right;int Rectangle:GetArea() const int Width = itsRight-itsLeft;int Height = itsTop - itsBottom; return (Width * Height); int main()Rectangle MyRectangle (100, 20, 50, 80 );int Area = MyRectangle.GetArea(); cout Area: Area n; return 0; 程序运行输出: Area: 3000Upper Left X Coordinate: 2
23、04-11 定义一个矩形类,有长、宽两个属 性,有成员函数计算矩形的面积 解:#include class Rectangle public: 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()
24、float length, width; cout length;cout width;Rectangle r(length, width);cout 长为 length 宽为 width 的矩形的面积为: r.GetArea () endl; 程序运行输出: 请输入矩形的长度: 5 请输入矩形的宽度: 4 长为 5宽为 4 的矩形的面积为: 20 4-12 定义一个 数据类型 datatype 类,能处理包含字符型、整型、浮点型 三种类型的数据,给出其构造函数。 解:#include class datatype enum character, integer, floating_point
25、 vartype;union char c; int i; float f; public: datatype(char ch) vartype = character;c = ch; datatype(int ii) vartype = integer;i = ii; datatype(float ff) void main()程序运行输出:vartype = floating_point;这棵树的年龄为 12f = ff;float radius;这棵树的年龄为 16cout radius;Circle p(radius);5-12 在函数 fn1() 中定义一个静态变量void data
26、type:print() cout 半径为 radius 的圆n, fn1() 中对 n 的值加 1,在主函数中,switch (vartype) 的面积为: p.GetArea ()调用 fn1() 十次,显示 n 的值。case character: endl;解:cout 字符型 : c endl;#include break;程序运行输出:void fn1()case integer:请输入圆的半径: 5cout 整型 : i endl;半径为 5 的圆的面积为: 78.5static int n = 0;break;4-14 定义一个 tree 类,有成员 ages,n+;case
27、floating_point:成员函数 grow(int years) 对 ages 加上cout n 的值为 n endl;cout 浮点型 : f endl;years,age() 显示 tree 对象的 ages 的break;值。void main()解:#include for(int i = 0; i 10; i+)void main() class Tree fn1();datatype A(c), B(12), C(1.44F);int ages;A.print();public:程序运行输出:B.print();Tree(int n=0);n 的值为 1C.print();T
28、ree();n 的值为 2void grow(int years);n 的值为 3程序运行输出:void age();n 的值为 4字符型: c;n 的值为 5整型: 12Tree:Tree(int n) n 的值为 6浮点型: 1.44ages = n;n 的值为 74-13 定义一个 Circle 类,有数据成员n 的值为 8半径 Radius ,成员函数 GetArea() ,计Tree:Tree() n 的值为 9算圆的面积, 构造一个 Circle 的对象进age();n 的值为 10行测试。5-13定义类X、Y 乙函数h(X*),满解:void Tree:grow(int year
29、s) 足:类X有私有成员i,丫的成员函数g(X*)#include ages += years;是X的友元函数,实现对X的成员i加1 ,class Circle类Z是类X的友元类,其成员函数f(X*)void Tree:age() 实现对X的成员i加5,函数h(X*)是Xpublic:cout 这棵树的年龄为 ages 的友元函数,实现对 X的成员i加10。Circle(float radius) Radius =endl;在一个文件中定义和实现类,在另一个radius;文件中实现 main() 函数。Circle()void main()解:float GetArea() return 3.
30、14 *#include my_x_y_z.hRadius * Radius; Tree t(12);void main()private:t.age();float Radius;t.grow(4);X x;Z z;z.f(&x);public:在屏幕上显示出来。Boat(int j)weight = j;解:/ my_x_y_z.h 文件friend int totalWeight(Car &aCar,源程序:#ifndef MY_X_Y_Z_HBoat &aBoat);#include class X;int main()class Y int totalWeight(Car &aCar
31、 , Boatvoid g(X*);&aBoat)int myArray5;int i;class Xreturn aCar.weight + aBoat.weight;for ( i=0; i5; i+)private:void main()cout Value for myArray i myArrayi;X()i=0;Boat b1(5);friend void h(X*);cout totalWeight(c1 , b1) endl;for (i = 0; i5; i+)friend void Y:g(X*);cout i : myArrayi i =+10; 第 六 章 数组、指针与
32、字符void Y:g(X* x) x-i +; 串程序运行输出:class Z 6-1 数组 A10515 一共有多少个元Value for myArray0: 2public:素?Value for myArray1: 5void f(X* x) x-i += 5; 解:Value for myArray2: 7;10X 5X 15 = 750 个元素Value for myArray3: 8#endif / MY_X_Y_Z_H1-2 在数组 A20 中第一个元素和最后Value for myArray4: 3程序运行输出:无一个元素是哪一个?0: 25-14 定义 Boat 与 Car
33、两个类,二者都解:1: 5有 weight 属性,定义二者的一个友元函第一个元素是 A0 ,最后一个元素是2: 7数 totalWeight() ,计算二者的重量和。A19 。3: 8解:6-3 用一条语句定义一个有五个元素的4: 3源程序:整型数组,并依次赋予 15 的初值。6-11 引用和指针有何区别?何时只能#include 解:使用指针而不能使用引用?class Boat;源程序:解:class Carint IntegerArray5 = 1, 2 , 3 ,引用是一个别名,不能为 NULL值,不能4, 5 ;被重新分配;指针是一个存放地址的变private:或: int Integ
34、erArray= 1 , 2 , 3 ,量。当需要对变量重新赋以另外的地址int weight;4, 5 ;或赋值为NULL时只能使用指针。public:6-7 什么叫做指针?指针中储存的地址6-12 声明下列指针: float 类型变量的Car(int j)weight = j;和这个地址中的值有何区别?指针 pFloat , char 类型的指针 pStringfriend int totalWeight(Car &aCar ,解:和 struct customer 型的指针 prec 。Boat &aBoat);指针是一种数据类型,具有指针类型的解:;变量称为指针变量。指针变量存放的是f
35、loat *pfloat;class Boat另外一个对象的地址,这个地址中的值char *pString;就是另一个对象的内容。struct customer *prec;private:6-10 定义一个有五个元素的整型数组,6-13 给定 float 类型的指针 fp ,写出显int weight;在程序中提示用户输入元素值,最后再示 fp 所指向的值的输出流语句。解:cout Value = *fp;6-16定义一个整型变量 a, 个整型指 针P, 个引用r,通过p把a的值改为 10,通过 r 把 a 的值改为 5解:void main()int a;int *p = &a;int &
36、r = a; *p = 10;r = 5;6-21 编写一个函数, 统计一个英文句子 中字母的个数,在主程序中实现输入、 输出。解: 源程序:#include #include int count(char *str)int i,num=0;for (i=0; stri; i+)if ( (stri=a& stri=A & stri=Z) ) num+;return num;void main()char text100;cout 输入一个英语句子: endl; gets(text);cout 这个句子里有 count(text) 个字母。 endl; 程序运行输出: 输入一个英语句子: It
37、 is very interesting!这个句子里有 19 个字母。6-22 编写函数 int index(char *s , char *t) ,返回字符串 t 在字符串 s 中 出现的最左边的位置,如果在 s 中没有 与t匹配的子串,就返回-1。解:源程序:#include int index( char *s, char *t)int i,j,k;for(i = 0; si != 0; i+)for(j = i, k = 0; tk != 0& sj= tk; j+, k+)Jif (tk =0) return i;return -1;void main()int n;char str
38、120,str220;cout str1;cout str2;n = index(str1,str2);if (n 0)cout str2 在 str1 中左起第 n+1 个位置。 endl;elsecout str2 不在 str1 中。 endl;程序运行输出: 输入一个英语单词: abcdefgh 输入另一个英语单词: de de 在 abcdefghijk 中左起第 4 个位置。第 七 章继承与派生7-7 定义一个基类,构造其派生类,在 构造函数中输出提示信息,观察构造函 数的执行情况。解:#include class BaseClass public: BaseClass();Bas
39、eClass:BaseClass()cout 构造基类对象 ! endl;class DerivedClass :publicBaseClass public: DerivedClass();DerivedClass:DerivedClass()cout 构造派生类对象 ! endl; void main() DerivedClass d; 程序运行输出: 构造基类对象 ! 构造派生类对象 !7-8 定义一个 Document 类,有 name 成 员变量,从 Document派生出Book类, 增加PageCount变量。解:#include #include class Document
40、public:Document();Document( char *name ); char *Name; / Document name. void PrintNameOf(); / Print name.;Document:Document( char *name )Name= new char strlen( name ) + 1 ; strcpy( Name, name );void Document:PrintNameOf()void main()box a;cout Name endl;Derived a;程序运行输出:class Book : public Documenta.f
41、n1();构造 object 对象构造 box 对象public:7-10 定义 object 类,有 weight 属性及析构 box 对象Book( char *name, long pagecount );相应的操作函数,由此派生出box 类,析构 object 对象void PrintNameOf();增加 Height 和 width 属性及相应的操作private:函数,声明一个 box 对象,观察构造函7-11定义一个基类BaseClass,从它派long PageCount;数与析构函数的调用顺序。生出类 DerivedClass , BaseClass 有成;解:员函数 fn1() 、fn2() , DerivedClass 也Book:Book( char *name, long#include 有成员函数 fn1() 、fn2() ,在主程序中pagecount ):Document(name)class object定义一个 DerivedClass 的对象,
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年新型高性能石灰石建材研发与应用合作协议
- 2025年幼儿教育入园合同:幼儿快乐成长与家庭和谐共育协议
- 2025年度高端商务车租赁合作协议:专业定制化车辆租赁合同范本
- 工程项目居间合同协议书
- 2025年《中华人民共和国药品管理法》培训试卷+答案
- 2025-2030中国新能源市场发展创新规划及前景需求规模预测报告
- 2025年药品经营和使用质量监督管理办法习题(附答案)
- 执业药师《药事管理与法规》备考试题及答案
- 江西省“天工杯”食品安全快速检测技能竞赛理论试题及答案
- 2025年执业药师药事管理匹配题真题及答案
- 建筑装饰造型设计基础-完整全套教学课件
- 癌痛三阶梯治疗及阿片类镇痛药的合理使用
- 火灾应急预案篇
- 特斯拉更换电池标准
- 2023年贵州省注册会计师协会(贵州省资产评估协会)招考聘用笔试参考题库含答案解析
- 全过程工程咨询服务详细清单
- 法律法规法学 - 马工程《宪法学》重点整理
- 学生自主管理委员会章程
- 脑梗塞试题汇编
- 淋巴瘤基础知识
- SH/T 0660-1998气相防锈油试验方法
评论
0/150
提交评论