C++语言程序设计课后答案(1)_第1页
C++语言程序设计课后答案(1)_第2页
C++语言程序设计课后答案(1)_第3页
C++语言程序设计课后答案(1)_第4页
C++语言程序设计课后答案(1)_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、第二章C+简单程序设计 解: int yourAge = 39; 2-10执行完下列语句后,a b、c三个 源程序: cout I am: myAge years 变量的值为多少? #include old.n; a = 30; int main() cout You are: yourAge years b = a+; oldn; c = +a; int i; myAge+; / postfix increment a: 32 ; b: 30 ; c: 32; cout i; cout One year passes.n; 100到200,步长为2;然后用while和 cout 您输入一个数

2、字是 i endl; cout I am: myAge years dowhile语句完成同样的循环。 return 0; old.n; 解: cout You are: yourAge years for循环: 程序运行输出: oldn; for (int n = 100; n = 200; n += 2); 2-20打印ASC1码为32127的字符。 cout Another year passesn; while循环: #include cout I am: myAge+ years int x = 100; int main() old.n; while (n = 200) cout

3、You are: +yourAge n += 2; for (int i = 32; i128; i+) years oldn; dowhile循环: cout (char) i; cout Lets print it again.n; int n = 100; return 0; cout I am: myAge years do old.n; 程序运行输出: cout You are: yourAge years n += 2; !#$%G()*+,./0123456789:;?ABCDE oldn; while(n = 200); FGHIJKLMNOP_QRSTUVWXYZAabcde

4、fg return 0; 2-17修改下面这个程序中的错误,改正 hijklmnopqrstuvwxyzs 后它的运行结果是什么? 2-21运行下面的程序,观察其输出,与 解: #include 你的设想是否相同? 程序运行输出: void main() #include I am 39 years old int i int main() You are 39 years old int j; One year passes i = 10; /*给i赋值 unsigned int x; I am 40 years old j = 20; /* 给 j 赋值 */ unsigned int y

5、 = 100; You are 40 years old cout i + j = i + j; /* 输出结果 */ unsigned int z = 50; Another year passes return 0; x= y -乙 I am 40 years old cout Difference is: x; You are 41 years old 解: x = z - y; Lets print it again 改正: cout nNow difference is: x I am 41 years old #include endl; You are 41 years old

6、int main() return 0; 2-28编写一个完整的程序,实现功能: 向用户提问现在正在下雨吗?,提示 int i; 程序运行输出: 用户输入Y或N。若输入为Y,显示现 int j; Difference is: 50 在正在下雨。;若输入为N,显示现 i = 10; /给i赋值 Now difference is: 4294967246 在没有下雨。;否则继续提问现在正 j = 20; /* 给 j 赋值 */ 注意,第二行的输出并非-50,注意X、 在下雨吗? cout i + j = i + j; /* 输出结果 */ y、z的数据类型。 源程序: return 0; 2-2

7、2运行下面的程序,观察其输出,体 #include 会i+与+i的差别。 #include 程序运行输出: #include void main() i + j = 30 int main() 2-18编写一个程序,运行时提示输入一 char flag; 个数字,再把这个数字显示出来。 int myAge = 39; / initialize two integers while(1) case 6: flag = 1; cout 现在正在下雨吗?(Yes or No):; cout flag; break; j = 2; if ( toupper(flag) = Y) default: do

8、 cout 你的成绩为差!; if(i%j = 0) cout 现在正在下雨。; break; flag = 0; break; if ( toupper(flag) = N) 程序运行输出: 你考试考了多少分?(0100): 85 j+; cout 现在没有下雨。; 你的成绩为良! while (j = k); break; 2-31用穷举法找出1100间的质数,显 if (flag) 示出来。分别使用while,do-while, for cout i 是质数. endl; 循环语句实现。 i+; 解: while(i = 100); 程序运行输出: 源程序: 现在正在下雨吗? (Yes

9、or No):x 使用while循环语句: 使用for循环语句: 现在正在下雨吗? (Yes or No):l #include #include 现在正在下雨吗? (Yes or No):q #include #include 现在正在下雨吗? (Yes or No):n void main() void main() 现在没有下雨。 或: int i,j,k,flag; int i,j,k,flag; 现在正在下雨吗? (Yes or No):y i = 2; for(i = 2; i = 100; i+) 现在正在下雨。 while(i = 100) 2-29编写一个完整的程序,运行时向

10、用 flag = 1; 户提问你考试考了多少分?(0100) , flag = 1; k = sqrt(i); 接收输入后判断其等级,显示出来。规 k = sqrt(i); for (j = 2; j = k; j+) 则如下: j = 2; 解: while (j = k) if(i%j = 0) #include void main() if(i%j = 0) flag = 0; break; int i,score; flag = 0; cout score; if (flag) if (score100 | score0) j+; cout i 是质数. endl; cout 分数值必

