語(yǔ)言程序設(shè)計(jì)課后答案_第1頁(yè)
語(yǔ)言程序設(shè)計(jì)課后答案_第2頁(yè)
語(yǔ)言程序設(shè)計(jì)課后答案_第3頁(yè)
語(yǔ)言程序設(shè)計(jì)課后答案_第4頁(yè)
語(yǔ)言程序設(shè)計(jì)課后答案_第5頁(yè)
已閱讀5頁(yè),還剩21頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、第 二 章 C+簡(jiǎn)單程序設(shè)計(jì)2-10 執(zhí)行完下列語(yǔ)句后,a、b、c三個(gè)變量的值為多少?a = 30;b = a+;c = +a;解: a:32 ; b:30 ; c:32;2-13 寫一條for語(yǔ)句,計(jì)數(shù)條件為n從100到200,步長(zhǎng)為2;然后用while和dowhile語(yǔ)句完成同樣的循環(huán)。解: for循環(huán):for (int n = 100; n <= 200; n += 2); while循環(huán):int x = 100;while (n <= 200)n += 2; dowhile循環(huán):int n = 100;don += 2; while(n <= 200);2-17 修改

2、下面這個(gè)程序中的錯(cuò)誤,改正后它的運(yùn)行結(jié)果是什么?#include <iostream.h>void main()int iint j;i = 10; /* 給i賦值j = 20; /* 給j賦值 */cout << "i + j = << i + j; /* 輸出結(jié)果 */return 0;解: 改正:#include <iostream.h>int main()int i;int j;i = 10; / 給i賦值j = 20; /* 給j賦值 */cout << "i + j = " <<

3、i + j; /* 輸出結(jié)果 */return 0;程序運(yùn)行輸出:i + j = 302-18 編寫一個(gè)程序,運(yùn)行時(shí)提示輸入一個(gè)數(shù)字,再把這個(gè)數(shù)字顯示出來(lái)。解: 源程序:#include <iostream.h> int main()int i;cout << "請(qǐng)輸入一個(gè)數(shù)字:"cin >> i;cout << "您輸入一個(gè)數(shù)字是" << i << endl;return 0;程序運(yùn)行輸出:請(qǐng)輸入一個(gè)數(shù)字:5您輸入一個(gè)數(shù)字是52-20 打印ASCII碼為32127的字符。解: #i

4、nclude <iostream.h>int main()for (int i = 32; i<128; i+)cout << (char) i;return 0;程序運(yùn)行輸出:!"#$%G'()*+,./0123456789:;<>?ABCDEFGHIJKLMNOP_QRSTUVWXYZ'abcdefghijklmnopqrstuvwxyz<|>s2-21 運(yùn)行下面的程序,觀察其輸出,與你的設(shè)想是否相同?#include <iostream.h>int main()unsigned int x;uns

5、igned 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;解: 程序運(yùn)行輸出:Difference is: 50Now difference is: 4294967246注意,第二行的輸出并非 -50,注意x、y、z的數(shù)據(jù)類型。2-22 運(yùn)行下面的程序,觀察其輸出,體會(huì)i+與+

6、i的差別。#include <iostream.h>int main()int myAge = 39; / initialize two integersint yourAge = 39;cout << "I am: " << myAge << " years old.n"cout << "You are: " << yourAge << " years oldn"myAge+; / postfix increment+yourAg

7、e; / 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

8、: " << myAge+ << " years old.n"cout << "You are: " << +yourAge << " years oldn"cout << "Let's print it again.n"cout << "I am: " << myAge << " years old.n"cout << "You

9、 are: " << yourAge << " years oldn"return 0;解: 程序運(yùn)行輸出: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 oldLet's print it againI am 41 years oldYou are 41 years old2-28 編寫一個(gè)完整的程

10、序,實(shí)現(xiàn)功能:向用戶提問"現(xiàn)在正在下雨嗎?",提示用戶輸入Y或N。若輸入為Y,顯示"現(xiàn)在正在下雨。"; 若輸入為N,顯示"現(xiàn)在沒有下雨。";否則繼續(xù)提問"現(xiàn)在正在下雨嗎?"解: 源程序:#include <iostream.h>#include <stdlib.h> void main()char flag;while(1)cout << "現(xiàn)在正在下雨嗎?(Yes or No):"cin >> flag;if ( toupper(flag) =

