C++語言程序設(shè)計課后答案_第1頁
C++語言程序設(shè)計課后答案_第2頁
C++語言程序設(shè)計課后答案_第3頁
C++語言程序設(shè)計課后答案_第4頁
C++語言程序設(shè)計課后答案_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、第 二 章 C+簡單程序設(shè)計2-10 執(zhí)行完下列語句后,a、b、c三個變量的值為多少?a = 30;b = a+;c = +a;a:32 ; b:30 ; c:32;2-13 寫一條for語句,計數(shù)條件為n從100到200,步長為2;然后用while和dowhile語句完成同樣的循環(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、的運行結(jié)果是什么?#include void main()int iint j;i = 10; /* 給i賦值j = 20; /* 給j賦值 */cout i + j = i + j; /* 輸出結(jié)果 */return 0;解: 改正:#include int main()int i;int j;i = 10; / 給i賦值j = 20; /* 給j賦值 */cout i + j = i + j; /* 輸出結(jié)果 */return 0;程序運行輸出:i + j = 302-18 編寫一個程序,運行時提示輸入一個數(shù)字,再把這個數(shù)字顯示出來。解: 源程序:#include int main()in

3、t i;cout i;cout 您輸入一個數(shù)字是 i endl;return 0;程序運行輸出:2-20 打印ASCII碼為32127的字符。#include int main()for (int i = 32; i128; i+)cout (char) i;return 0;程序運行輸出:!#$%G()*+,./0123456789:;?ABCDEFGHIJKLMNOP_QRSTUVWXYZabcdefghijklmnopqrstuvwxyzs2-21 運行下面的程序,觀察其輸出,與你的設(shè)想是否相同?#include int main()unsigned int x;unsigned int

4、 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的數(shù)據(jù)類型。2-22 運行下面的程序,觀察其輸出,體會i+與+i的差別。#include int main()int myAge = 39; / initialize two integersint yourAge = 39;

5、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;cout You are: +yourAge years oldn;cout Let

6、s 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 againI am 41 years oldYou are 41 years ol

7、d2-28 編寫一個完整的程序,實現(xiàn)功能:向用戶提問現(xiàn)在正在下雨嗎?,提示用戶輸入Y或N。若輸入為Y,顯示現(xiàn)在正在下雨。; 若輸入為N,顯示現(xiàn)在沒有下雨。;否則繼續(xù)提問現(xiàn)在正在下雨嗎?源程序:#include #include void main()char flag;while(1)cout flag;if ( toupper(flag) = Y)cout 現(xiàn)在正在下雨。;break;if ( toupper(flag) = N)cout 現(xiàn)在沒有下雨。;break;程序運行輸出:現(xiàn)在正在下雨嗎?(Yes or No):x現(xiàn)在正在下雨嗎?(Yes or No):l現(xiàn)在正在下雨嗎?(Yes o

8、r No):q現(xiàn)在正在下雨嗎?(Yes or No):n現(xiàn)在沒有下雨?;颍含F(xiàn)在正在下雨嗎?(Yes or No):y現(xiàn)在正在下雨。2-29 編寫一個完整的程序,運行時向用戶提問你考試考了多少分?(0100),接收輸入后判斷其等級,顯示出來。規(guī)則如下:解: #include void main()int i,score;cout score;if (score100 | score0)cout 分數(shù)值必須在0到100之間!;elsei = score/10;switch (i)case 10:case 9:cout 你的成績?yōu)閮?yōu)!;break;case 8:cout 你的成績?yōu)榱迹?break;

9、case 7:case 6:cout 你的成績?yōu)橹校?break;default:cout 你的成績?yōu)椴睿? 程序運行輸出:你考試考了多少分?(0100):85你的成績?yōu)榱迹?-31 用窮舉法找出1100間的質(zhì)數(shù),顯示出來。分別使用while,do-while,for循環(huán)語句實現(xiàn)。解: 源程序: 使用while循環(huán)語句:#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 (fl

10、ag)cout i 是質(zhì)數(shù). endl;i+; 使用dowhile循環(huán)語句:#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 是質(zhì)數(shù). endl;i+;while(i = 100); 使用for循環(huán)語句:#include #include void main()int i,j,k,flag;for(i = 2; i = 100; i+)flag = 1;k = sqr

11、t(i);for (j = 2; j = k; j+)if(i%j = 0)flag = 0;break;if (flag)cout i 是質(zhì)數(shù). endl;程序運行輸出: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 定義一個表示時間的結(jié)構(gòu)體,可以精確表示年、月、日、小時、分、秒;提示用戶輸入年、月、日、小時、分、秒的值,然后完整地顯示出來。

12、 解: 源程序見實驗指導部分實驗二2-34 在程序中定義一個整型變量,賦以1100的值,要求用戶猜這個數(shù),比較兩個數(shù)的大小,把結(jié)果提示給用戶,直到猜對為止。分別使用while、dowhile語句實現(xiàn)循環(huán)。解: /使用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;/使用dowhile語句#include void main() int n = 18

