C++ Primer Plus章节编程练习(第五章)
第五章 循環和關系表達式
1、編寫一個要求用戶輸入兩個整數的程序。該程序將計算并輸出這兩個整數之間(包括這兩個整數)所有整數的和。這里假設先輸入較小的整數。例如,如果用戶輸入的是2和9,則程序將指出2~9之間所有整數的和為44。
??分析:簡單的for循環
1 #include<iostream>2 using namespace std;3 int main(){4 int min, max;5 cout << "Enter first number: ";6 cin >> min;7 cout << "Enter second number: "; 8 cin >> max; 9 int i, s=0; 10 for(i=min; i<=max; i++){ 11 s += i; 12 } 13 cout << s; 14 }2、 使用array對象(而不是數組)和long double(而不是long long)重新編寫程序清單5.4,并計算100!的值。
? ? ?分析:簡單的for循環
1 #include "iostream"2 #include "array"3 using namespace std;4 int main()5 {6 array<long double, 100> myArray;7 myArray[0] = 1;8 for (int i = 1; i < 100; i++) { 9 myArray[i] = (i + 1)*myArray[i - 1]; 10 cout << i + 1 << "! = " << myArray[i]; 11 cout << endl; 12 } 13 return 0; 14 }3、編寫一個要求用戶輸入數字的程序。每次輸入后,程序都將報告到目前為止,所有的輸入累計和。當用戶輸入0時,程序結束?
? ??分析:簡單的while循環
1 #include<iostream>2 using namespace std;3 int main(){4 int number; //輸入5 int total = 0; //總和 6 cin >> number;7 while(number!=0){8 total += number;9 cout << "total: " << total << endl; 10 cin >> number; 11 } 12 }4、 Daphne以10%的單利投資了100美元。也就是說,每一年的利潤都是投資額的10%(利息=0.10*原始存款)。而Cleo以5%的復利投資了100美元。也就是說,利息是當前存款(包括獲得的利息)的5%(利息=0.05*當前存款)。請計算多少年后,Cleo的投資價值才能超過Daphne的投資價值,并顯示此時兩人的投資價值。
? ??分析:do...while的簡單應用
1 #include<iostream>2 using namespace std;3 int main(){4 double d=100, c=100;5 int count = 0;6 do{7 d += 10.0; 8 c *= 1.05; 9 count++; 10 }while(d>c); 11 cout << "after " << count << " years "; 12 cout << "Cleo pass Daphne" <<endl; 13 cout << "Cleo: " << c <<endl; 14 cout << "Daphne: " << d <<endl; 15 }5、 假設要銷售《C++ For Fools》一書,請編寫一個程序,輸入全年中每一年的銷售量(圖書數量,而不是銷售額)。程序通過循環,使用初始化為月份字符串的char * 數組(或string對象數組)逐月進行提示,并將輸入的數據存儲在一個int數組中。然后,程序計算數組中各元素的總和,并報告這一年的銷售情況。
???分析:for循環的簡單運用
1 #include<iostream>2 #include<string>3 using namespace std;4 int main(){5 string months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};6 int sales[12];7 int count = 0;8 cout << "Enter sales per month" << endl; 9 for(int i=0; i<12; i++){ 10 cout << months[i] << ": "; 11 cin >> sales[i]; 12 } 13 for(int i=0; i<12; i++) 14 count += sales[i]; 15 cout << "The total sales of this year is " << count; 16 }6、 完成編程練習5,但這一次使用一個二維數組來存儲輸入——3年中每個月的銷售量,程序將報告每年銷售量以及三年的總銷售量。
? ?分析:for循環的簡單運用
1 #include<iostream>2 #include<string>3 using namespace std;4 int main(){5 string months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};6 int sales[3][12];7 int total1 = 0, total2 = 0, total3 = 0; //每年總銷量 8 cout << "Enter sales per month" << endl;9 for(int i=0; i<3; i++){ 10 for(int j=0; j<12; j++){ 11 cout << i+1 << " year " << months[j] << ": "; 12 cin >> sales[i][j]; 13 if(i==0) 14 total1 += sales[i][j]; 15 if(i==1) 16 total2 += sales[i][j]; 17 if(i==2) 18 total3 += sales[i][j]; 19 } 20 cout << endl; 21 } 22 cout << "first year: " << total1 << endl; 23 cout << "second year: " << total2 << endl; 24 cout << "third year: " << total3 << endl; 25 cout << "total: " << total1 + total2 + total3; 26 }7、設計一個名為car的結構,用它存儲下述有關汽車的信息:生產商(存儲在字符數組或string對象中的字符串)、生產年份(整數)。編寫一個程序,向用戶詢問有多少輛汽車。隨后,程序使用new來創建一個由相應數量的car結構組成的動態數組。接下來,程序提示用戶輸入每輛車的生產商(可能有多個單詞組成)和年份信息。請注意,這需要特別小心,因為它將交替讀取數值和字符串。最后,程序將顯示每個結構的內容。改程序的運行情況如下:
? How many cars do you wish to catalog??2
? Car #1:
? Please enter the make:?Hudson Hornet
? Please enter the year made:?1952
? Car #2:
???Please enter the make:?Kaiser
? ?Please enter the year made:?1951
? ?Here is your collection:
? ?1952 Hudson Hornet
? ?1951 Kaiser
???分析:綜合第四章知識
1 #include<iostream>2 #include<string>3 using namespace std;4 struct car{5 string make; //生產廠商 6 int year; //生產年份 7 };8 int main(){9 int n; 10 cout << "How many cars do yuou wish to catalog? "; 11 cin >> n; 12 cin.ignore(); //防止使用getline()函數時出現無法輸入的bug 13 car * cars = new car[n]; //動態創建結構體數組 14 for(int i=0; i<n; i++){ 15 cout << "Car #" << i+1 <<endl; 16 cout << "Please enter the make: "; 17 getline(cin, cars[i].make); 18 cout << "Please enter the year made: "; 19 cin >> cars[i].year; 20 cin.ignore(); 21 } 22 cout << "Here is your collection:" << endl; 23 for(int i=0; i<n; i++){ 24 cout << cars[i].year << " " << cars[i].make <<endl; 25 } 26 }? ?cin在getline(cin, str)之前使用時要在cin后加cin.ignore()來解決getline(cin, str)讀取回車的問題
8*、編寫一個程序,他使用一個char數組和循環來每次讀取一個單詞,直到用戶輸入done為止。隨后,該程序指出用戶輸入了多少單詞(不包括done在內)。下面是該程序的運行情況:
??Enter words (to stop, type the word done):
? anteater birthday category dumpster
? envy finagle geometry done for sure
? You enter a total of 7words.
? 你應在程序中包含頭文件cstring,并使用strcmp( )來進行比較測試。
1 #include<iostream>2 #include<cstring>3 using namespace std;4 int main()5 {6 char word[20];7 int num = 0;8 cout << "Enter words (to stop, type the word done):" << endl; 9 cin >> word; 10 while(strcmp(word , "done") != 0) 11 { 12 if(bool(cin >> word) == true) 13 { 14 num++; 15 } 16 } 17 cout << "You entered a total of " << num << " words." << endl; 18 return 0; 19 }9*、編寫一個滿足前一個練習中描述的程序,但使用string對象而不是字符數組。請在程序中包含頭文件string,并使用關系運算符來進行比較測試。
1 #include<iostream>2 #include<string>3 using namespace std;4 int main(){5 string word;6 int num = 0;7 cout << "Enter words (to stop, type the word done):" << endl;8 cin >> word; 9 while(word!="done") 10 { 11 if(bool(cin >> word) == true) 12 { 13 num++; 14 } 15 } 16 cout << "You entered a total of " << num << " words." << endl; 17 return 0; 18 }10、編寫一個使用循環嵌套的程序,要求用戶輸入一個值,指出要顯示多少行。然后,程序將顯示相應行數的星號,其中第一行包含一個星號,第二行包含兩個星號,依次類推。每一行包含的字符數等于用戶指定的行數,在星號不夠的情況下,在星號前面加上句點。
1 #include<iostream>2 using namespace std;3 int main(){4 int n;5 cin >> n;6 for(int i=0; i<n; i++){7 for(int j=0; j<n-i-1; j++) 8 cout << "."; 9 for(int j=0; j<i+1; j++) 10 cout << "*"; 11 cout << endl; 12 } 13 }轉載于:https://www.cnblogs.com/SChenqi/p/9662982.html
總結
以上是生活随笔為你收集整理的C++ Primer Plus章节编程练习(第五章)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android 美团Robust热更新
- 下一篇: rsync+shell脚本完成自动化备份