11、'Y')cout << "現(xiàn)在正在下雨。"break;if ( toupper(flag) = 'N')cout << "現(xiàn)在沒有下雨。"break;程序運(yùn)行輸出:現(xiàn)在正在下雨嗎?(Yes or No):x現(xiàn)在正在下雨嗎?(Yes or No):l現(xiàn)在正在下雨嗎?(Yes or No):q現(xiàn)在正在下雨嗎?(Yes or No):n現(xiàn)在沒有下雨。或:現(xiàn)在正在下雨嗎?(Yes or No):y現(xiàn)在正在下雨。2-29 編寫一個(gè)完整的程序,運(yùn)行時(shí)向用戶提問"你考試考了多少分?(0100)"

12、;,接收輸入后判斷其等級(jí),顯示出來(lái)。規(guī)則如下:解: #include <iostream.h> void main()int i,score;cout << "你考試考了多少分?(0100):"cin >> score;if (score>100 | score<0)cout << "分?jǐn)?shù)值必須在0到100之間!"elsei = score/10;switch (i)case 10:case 9:cout << "你的成績(jī)?yōu)閮?yōu)!"break;case 8:cout

13、 << "你的成績(jī)?yōu)榱迹?quot;break;case 7:case 6:cout << "你的成績(jī)?yōu)橹校?quot;break;default:cout << "你的成績(jī)?yōu)椴睿?quot; 程序運(yùn)行輸出:你考試考了多少分?(0100):85你的成績(jī)?yōu)榱迹?-31 用窮舉法找出1100間的質(zhì)數(shù),顯示出來(lái)。分別使用while,do-while,for循環(huán)語(yǔ)句實(shí)現(xiàn)。解: 源程序: 使用while循環(huán)語(yǔ)句:#include <iostream.h>#include <math.h> void main()in

14、t 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 << "是質(zhì)數(shù)." << endl;i+; 使用dowhile循環(huán)語(yǔ)句:#include <iostream.h>#include <math.h> void main()int i,j,k,flag;i = 2;doflag = 1;k = sqrt(i);j

15、 = 2;doif(i%j = 0)flag = 0;break;j+;while (j <= k);if (flag)cout << i << "是質(zhì)數(shù)." << endl;i+;while(i <= 100); 使用for循環(huán)語(yǔ)句:#include <iostream.h>#include <math.h> void main()int i,j,k,flag;for(i = 2; i <= 100; i+)flag = 1;k = sqrt(i);for (j = 2; j <= k;

16、j+)if(i%j = 0)flag = 0;break;if (flag)cout << i << "是質(zhì)數(shù)." << endl;程序運(yùn)行輸出:2是質(zhì)數(shù).3是質(zhì)數(shù).5是質(zhì)數(shù).7是質(zhì)數(shù).11是質(zhì)數(shù).13是質(zhì)數(shù).17是質(zhì)數(shù).19是質(zhì)數(shù).23是質(zhì)數(shù).29是質(zhì)數(shù).31是質(zhì)數(shù).37是質(zhì)數(shù).41是質(zhì)數(shù).43是質(zhì)數(shù).47是質(zhì)數(shù).53是質(zhì)數(shù).59是質(zhì)數(shù).61是質(zhì)數(shù).67是質(zhì)數(shù).71是質(zhì)數(shù).73是質(zhì)數(shù).79是質(zhì)數(shù).83是質(zhì)數(shù).89是質(zhì)數(shù).97是質(zhì)數(shù). 2-33 定義一個(gè)表示時(shí)間的結(jié)構(gòu)體,可以精確表示年、月、日、小時(shí)、分、秒;提示用戶輸入年、月、日、小時(shí)、

17、分、秒的值,然后完整地顯示出來(lái)。 解: 源程序見"實(shí)驗(yàn)指導(dǎo)"部分實(shí)驗(yàn)二2-34 在程序中定義一個(gè)整型變量,賦以1100的值,要求用戶猜這個(gè)數(shù),比較兩個(gè)數(shù)的大小,把結(jié)果提示給用戶,直到猜對(duì)為止。分別使用while、dowhile語(yǔ)句實(shí)現(xiàn)循環(huán)。解: /使用while語(yǔ)句#include <iostream.h> void main() int n = 18;int m = 0;while(m != n) cout << "請(qǐng)猜這個(gè)數(shù)的值為多少?(0100):"cin >> m;if (n > m)cout <&

18、lt; "你猜的值太小了!" << endl;else if (n < m)cout << "你猜的值太大了!" << endl;elsecout << "你猜對(duì)了!" << endl;/使用dowhile語(yǔ)句#include <iostream.h> void main() int n = 18;int m = 0;docout << "請(qǐng)猜這個(gè)數(shù)的值為多少?(0100):"cin >> m;if (n >