11、须在 0到100之间!; else if (flag) cout i 是质数. endl; 程序运行输出: i = score/10; i+; 2是质数. switch (i) 3是质数. 5是质数. case 10: 使用dowhile循环语句: 7是质数. case 9: #include 11是质数. cout 你的成绩为优!; #include 13是质数. break; void main() 17是质数. case 8: 19是质数. cout 你的成绩为良!; int i,j,k,flag; 23是质数. break; i = 2; 29是质数. case 7: do 31是质数

12、. 37是质数. cout m; int;如果第二个参数为0,则返回值为-1。 47是质数. if (n m) 在主程序中实现输入输出。 53是质数. cout 你猜的值太小了! endl; 解: 59是质数. else if (n m) 源程序: 61是质数. cout 你猜的值太大了! endl; #include 67是质数. else short int Divider(unsigned short int a, 71是质数. cout 你猜对了! endl; unsigned short int b) 73是质数. while(n != m); 79是质数. if (b = 0) 8

13、3是质数. 程序运行输出: return -1; 89是质数. 请猜这个数的值为多少?(0100):50 else 97是质数. 你猜的值太大了! return a/b; 2-33定义一个表示时间的结构体,可以 请猜这个数的值为多少?(0100):25 精确表示年、月、日、小时、分、秒; 你猜的值太大了! typedef unsigned short int USHORT; 提示用户输入年、月、日、小时、分、 请猜这个数的值为多少?(0100):10 typedef unsigned long int ULONG; 秒的值,然后完整地显示出来。 你猜的值太小了! int main() 解: 请

14、猜这个数的值为多少?(0100):15 源程序见”实验指导部分实验二 你猜的值太小了! USHORT one, two; 2-34在程序中定义一个整型变量,赋以 请猜这个数的值为多少?(0100):18 short int answer; 1100的值,要求用户猜这个数,比较两 你猜对了! cout one; 语句实现循环。 想的有何不同?仔细体会引用的用法。 cout two; /使用while语句 #include answer = Divider(one, two); #include int main() if (answer -1) void main() cout Answer:

15、answer; int n = 18; int intOne; else int m = 0; int cout Error, cant divide by zero!; while(m != n) intOne = 5; return 0; cout intOne:tt intOne endl; cout 请猜这个数的值为多少? cout rSomeRef:t rSomeRef m; int intTwo = 8; Number one:8 if (n m) rSomeRef = intTwo; / not what you think! Number two:2 cout 你猜的值太小了!

16、 endl; cout nintOne:tt intOne endl; Answer: 4 else if (n m) cout intTwo:tt intTwo endl; 3-8编写函数把华氏温度转换为摄氏温 cout 你猜的值太大了! endl; cout rSomeRef:t rSomeRef 度,公式为:C = (F - 32) * 59;在主程序 else endl; 中提示用户输入一个华氏温度,转化后 cout 你猜对了! endl; return 0; 输出相应的摄氏温度。 解: 程序运行输出: 源程序见实验指导部分实验三 /使用dowhile语句 intOne: 5 3-10

17、编写函数求两个整数的最大公约 #include rSomeRef: 5 数和最小公倍数。 void main() intOne: 8 源程序: int n = 18; intTwo: 8 #include int m = 0; rSomeRef: 8 #include do 3-7编写函数,参数为两个unsigned int fn1(int i,int j); /求最大公约数的函数 void main() P1(2) = 2 int fn1(int i) 请输入正整数n: 3 int i,j,x,y; 请输入正整数x: 4 cout i ; return 1; x = 4 cout j ; r

18、eturn i + fn1(i -1); 第四章类 x = fn1(i,j); 4-9设计并测试一个名为Rectangle的矩 y = i * j / x; 程序运行输出: 形类,其属性为矩形的左下角与右上角 cout i 和 j 的最大公约数 请输入一个正整数:100 两个点的坐标,能计算矩形的面积。 是: x endl; 从1累加到100的和为:5050 源程序: cout i 和 j 的最小公倍数 3-14用递归的方法编写函数求 #include 是: y 2; int fn1(int i, int j) fib(1) = fib(2) = 1;观察递归调用的过程。 public: 解:

19、 Rectangle (int top, int left, int bottom, int int temp; 源程序见实验指导部分实验三 right); if (i j) 3-15用递归的方法编写函数求n阶勒让 Rectangle () 德多项式的值,在主程序中实现输入、 int GetTop() const return itsTop; temp = i; 输出; int GetLeft() const return itsLeft; i = j; 解: int GetBottom() const return itsBottom; j = i; #include int GetRigh