13、;int m = 0;docout m;if (n m)cout 你猜的值太小了! endl;else if (n m)cout 你猜的值太大了! endl;elsecout 你猜對了! endl;while(n != m); 程序運行輸出:請猜這個數(shù)的值為多少?(0100):50你猜的值太大了!請猜這個數(shù)的值為多少?(0100):25你猜的值太大了! 請猜這個數(shù)的值為多少?(0100):10你猜的值太小了!請猜這個數(shù)的值為多少?(0100):15你猜的值太小了!請猜這個數(shù)的值為多少?(0100):18你猜對了! 第三章 函數(shù)3-2 觀察下面程序的運行輸出,與你設(shè)想的有何不同?仔細體會引用的用

14、法。源程序:#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 rSomeRef endl;return 0;程序運行輸出:intOne: 5rSomeRef

15、: 5intOne: 8intTwo: 8rSomeRef: 83-7 編寫函數(shù),參數(shù)為兩個unsigned short int型數(shù),返回值為第一個參數(shù)除以第二個參數(shù)的結(jié)果,數(shù)據(jù)類型為short int;如果第二個參數(shù)為0,則返回值為-1。在主程序中實現(xiàn)輸入輸出。解: 源程序:#include 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 long

16、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 編寫函數(shù)把華氏溫度轉(zhuǎn)換為攝氏溫度,公式為:C = (F - 32) * 5/9; 在主程序中提示用戶輸入一個華氏溫度,轉(zhuǎn)

17、化后輸出相應(yīng)的攝氏溫度。解: 源程序見實驗指導部分實驗三3-10 編寫函數(shù)求兩個整數(shù)的最大公約數(shù)和最小公倍數(shù)。源程序:#include #include int fn1(int i,int j); /求最大公約數(shù)的函數(shù)void main()int i,j,x,y;cout i ;cout j ;x = fn1(i,j);y = i * j / x;cout i 和 j 的最大公約數(shù)是: x endl;cout 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

18、)temp = i % j;i = j;j = temp;return i;程序運行輸出:請輸入一個正整數(shù):120請輸入另一個正整數(shù):72120和72的最大公約數(shù)是:24120和72的最小公倍數(shù)是:3603-12 在主程序中提示輸入整數(shù)n,編寫函數(shù)用遞歸的方法求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;觀察遞歸調(diào)用的過程。解: 源程序見實驗指導部分實驗三3-15 用遞歸的方法編寫函數(shù)求n階勒讓

19、德多項式的值,在主程序中實現(xià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-2,x) /n ;程序運行輸出:請輸入正整數(shù)n:1請輸入正整數(shù)x:2n = 1x = 2P1

20、(2) = 2請輸入正整數(shù)n:3請輸入正整數(shù)x:4n = 3x = 4P3(4) = 154 第 四 章 類4-9 設(shè)計并測試一個名為Rectangle的矩形類,其屬性為矩形的左下角與右上角兩個點的坐標,能計算矩形的面積。源程序:#include class Rectanglepublic:Rectangle (int top, int left, int bottom, int right);Rectangle () int GetTop() const return itsTop; int GetLeft() const return itsLeft; int GetBottom() co

21、nst 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;private:int itsTop;int itsLeft;int itsBottom;int itsR

22、ight;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 main()Rectangle MyRectangle (100, 20, 50, 80 );int A

23、rea = MyRectangle.GetArea();cout Area: Area n;return 0;程序運行輸出:Area: 3000Upper Left X Coordinate: 204-11 定義一個矩形類,有長、寬兩個屬性,有成員函數(shù)計算矩形的面積 解: #include class Rectanglepublic:Rectangle(float len, float width)Length = len;Width = width;Rectangle();float GetArea() return Length * Width; float GetLength() ret

24、urn 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寬為4的矩形的面積為:204-12 定義一個數(shù)據(jù)類型 datatype類,能處理包含字符型、整型、浮點型三種類型的數(shù)據(jù),給出

25、其構(gòu)造函數(shù)。解: #include class datatypeenumcharacter,integer,floating_point 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) vartype = floating_point;f = ff;void print();void datatype:print() switch (vartype)

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

27、class Circlepublic:Circle(float radius) Radius = radius;Circle()float GetArea() return 3.14 * Radius * Radius; private:float Radius;void main()float radius;cout radius;Circle p(radius);cout 半徑為 radius 的圓的面積為: p.GetArea () endl;程序運行輸出:請輸入圓的半徑:5半徑為5的圓的面積為:78.54-14 定義一個tree類,有成員ages,成員函數(shù)grow(int years)

28、對ages加上years,age()顯示tree對象的ages的值。解: #include class Tree int ages;public:Tree(int n=0);Tree();void grow(int years);void age();Tree:Tree(int n) 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);程序運行輸出:這棵樹的