19、 m)cout << "你猜的值太小了!" << endl;else if (n < m)cout << "你猜的值太大了!" << endl;elsecout << "你猜對(duì)了!" << endl;while(n != m); 程序運(yùn)行輸出:請(qǐng)猜這個(gè)數(shù)的值為多少?(0100):50你猜的值太大了!請(qǐng)猜這個(gè)數(shù)的值為多少?(0100):25你猜的值太大了! 請(qǐng)猜這個(gè)數(shù)的值為多少?(0100):10你猜的值太小了!請(qǐng)猜這個(gè)數(shù)的值為多少?(0100):15你猜的值

20、太小了!請(qǐng)猜這個(gè)數(shù)的值為多少?(0100):18你猜對(duì)了! 第三章 函數(shù)3-2 觀察下面程序的運(yùn)行輸出,與你設(shè)想的有何不同?仔細(xì)體會(huì)引用的用法。源程序:#include <iostream.h> int main()int intOne;int &rSomeRef = intOne;intOne = 5;cout << "intOne:tt" << intOne << endl;cout << "rSomeRef:t" << rSomeRef << endl;in

21、t intTwo = 8;rSomeRef = intTwo; / not what you think!cout << "nintOne:tt" << intOne << endl;cout << "intTwo:tt" << intTwo << endl;cout << "rSomeRef:t" << rSomeRef << endl;return 0;程序運(yùn)行輸出:intOne: 5rSomeRef: 5intOne: 8

22、intTwo: 8rSomeRef: 83-7 編寫函數(shù),參數(shù)為兩個(gè)unsigned short int型數(shù),返回值為第一個(gè)參數(shù)除以第二個(gè)參數(shù)的結(jié)果,數(shù)據(jù)類型為short int;如果第二個(gè)參數(shù)為0,則返回值為-1。在主程序中實(shí)現(xiàn)輸入輸出。解: 源程序:#include <iostream.h>short int Divider(unsigned short int a, unsigned short int b)if (b = 0)return -1;elsereturn a/b;typedef unsigned short int USHORT;typedef unsigned

23、 long int ULONG;int main()USHORT one, two;short int answer;cout << "Enter two numbers.n Number one: "cin >> one;cout << "Number two: "cin >> two;answer = Divider(one, two);if (answer > -1)cout << "Answer: " << answer;elsecout <&

24、lt; "Error, can't divide by zero!"return 0;程序運(yùn)行輸出:Enter two numbers.Number one:8Number two:2Answer: 43-8 編寫函數(shù)把華氏溫度轉(zhuǎn)換為攝氏溫度,公式為:C = (F - 32) * 5/9; 在主程序中提示用戶輸入一個(gè)華氏溫度,轉(zhuǎn)化后輸出相應(yīng)的攝氏溫度。解: 源程序見"實(shí)驗(yàn)指導(dǎo)"部分實(shí)驗(yàn)三3-10 編寫函數(shù)求兩個(gè)整數(shù)的最大公約數(shù)和最小公倍數(shù)。解: 源程序:#include <iostream.h>#include <math.h&

25、gt;int fn1(int i,int j); /求最大公約數(shù)的函數(shù)void main()int i,j,x,y;cout << "請(qǐng)輸入一個(gè)正整數(shù):"cin >> i ;cout << "請(qǐng)輸入另一個(gè)正整數(shù):"cin >> j ;x = fn1(i,j);y = i * j / x;cout << i << "和" << j << "的最大公約數(shù)是:" << x << endl;cout &l

26、t;< i << "和" << j << "的最小公倍數(shù)是:" << y << endl;int fn1(int i, int j)int temp;if (i < j)temp = i;i = j;j = i;while(j != 0)temp = i % j;i = j;j = temp;return i;程序運(yùn)行輸出:請(qǐng)輸入一個(gè)正整數(shù):120請(qǐng)輸入另一個(gè)正整數(shù):72120和72的最大公約數(shù)是:24120和72的最小公倍數(shù)是:3603-12 在主程序中提示輸入整數(shù)n,編寫函數(shù)用遞

27、歸的方法求1 + 2 + + n的值。解: #include <iostream.h>#include <math.h>int fn1(int i);void main()int i;cout << "請(qǐng)輸入一個(gè)正整數(shù):"cin >> i ;cout << "從1累加到" <<i << "的和為:" << fn1(i) << endl;int fn1(int i)if (i = 1)return 1;elsereturn i +

