



下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、第 二 章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 和 do while語句完成同樣的循環。解:for 循環:for (int n = 100; n <= 200; n += 2);while 循環:int x = 100;while (n <= 200)n += 2;do while 循環:int n = 100;don += 2; while(n <= 2
2、00);2-17 修改下面這個程序中的錯誤,改正后它的運行結果是什么?#include <iostream.h>void main()int iint j;i = 10; /*給 i 賦值j = 20; /*給 j 賦值*/cout << "i + j = << i + j; /*輸出結果*/return 0;解:改正:#include <iostream.h>int main()int i;int j;i = 10; /給 i 賦值j = 20; /*給 j 賦值*/cout << "i + j = "
3、 << i + j; /*輸出結果*/return 0;程序運行輸出:i + j = 302-18 編寫一個程序,運行時提示輸入一個數字,再把這個數字顯示出來。解:源程序:#include <iostream.h>int main()int i;cout << "請輸入一個數字:"cin >> i;cout << "您輸入一個數字是" << i << endl;return 0;程序運行輸出:請輸入一個數字:5您輸入一個數字是52-20 打印 ASCII碼為 32127
4、的字符。解:#include <iostream.h>int main()for (int i = 32; i<128; i+)cout << (char) i;return 0;程序運行輸出:!"#$%G'()*+ , ./0123456789:;<>?ABCDEFGHIJKLMNOP_QRSTUVWXYZ'abcdefghijklmnopqrs tuvwxyz<|>s2-21 運行下面的程序,觀察其輸出,與你的設想是否相同?#include <iostream.h>int main()unsigne
5、d 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 運行下面的程序
6、,觀察其輸出,體會i+與+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 increme
7、nt+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 << &
8、quot;I am: " << 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 <<
9、"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 oldLet's print it againI am 41 years oldYou are 41 years old2
10、-28 編寫一個完整的程序,實現功能:向用戶提問" 現在正在下雨嗎?" ,提示用戶輸入Y或 N。若輸入為 Y,顯示 " 現在正在下雨。 " ; 若輸入為 N,顯示 " 現在沒有下雨。 ";否則繼續提問 "現在正在下雨嗎? "解:源程序:#include <iostream.h>#include <stdlib.h>void main()char flag;while(1)cout << "現在正在下雨嗎?(Yes or No):"cin >> fl
11、ag;if ( toupper(flag) = 'Y')cout << "現在正在下雨。"break;if ( toupper(flag) = 'N')cout << "現在沒有下雨。"break;程序運行輸出:現在正在下雨嗎?(Yes or No):x現在正在下雨嗎?(Yes or No):l現在正在下雨嗎?(Yes or No):q現在正在下雨嗎?(Yes or No):n現在沒有下雨。或:現在正在下雨嗎?(Yes or No):y現在正在下雨。2-29 編寫一個完整的程序,運行時向用戶提問&q
12、uot; 你考試考了多少分?(0100)" ,接收輸入后判斷其等級,顯示出來。規則如下:解:#include <iostream.h>void main()int i,score;cout << "你考試考了多少分?(0100):"cin >> score;if (score>100 | score<0)cout << "分數值必須在0 到 100 之間 !"elsei = score/10;switch (i)case 10:case 9:cout << "你的
13、成績為優!"break;case 8:cout << "你的成績為良!"break;case 7:case 6:cout << "你的成績為中!"break;default:cout << "你的成績為差!"程序運行輸出:你考試考了多少分?(0100): 85你的成績為良!2-31 用窮舉法找出1100 間的質數,顯示出來。分別使用while ,do-while ,for 循環語句實現。解:源程序:使用 while 循環語句:#include <iostream.h>#incl
14、ude <math.h>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 (flag)cout << i << "是質數 ." << endl;i+;使用 dowhile 循環語句:#include <iostream.h>#include <math.h>void main()int i,j,k,fla
15、g;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 <iostream.h>#include <math.h>void main()int i,j,k,flag;for(i = 2; i <= 100; i+)flag = 1;k
16、= 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 是質數 .8
17、3 是質數 .89 是質數 .97 是質數 .2-33 定義一個表示時間的結構體,可以精確表示年、月、日、小時、分、秒;提示用戶輸入年、月、日、小時、分、秒的值,然后完整地顯示出來。解:源程序見"實驗指導"部分實驗二2-34 在程序中定義一個整型變量,賦以1100小,把結果提示給用戶,直到猜對為止。分別使用的值,要求用戶猜這個數,比較兩個數的大while 、 do while 語句實現循環。解:/ 使用 while 語句#include <iostream.h>void main() int n = 18;int m = 0;while(m != n)cout
18、<< "請猜這個數的值為多少?(0100):"cin >> m;if (n > m)cout << "你猜的值太小了!" << endl;else if (n < m)cout << "你猜的值太大了!" << endl;elsecout << "你猜對了! " << endl;/ 使用 do while 語句#include <iostream.h>void main() int n = 18;
19、int m = 0;docout << "請猜這個數的值為多少?(0100):"cin >> m;if (n > m)cout << "你猜的值太小了!" << endl;else if (n < m)cout << "你猜的值太大了!" << endl;elsecout << "你猜對了! " << endl;while(n != m);程序運行輸出:請猜這個數的值為多少?(0100):50你猜的值太大了!
20、請猜這個數的值為多少?(0100):25你猜的值太大了!請猜這個數的值為多少?(0100):10你猜的值太小了!請猜這個數的值為多少?(0100):15你猜的值太小了!請猜這個數的值為多少?(0100):18你猜對了!第三章函數3-2 觀察下面程序的運行輸出,與你設想的有何不同?仔細體會引用的用法。源程序:#include <iostream.h>int main()int intOne;int &rSomeRef = intOne;intOne = 5;cout << "intOne:tt" << intOne <<
21、 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"
22、<< rSomeRef << endl;return 0;程序運行輸出:intOne: 5rSomeRef: 5intOne: 8intTwo: 8rSomeRef: 83-7 編寫函數,參數為兩個 unsigned short int 型數,返回值為第一個參數除以第二個參數的結果,數據類型為 short int ;如果第二個參數為 0,則返回值為 -1。在主程序中實現輸入輸出。解:源程序:#include <iostream.h>short int Divider(unsigned short int a, unsigned short int b)if
23、(b = 0)return -1;elsereturn a/b;typedef unsigned short int USHORT;typedef unsigned 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
24、 (answer > -1)cout << "Answer: " << answer;elsecout << "Error, can't divide by zero!"return 0;程序運行輸出:Enter two numbers.Number one:8Number two:2Answer: 43-8 編寫函數把華氏溫度轉換為攝氏溫度,公式為: C = (F - 32) * 5/9; 在主程序中提示用戶輸入一個華氏溫度,轉化后輸出相應的攝氏溫度。解:源程序見 "實驗指導 "部分
25、實驗三3-10 編寫函數求兩個整數的最大公約數和最小公倍數。解:源程序:#include <iostream.h>#include <math.h>int fn1(int i,int j); /求最大公約數的函數void main()int i,j,x,y;cout << "請輸入一個正整數:"cin >> i ;cout << "請輸入另一個正整數:"cin >> j ;x = fn1(i,j);y = i * j / x;cout << i << &quo
26、t;和 " << j << "的最大公約數是:" << x << endl;cout << i << "和 " << j << "的最小公倍數是:" << 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;程序運
27、行輸出:請輸入一個正整數:120請輸入另一個正整數:72120 和 72 的最大公約數是:120 和 72 的最小公倍數是:3-12 在主程序中提示輸入整數24360n,編寫函數用遞歸的方法求1+2+ n 的值。解:#include <iostream.h>#include <math.h>int fn1(int i);void main()int i;cout << "請輸入一個正整數:cin >> i ;"cout << "從 1 累加到 " <<i << "
28、;的和為: " << fn1(i) << endl;int fn1(int i)if (i = 1)return 1;elsereturn i + fn1(i -1);程序運行輸出:請輸入一個正整數:100從 1 累加到 100 的和為: 50503-14 用遞歸的方法編寫函數求Fibonaccifib(1) = fib(2) = 1; 觀察遞歸調用的過程。級數,公式為fib(n) = fib(n-1) + fib(n-2),n>2;解:源程序見 "實驗指導 "部分實驗三3-15 用遞歸的方法編寫函數求n 階勒讓德多項式的值,在主程序
29、中實現輸入、輸出;解:#include <iostream.h>float p(int n, int x);void main()int n,x;cout << "請輸入正整數n: "cin >> n;cout << "請輸入正整數x: "cin >> x;cout << "n = " << n << endl;cout << "x = " << x << endl;cout <
30、< "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 ;程序運行輸出:請輸入正整數n: 1請輸入正整數x: 2n = 1x = 2P1(2) = 2請輸入正整數請輸入正整數n: 3x:
31、4n = 3x = 4P3(4) = 1544-9第 四 章設計并測試一個名為類Rectangle的矩形類,其屬性為矩形的左下角與右上角兩個點的坐標,能計算矩形的面積。解:源程序:#include <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 ret
32、urn 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 itsRight;Re
33、ctangle: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 Area = M
34、yRectangle.GetArea();cout << "Area: " << Area << "n"return 0;程序運行輸出:Area: 3000Upper Left X Coordinate: 204-11 定義一個矩形類,有長、寬兩個屬性,有成員函數計算矩形的面積解:#include <iostream.h>class Rectanglepublic:Rectangle(float len, float width)Length = len;Width = width;Rectangle();
35、float GetArea() return Length * Width; float GetLength() return Length; float GetWidth() return Width; private:float Length;float Width;void main()float length, width;cout << "請輸入矩形的長度:"cin >> length;cout << "請輸入矩形的寬度:"cin >> width;Rectangle r(length, widt
36、h);cout << "長為 " << length << " 寬為 " << width << " 的矩形的面積為:"<< r.GetArea () << endl;程序運行輸出:請輸入矩形的長度:5請輸入矩形的寬度:4長為 5 寬為 4 的矩形的面積為:204-12 定義一個 " 數據類型 " datatype 類,能處理包含字符型、整型、浮點型三種類型的數據,給出其構造函數。解:#include <iostream.h&
37、gt;class datatypeenumcharacter,integer,floating_point vartype; unionchar 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) case char
38、acter: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
39、();C.print();程序運行輸出:字符型 : c整型:12浮點型 : 1.444-13 定義一個 Circle 類,有數據成員半徑 Radius,成員函數 GetArea(),計算圓的面積,構造一個 Circle 的對象進行測試。解:#include <iostream.h>class Circlepublic:Circle(float radius) Radius = radius;Circle()float GetArea() return 3.14 * Radius * Radius; private:float Radius;void main()float radi
40、us;cout << "請輸入圓的半徑:"cin >> radius;Circle p(radius);cout << "半徑為 " << radius << " 的圓的面積為:" << p.GetArea ()<< endl;程序運行輸出:請輸入圓的半徑:5半徑為 5 的圓的面積為:78.54-14 定義一個tree 類,有成員 ages,成員函數grow(int years) 對 ages 加上 years,age()顯示 tree對象的 ages
41、的值。解:#include <iostream.h>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.ag
42、e();t.grow(4);程序運行輸出:這棵樹的年齡為12這棵樹的年齡為16第 五 章C+程序的基本結構5-12 在函數 fn1() 中定義一個靜態變量n, fn1() 中對 n 的值加 1,在主函數中,調用fn1() 十次,顯示n 的值。解:#include <iostream.h>void fn1()static int n = 0;n+;cout << "n 的值為 " << n <<endl;void main()for(int i = 0; i < 10; i+)fn1();程序運行輸出:n 的值為 1n 的
43、值為 2n 的值為 3n 的值為 4n 的值為 5n 的值為 6n 的值為 7n 的值為 8n 的值為 9n 的值為 105-13 定義類 X、Y、Z,函數 h(X*) ,滿足:類 X 有私有成員i ,Y 的成員函數g(X*)是 X 的友元函數,實現對X 的成員i 加1,類Z 是類X 的友元類,其成員函數f(X*) 實現對X 的成員 i 加5,函數 h(X*) 是一個文件中實現解:X 的友元函數,實現對main()函數。X 的成員i 加10。在一個文件中定義和實現類,在另#include "my_x_y_z.h"void main()X x;Z z;z.f(&x);
44、/ my_x_y_z.h 文件#ifndef MY_X_Y_Z_Hclass X;class Y void g(X*);class Xprivate: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; 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 兩個類,二者都有 w
45、eight 屬性,定義二者的一個友元函數totalWeight() ,計算二者的重量和。解:源程序:#include <iostream.h>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 , Boa
46、t &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;程序運行輸出:9第 六 章6-1 數組A10515數組、指針與字符串一共有多少個元素?解:10× 5× 15 = 750 個元素1-2 在數組 A20 中第一個元素和最后一個元素是哪一個?解:第一個元素是A0 ,最后一個元素是
47、A19 。6-3 用一條語句定義一個有五個元素的整型數組,并依次賦予15 的初值。解:源程序:int IntegerArray5 = 1 , 2, 3, 4, 5 ;或: int IntegerArray = 1 , 2, 3, 4, 5 ;6-7 什么叫做指針?指針中儲存的地址和這個地址中的值有何區別?解:指針是一種數據類型, 具有指針類型的變量稱為指針變量。 指針變量存放的是另外一個對象的地址,這個地址中的值就是另一個對象的內容。6-10 定義一個有五個元素的整型數組,在程序中提示用戶輸入元素值,最后再在屏幕上顯示出來。解:源程序:#include <iostream.h>in
48、t 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 << ": " << myArrayi << "n"return 0;程序運行輸出:Value for myArray0: 2Value for m
49、yArray1: 5Value for myArray2: 7Value for myArray3: 8Value for myArray4: 30: 21: 52: 73: 84: 36-11引用和指針有何區別?何時只能使用指針而不能使用引用?解:引用是一個別名,不能為NULL 值,不能被重新分配;指針是一個存放地址的變量。當需要對變量重新賦以另外的地址或賦值為NULL 時只能使用指針。6-12聲明下列指針: float 類型變量的指針pFloat,char 類型的指針 pString 和 struct customer型的指針 prec。解:float *pfloat;char *pStr
50、ing;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 編寫一個函數,統計一個英文句子中字母的個數,在主程序中實現輸入、輸出。解:源程序:#include <
51、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') )num+;return num;void main()char text100;cout << "輸入一個英語句子:" << endl;get
52、s(text);cout << "這個句子里有" << count(text) << " 個字母。 " << endl;程序運行輸出:輸入一個英語句子:It is very interesting!這個句子里有19 個字母。6-22 編寫函數 int index(char *s , char *t) ,返回字符串 t 在字符串 s 中出現的最左邊的位置,如果在 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 << "輸入一個英語單詞:"cin
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 北師大版七年級數學下冊《2.1兩條直線的位置關系》同步測試題及答案
- 政策環境變化下的戰略與風險考核試題及答案
- 企業聲譽風險管理與戰略決策試題及答案
- 2025年金融軟件的技術要求試題及答案
- 博物館展品安全管理措施計劃
- 數據通信基礎知識考題及答案
- 班級共同體意識的培養計劃
- 主題班會活動的設計與實施計劃
- 完善工業企業安全生產計劃
- 山東省萊城區劉仲瑩中學2025年七年級數學第二學期期末達標檢測模擬試題含解析
- 七年級下學期語文5月月考試卷
- 2024年樂山市市級事業單位選調工作人員真題
- 2025年下半年湘潭市技師學院招考人員易考易錯模擬試題(共500題)試卷后附參考答案
- 舞臺劇合作合同協議
- 初級qc考試題及答案
- 影視文化試題及答案解析
- 浙江開放大學2025年《行政復議法》形考作業3答案
- 施工現場安全施工方案
- 中醫適宜技術-中藥熱奄包
- 材料力學第4版單輝祖習題答案
- 解除收養關系登記申請書
評論
0/150
提交評論