20、t() const return itsRight; float p(int n, int x); void SetTop(int top) itsTop = top; while(j != 0) void main() void SetLeft (int left) itsLeft = left; void SetBottom (int bottom) itsBottom = temp = i % j; int n,x; bottom; i = j; cout n; int GetArea() const; cout x; int itsTop; cout n = n en dl; int

21、itsLeft; 程序运行输出: cout x = x en dl; int itsBottom; 请输入一个正整数:120 cout P n ( x )= int itsRight; 请输入另一个正整数:72 p(n,x) endl; ; 120和72的最大公约数是:24 Rectangle:Rectangle(int top, int left, int 120和72的最小公倍数是:360 float p(int n, int x) bottom, int right) 3-12在主程序中提示输入整数n,编写 函数用递归的方法求 1+ 2 + n的 if (n = 0) itsTop =

22、top; 值。 return 1; itsLeft = left; 解: else if (n = 1) itsBottom = bottom; #include return x; itsRight = right; #include else int fn1(int i); return (2*n-1)*x*p(n-1,x) - (n-1)*p(n-2,x) int Rectangle:GetArea() const void main() /n ; int Width = itsRight-itsLeft; int i; 程序运行输出: int Height = itsTop - its

23、Bottom; cout i ; 请输入正整数x: 2 cout 从1累加到i 的和为: n = 1 int main() fn1(i) endl; x = 2 Rectangle MyRectangle (100, 20, 50, 80 ); enum 径Radius,成员函数 GetArea(),计算圆 int Area = MyRectangle.GetArea(); character, 的面积,构造一个Circle的对象进行测 cout Area: Area n; integer, 试。 return 0; floating_point 解: vartype; #include 程序

24、运行输出: union class Circle Area: 3000 Upper Left X Coordinate: 20 char c; public: 4-11定义一个矩形类,有长、宽两个属 int i; Circle(float radius) Radius = radius; 性,有成员函数计算矩形的面积 float f; Circle() 解: ; float GetArea() return 3.14 * Radius * #include public: Radius; class Rectangle datatype(char ch) private: vartype =

25、character; float Radius; public: c = ch; ; Rectangle(float len, float width) void main() datatype(int ii) Length = len; vartype = integer; float radius; Width = width; i = ii; cout radius; Rectangle(); datatype(float ff) Circle p(radius); float GetArea() return Length * Width; vartype = floating_poi

26、nt; cout 半径为 radius 的圆的面 float GetLength() return Length; f = ff; 积为: p.GetArea () float GetWidth() return Width; endl; private: void print(); float Length; ; 程序运行输出: float Width; void datatype:print() 请输入圆的半径:5 ; switch (vartype) 半径为5的圆的面积为:78.5 void main() case character: 4-14定义一个tree类,有成员ages,成 c

27、out 字符型: c endl; 员函数 grow(int years)对 ages 力口 上 float length, width; break; years,age()显示tree对象的ages的值。 cout length; cout 整型: i endl; #include cout width; case floating_point: int ages; Rectangle r(length, width); cout 浮点型: f endl; public: cout 长为 length 宽为 break; Tree(int n=0); width 的矩形的面积为: Tree(

28、); r.GetArea () endl; void grow(int years); void main() void age(); 程序运行输出: datatype A(c), B(12), C(1.44F); ; 请输入矩形的长度:5 A.print(); Tree:Tree(int n) 请输入矩形的宽度:4 B.print(); ages = n; 长为5宽为4的矩形的面积为:20 C.print(); 4-12定义一个数据类型datatype类, Tree:Tree() 能处理包含字符型、整型、浮点型三种 程序运行输出: age(); 类型的数据,给出其构造函数。 字符型:c 解:

29、 整型:12 void Tree:grow(int years) #include 浮点型:1.44 ages += years; class datatype 4-13定义一个Circle类,有数据成员半 void Tree:age() 解: private: cout 这棵树的年龄为 ages #include my_x_y_z.h int weight; endl; void main() public: Boat(int j)weight = j; void main() X x; friend int totalWeight(Car Tree t(12); z.f( ; t.age(