28、 fn1(i -1);程序運(yùn)行輸出: 請(qǐng)輸入一個(gè)正整數(shù):100從1累加到100的和為:50503-14 用遞歸的方法編寫函數(shù)求Fibonacci 級(jí)數(shù),公式為fib(n) = fib(n-1) + fib(n-2),n>2; fib(1) = fib(2) = 1;觀察遞歸調(diào)用的過程。解: 源程序見"實(shí)驗(yàn)指導(dǎo)"部分實(shí)驗(yàn)三3-15 用遞歸的方法編寫函數(shù)求n階勒讓德多項(xiàng)式的值,在主程序中實(shí)現(xiàn)輸入、輸出;解: #include <iostream.h>float p(int n, int x);void main()int n,x;cout << &

29、quot;請(qǐng)輸入正整數(shù)n:"cin >> n;cout << "請(qǐng)輸入正整數(shù)x:"cin >> x;cout << "n = " << n << endl;cout << "x = " << x << endl;cout << "P" << n << "(" << x << ") = " <&l

30、t; 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-2,x) /n ;程序運(yùn)行輸出:請(qǐng)輸入正整數(shù)n:1請(qǐng)輸入正整數(shù)x:2n = 1x = 2P1(2) = 2請(qǐng)輸入正整數(shù)n:3請(qǐng)輸入正整數(shù)x:4n = 3x = 4P3(4) = 154 第 四 章 類4-9 設(shè)計(jì)并測(cè)試一個(gè)名為Rectangle的矩形類,其屬性為矩形的左下角與右上角兩個(gè)點(diǎn)的坐標(biāo),能計(jì)算矩形的面積。解: 源程序:#inclu

31、de <iostream.h>class Rectanglepublic:Rectangle (int top, int left, int bottom, int right);Rectangle () int GetTop() const return itsTop; int GetLeft() const return itsLeft; int GetBottom() const return itsBottom; int GetRight() const return itsRight; void SetTop(int top) itsTop = top; void Set

32、Left (int left) itsLeft = left; void SetBottom (int bottom) itsBottom = bottom; void SetRight (int right) 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

33、 = bottom;itsRight = right;int Rectangle:GetArea() constint 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

34、 0;程序運(yùn)行輸出:Area: 3000Upper Left X Coordinate: 204-11 定義一個(gè)矩形類,有長(zhǎng)、寬兩個(gè)屬性,有成員函數(shù)計(jì)算矩形的面積 解: #include <iostream.h>class Rectanglepublic:Rectangle(float len, float width)Length = len;Width = width;Rectangle();float GetArea() return Length * Width; float GetLength() return Length; float GetWidth() retur

35、n Width; private:float Length;float Width;void main()float length, width;cout << "請(qǐng)輸入矩形的長(zhǎng)度:"cin >> length;cout << "請(qǐng)輸入矩形的寬度:"cin >> width;Rectangle r(length, width);cout << "長(zhǎng)為" << length << "寬為" << width <<

36、; "的矩形的面積為:" << r.GetArea () << endl;程序運(yùn)行輸出:請(qǐng)輸入矩形的長(zhǎng)度:5請(qǐng)輸入矩形的寬度:4長(zhǎng)為5寬為4的矩形的面積為:204-12 定義一個(gè)"數(shù)據(jù)類型" datatype類,能處理包含字符型、整型、浮點(diǎn)型三種類型的數(shù)據(jù),給出其構(gòu)造函數(shù)。解: #include <iostream.h>class datatypeenumcharacter,integer,floating_point vartype;union char c;int i;float f;public:datatype

37、(char ch) vartype = character;c = ch;datatype(int ii) vartype = integer;i = ii;datatype(float ff) vartype = floating_point;f = ff;void print();void datatype:print() switch (vartype) case character:cout << "字符型: " << c << endl;break;case integer:cout << "整型: &qu

38、ot; << i << endl;break;case floating_point:cout << "浮點(diǎn)型: " << f << endl;break;void main() datatype A('c'), B(12), C(1.44F);A.print();B.print();C.print();程序運(yùn)行輸出:字符型: c整型: 12浮點(diǎn)型: 1.444-13 定義一個(gè)Circle類,有數(shù)據(jù)成員半徑Radius,成員函數(shù)GetArea(),計(jì)算圓的面積,構(gòu)造一個(gè)Circle的對(duì)象進(jìn)行測(cè)試。解