29、年齡為12這棵樹的年齡為16第 五 章 C+程序的基本結(jié)構(gòu) 5-12 在函數(shù)fn1()中定義一個靜態(tài)變量n,fn1()中對n的值加1,在主函數(shù)中,調(diào)用fn1()十次,顯示n的值。解: #include void fn1()static int n = 0;n+;cout n的值為 n endl;void main()for(int i = 0; i i =+10; void Y:g(X* x) x-i +; class Z public:void f(X* x) x-i += 5; ;#endif / MY_X_Y_Z_H程序運行輸出:無5-14 定義Boat與Car兩個類,二者都有weigh

30、t屬性,定義二者的一個友元函數(shù)totalWeight(),計算二者的重量和。解: 源程序:#include class Boat;class Carprivate:int weight;public:Car(int j)weight = j;friend int totalWeight(Car &aCar, Boat &aBoat);class Boatprivate:int weight;public:Boat(int j)weight = j;friend int totalWeight(Car &aCar, Boat &aBoat);int totalWeight(Car &aCar,

31、Boat &aBoat)return aCar.weight + aBoat.weight;void main()Car c1(4);Boat b1(5);cout totalWeight(c1, b1) endl;程序運行輸出:9第 六 章 數(shù)組、指針與字符串6-1 數(shù)組A10515一共有多少個元素?解: 10515 = 750 個元素1-2 在數(shù)組A20中第一個元素和最后一個元素是哪一個?解: 第一個元素是A0,最后一個元素是A19。6-3 用一條語句定義一個有五個元素的整型數(shù)組,并依次賦予15的初值。解: 源程序:int IntegerArray5 = 1, 2, 3, 4, 5 ;或:

32、int IntegerArray = 1, 2, 3, 4, 5 ;6-7 什么叫做指針?指針中儲存的地址和這個地址中的值有何區(qū)別?解: 指針是一種數(shù)據(jù)類型,具有指針類型的變量稱為指針變量。指針變量存放的是另外一個對象的地址,這個地址中的值就是另一個對象的內(nèi)容。6-10 定義一個有五個元素的整型數(shù)組,在程序中提示用戶輸入元素值,最后再在屏幕上顯示出來。解: 源程序:#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

33、 : myArrayi n;return 0;程序運行輸出:Value for myArray0: 2Value for myArray1: 5Value for myArray2: 7Value for myArray3: 8Value for myArray4: 30: 21: 52: 73: 84: 36-11 引用和指針有何區(qū)別?何時只能使用指針而不能使用引用?解: 引用是一個別名,不能為NULL值,不能被重新分配;指針是一個存放地址的變量。當需要對變量重新賦以另外的地址或賦值為NULL時只能使用指針。6-12 聲明下列指針:float類型變量的指針pFloat,char類型的指針pS

34、tring和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 = &a;int &r = a;*p = 10;r = 5;6-21 編寫一個函數(shù),統(tǒng)計一個英文句子中字母的個數(shù),在主程序中實現(xiàn)輸入、輸出。解: 源程序:#inc

35、lude #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 is very interesting!這個句子里有19個字母。6-22 編寫函數(shù)int index(char *s, char *t),返回字符串t 在字

36、符串s中出現(xiàn)的最左邊的位置,如果在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+);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 在 str1 中左起第 n+1 個位置

37、。endl;elsecout str2 不在 str1 中。 endl;程序運行輸出:輸入一個英語單詞:abcdefgh輸入另一個英語單詞:dede在abcdefghijk中左起第4個位置。 第 七 章 繼承與派生7-7 定義一個基類,構(gòu)造其派生類,在構(gòu)造函數(shù)中輸出提示信息,觀察構(gòu)造函數(shù)的執(zhí)行情況。解: #include class BaseClasspublic:BaseClass();BaseClass:BaseClass()cout 構(gòu)造基類對象! endl;class DerivedClass : public BaseClasspublic:DerivedClass(); ;Deri

38、vedClass:DerivedClass()cout 構(gòu)造派生類對象! endl;void main()DerivedClass d;程序運行輸出:構(gòu)造基類對象!構(gòu)造派生類對象!7-8 定義一個Document類,有name成員變量,從Document派生出Book類,增加PageCount變量。解: #include #include class Documentpublic:Document();Document( char *name );char *Name; / Document name.void PrintNameOf(); / Print name.;Document:Doc

39、ument( 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 = page

40、count;void Book:PrintNameOf()cout Name of book: ;Document:PrintNameOf();void main()Document a(Document1);Book b(Book1,100);b.PrintNameOf();程序運行輸出:Name of book: Book17-9 定義基類Base,有兩個共有成員函數(shù)fn1()、fn2(),私有派生出Derived類,如果想在Derived類的對象中使用基類函數(shù)fn1(),應(yīng)怎么辦?解: class Base public:int fn1() const return 1; int fn2() const return 2; ;clas

溫馨提示

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

評論

0/150

提交評論