30、); int totalWeight(Car / my_x_y_z.h 文件 #ifndef MY_X_Y_Z_H return aCar.weight + aBoat.weight; 程序运行输出: class X; 这棵树的年龄为12 class Y void main() 这棵树的年龄为16 void g(X*); 第五章C+程序的基本结构 ; Car c1(4); class X Boat b1(5); 5-12在函数fn1()中定义一个静态变量 cout totalWeight(c1 ,b1) endl; n, fn1()中对n的值加1,在主函数中, private: 调用fn1()

31、十次,显示n的值。 int i; 程序运行输出: 解: public: 9 #include X()i=0; 第六章数组、指针与字符 void fn1() friend void h(X*); 串 friend void Y:g(X*); 6-1数组A10515 共有多少个元 static int n = 0; friend class Z; 素? n+; ; 解: cout n 的值为i =+10; 10X 5 X 15 = 750 个元素 void Y:g(X* x) x-i +; 1-2在数组A20中第一个元素和最后一 void main() class Z 个元素是哪一个? publi

32、c: 解: for(int i = 0; i i += 5; 第一个元素是 A0,最后一个元素是 fn1(); ; A19。 #endif / MY_X_Y_Z_H 6-3用一条语句定义一个有五个元素的 程序运行输出: 程序运行输出:无 整型数组,并依次赋予 15的初值。 n的值为1 5-14定义Boat与Car两个类,二者都有 解: n的值为2 weight属性,定义二者的一个友元函数 源程序: n的值为3 totalWeight(),计算二者的重量和。 int IntegerArray5 = 1 , 2,3, 4, 5 ; n的值为4 解: 或:int IntegerArray = 1,2

33、,3,4, n的值为5 源程序: 5 ; n的值为6 #include 6-7什么叫做指针?指针中储存的地址 n的值为7 class Boat; 和这个地址中的值有何区别? n的值为8 class Car 解: n的值为9 指针是一种数据类型,具有指针类型的 n的值为10 private: 变量称为指针变量。指针变量存放的是 5-13定义类X、Y、乙函数h(X*),满足: int weight; 另外一个对象的地址,这个地址中的值 类X有私有成员i,Y的成员函数g(X*) public: 就是另一个对象的内容。 是X的友元函数,实现对X的成员i加1, Car(int j)weight = j;

34、 6-10定义一个有五个元素的整型数组, 类Z是类X的友元类,其成员函数f(X*) friend int totalWeight(Car 在屏幕上显示出来。 的友元函数,实现对 X的成员i加10。 ; 解: 在一个文件中定义和实现类,在另一个 class Boat 源程序: 文件中实现main()函数。 #include int main() int myArray5; int i; for ( i=0; i5; i+) cout Value for myArray i myArrayi; for (i = 0; i5; i+) cout i : myArrayi n; return 0; 程

35、序运行输出: Value for myArray0: 2 Value for myArray1: 5 Value for myArray2: 7 Value for myArray3: 8 Value for myArray4: 3 0: 2 1: 5 2: 7 3: 8 4: 3 6-11引用和指针有何区别?何时只能 使用指针而不能使用引用? 解: 引用是一个别名,不能为NULL值,不能 被重新分配;指针是一个存放地址的变 量。当需要对变量重新赋以另外的地址 或赋值为NULL时只能使用指针。 6-12声明下列指针:float类型变量的指 针pFloat,char类型的指针 pString和

36、struct customer 型的指针 prec。 解: float *pfloat; char *pString; struct customer *prec; 6-13给定float类型的指针fp,写出显示 fp所指向的值的输出流语句。 解: cout Value = *fp; 6-16定义一个整型变量 a,一个整型指 针P,个引用r,通过p把a的值改为 10,通过r把a的值改为5 解: void main() int a; int *p = int *p = 10; r = 5; 6-21编写一个函数,统计一个英文句子 中字母的个数,在主程序中实现输入、 输出。 解: 源程序: #in

37、clude #include int count(char *str) int i,num=0; for (i=0; stri; i+) if ( (stri=a return num; void main() char text100; cout 输入一个英语句子: endl; gets(text); cout 这个句子里有 count(text) 个字母。vv endl; 程序运行输出: 输入一个英语句子: It is very interesting! 这个句子里有19个字母。 6-22 编写函数 int index(char *s, char *t),返回字符串t在字符串s中出现的 最

38、左边的位置,如果在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 j+, k+) J if (tk =0) return i; return -1; void main() int n; char str120,str220; cout str1; cout str2; n = index(str1,str2); if (n 0) cout str2 在vv str1 中左起第 class

39、BaseClass public: BaseClass(); ; BaseClass:BaseClass() cout vv 构造基类对象! vv endl; class DerivedClass : public BaseClass public: DerivedClass(); ; DerivedClass:DerivedClass() cout vv 构造派生类对象! vv endl; void main() DerivedClass d; 程序运行输出: 构造基类对象! 构造派生类对象! 7-8 定义一个 Document类,有 name 成 员变量,从 Document派生出Book

40、类, 增加PageCount变量。 解: #include #include class Document 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() cout Name endl;

41、class Book : public Document public: 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); b.PrintNameOf(); 程序运行输出: Name of book: Book1 7-9定义基类Base,有两个共有成员函 数fn1()、fn2(),私有派生出 Derived类, 如果想在Derived类的对象中使用基类 函数fn1(),应怎么办? 解: class Base public: int fn1() const return 1; int fn2() const return 2; ; class Derived : private Base publ

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论