39、: #include <iostream.h>class Circlepublic:Circle(float radius) Radius = radius;Circle()float GetArea() return 3.14 * Radius * Radius; private:float Radius;void main()float radius;cout << "請(qǐng)輸入圓的半徑:"cin >> radius;Circle p(radius);cout << "半徑為" << radiu

40、s << "的圓的面積為:" << p.GetArea () << endl;程序運(yùn)行輸出:請(qǐng)輸入圓的半徑:5半徑為5的圓的面積為:78.54-14 定義一個(gè)tree類,有成員ages,成員函數(shù)grow(int years)對(duì)ages加上years,age()顯示tree對(duì)象的ages的值。解: #include <iostream.h>class Tree int ages;public:Tree(int n=0);Tree();void grow(int years);void age();Tree:Tree(int n)

41、 ages = n;Tree:Tree() age();void Tree:grow(int years) ages += years;void Tree:age() cout << "這棵樹的年齡為" << ages << endl;void main()Tree t(12);t.age();t.grow(4);程序運(yùn)行輸出:這棵樹的年齡為12這棵樹的年齡為16第 五 章 C+程序的基本結(jié)構(gòu) 5-12 在函數(shù)fn1()中定義一個(gè)靜態(tài)變量n,fn1()中對(duì)n的值加1,在主函數(shù)中,調(diào)用fn1()十次,顯示n的值。解: #include &l

42、t;iostream.h>void fn1()static int n = 0;n+;cout << "n的值為" << n <<endl;void main()for(int i = 0; i < 10; i+)fn1();程序運(yùn)行輸出:n的值為1n的值為2n的值為3n的值為4n的值為5n的值為6n的值為7n的值為8n的值為9n的值為105-13 定義類X、Y、Z,函數(shù)h(X*),滿足:類X有私有成員i,Y的成員函數(shù)g(X*)是X的友元函數(shù),實(shí)現(xiàn)對(duì)X的成員i加1,類Z是類X的友元類,其成員函數(shù)f(X*)實(shí)現(xiàn)對(duì)X的成員i加5,

43、函數(shù)h(X*)是X的友元函數(shù),實(shí)現(xiàn)對(duì)X的成員i加10。在一個(gè)文件中定義和實(shí)現(xiàn)類,在另一個(gè)文件中實(shí)現(xiàn)main()函數(shù)。解: #include "my_x_y_z.h"void main() X x;Z z;z.f(&x);/ my_x_y_z.h文件#ifndef MY_X_Y_Z_Hclass X; class Y void g(X*);class X private:int i;public:X()i=0;friend void h(X*);friend void Y:g(X*);friend class Z;void h(X* x) x->i =+10;

44、void Y:g(X* x) x->i +; class Z public:void f(X* x) x->i += 5; ;#endif / MY_X_Y_Z_H程序運(yùn)行輸出:無(wú)5-14 定義Boat與Car兩個(gè)類,二者都有weight屬性,定義二者的一個(gè)友元函數(shù)totalWeight(),計(jì)算二者的重量和。解: 源程序:#include <iostream.h>class Boat;class Carprivate:int weight;public:Car(int j)weight = j;friend int totalWeight(Car &aCar,

45、 Boat &aBoat);class Boatprivate:int weight;public:Boat(int j)weight = j;friend int totalWeight(Car &aCar, Boat &aBoat);int totalWeight(Car &aCar, Boat &aBoat)return aCar.weight + aBoat.weight;void main()Car c1(4);Boat b1(5);cout << totalWeight(c1, b1) << endl;程序運(yùn)行輸出:9

46、第 六 章 數(shù)組、指針與字符串6-1 數(shù)組A10515一共有多少個(gè)元素?解: 10×5×15 = 750 個(gè)元素1-2 在數(shù)組A20中第一個(gè)元素和最后一個(gè)元素是哪一個(gè)?解: 第一個(gè)元素是A0,最后一個(gè)元素是A19。6-3 用一條語(yǔ)句定義一個(gè)有五個(gè)元素的整型數(shù)組,并依次賦予15的初值。解: 源程序:int IntegerArray5 = 1, 2, 3, 4, 5 ;或:int IntegerArray = 1, 2, 3, 4, 5 ;6-7 什么叫做指針?指針中儲(chǔ)存的地址和這個(gè)地址中的值有何區(qū)別?解: 指針是一種數(shù)據(jù)類型,具有指針類型的變量稱為指針變量。指針變量存放的是另

47、外一個(gè)對(duì)象的地址,這個(gè)地址中的值就是另一個(gè)對(duì)象的內(nèi)容。6-10 定義一個(gè)有五個(gè)元素的整型數(shù)組,在程序中提示用戶輸入元素值,最后再在屏幕上顯示出來(lái)。解: 源程序:#include <iostream.h> int main()int myArray5;int i;for ( i=0; i<5; i+) cout << "Value for myArray" << i << ": "cin >> myArrayi;for (i = 0; i<5; i+)cout << i &

48、lt;< ": " << myArrayi << "n"return 0;程序運(yùn)行輸出:Value for myArray0: 2Value for myArray1: 5Value for myArray2: 7Value for myArray3: 8Value for myArray4: 30: 21: 52: 73: 84: 36-11 引用和指針有何區(qū)別?何時(shí)只能使用指針而不能使用引用?解: 引用是一個(gè)別名,不能為NULL值,不能被重新分配;指針是一個(gè)存放地址的變量。當(dāng)需要對(duì)變量重新賦以另外的地址或賦值為NULL時(shí)

49、只能使用指針。6-12 聲明下列指針:float類型變量的指針pFloat,char類型的指針pString和struct customer型的指針prec。解: float *pfloat;char *pString;struct customer *prec;6-13 給定float類型的指針fp,寫出顯示fp所指向的值的輸出流語(yǔ)句。解: cout << "Value = " << *fp;6-16 定義一個(gè)整型變量a,一個(gè)整型指針p,一個(gè)引用r,通過p把a(bǔ)的值改為10,通過r把a(bǔ)的值改為5解: void main()int a;int *p =

50、 &a;int &r = a;*p = 10;r = 5;6-21 編寫一個(gè)函數(shù),統(tǒng)計(jì)一個(gè)英文句子中字母的個(gè)數(shù),在主程序中實(shí)現(xiàn)輸入、輸出。解: 源程序:#include <iostream.h>#include <stdio.h>int count(char *str)int i,num=0;for (i=0; stri; i+)if ( (stri>='a' && stri<='z') | (stri>='A' && stri<='Z'

51、;) )num+;return num;void main()char text100;cout << "輸入一個(gè)英語(yǔ)句子:" << endl;gets(text);cout << "這個(gè)句子里有" << count(text) << "個(gè)字母。" << endl;程序運(yùn)行輸出:輸入一個(gè)英語(yǔ)句子:It is very interesting!這個(gè)句子里有19個(gè)字母。6-22 編寫函數(shù)int index(char *s, char *t),返回字符串t 在字符串s中

52、出現(xiàn)的最左邊的位置,如果在s中沒有與t匹配的子串,就返回-1。解: 源程序:#include <iostream.h>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+);if (tk ='0')return i;return -1;void main()int n;char str120,str220;cout << "輸入一個(gè)英語(yǔ)

53、單詞:"cin >> str1;cout << "輸入另一個(gè)英語(yǔ)單詞:"cin >> str2;n = index(str1,str2);if (n > 0)cout << str2 << "在" << str1 << "中左起第" << n+1 << "個(gè)位置。"<<endl;elsecout << str2 << "不在" <&

54、lt; str1 << "中。" << endl;程序運(yùn)行輸出:輸入一個(gè)英語(yǔ)單詞:abcdefgh輸入另一個(gè)英語(yǔ)單詞:dede在abcdefghijk中左起第4個(gè)位置。 第 七 章 繼承與派生7-7 定義一個(gè)基類,構(gòu)造其派生類,在構(gòu)造函數(shù)中輸出提示信息,觀察構(gòu)造函數(shù)的執(zhí)行情況。解: #include <iostream.h>class BaseClasspublic:BaseClass();BaseClass:BaseClass()cout << "構(gòu)造基類對(duì)象!" << endl;class

55、DerivedClass : public BaseClasspublic:DerivedClass(); ;DerivedClass:DerivedClass()cout << "構(gòu)造派生類對(duì)象!" << endl;void main()DerivedClass d;程序運(yùn)行輸出:構(gòu)造基類對(duì)象!構(gòu)造派生類對(duì)象!7-8 定義一個(gè)Document類,有name成員變量,從Document派生出Book類,增加PageCount變量。解: #include <iostream.h>#include <string.h>class

56、Documentpublic: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;class Book : public Documentpublic:Book(

57、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();程序運(yùn)行輸出:Name of book: Book17-9 定義基類Base,有兩個(gè)共有成員函數(shù)fn1()、fn2(),私有派生出Derived類,如果想在Derived類的

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論