第五章内容參考上一篇的技術點分析:
[C Primer plus 心得]5.循環關系表達式
1.編寫一個要求用戶輸入兩個整數的程序,該程序将計算并輸出這兩個整數之間(包括這兩個整數)所有的整數的和。這裡假設先輸入較小的整數,例如如果用戶輸入的是2和9,則程序将指出2-9之間所有整數的和為44.
答案:
#include <iostream> using namespace std; int main(void) { int first = 0; int second = 0; int sum = 0; cout << "Please Enter two integer number:\n"; cin >> first; cin >> second; for (int i = first; i <= second; i ) { sum = i; } cout << "The sum value is " << sum <<endl; return 0; }
2.使用array對象(而不是數組)和long double(而不是long long )重新編寫程序清單5.4,并計算100!(100的階乘)的值答案:
#include <iostream> #include <array> using namespace std; const int ArSize = 101; int main() { array<long double, ArSize> arr; arr[0] = 1; arr[1] = 1; for (int i = 2; i < ArSize; i ){ arr[i] = i*arr[i - 1]; } for (int i = 0; i < ArSize; i ){ cout << i << "! = " << arr[i] << endl; } return 0; }
3.編寫一個要求用戶輸入數字的程序,每次輸入後,程序都将報告到目前為止,所有輸入的累積和,當用戶輸入0時,程序結束。答案:
#include <iostream> using namespace std; int main() { int input = 0; int sum = 0; cout << "Please Enter integer number:\n"; cin >> input; while (input != 0) { sum = input; cout << "The total input value is " << sum <<endl; cin >> input; } return 0; }
4.Daphne以10%的單利投資了100美元,每一年利潤都是投資額的10%,即每年10美元。而Cleo以5%的複利投資了100美元,利息是當前存款(包括獲得的利息)的5%,Cleo在第一年投資100美元的盈利是5%得到了105美元,下一年的盈利是105美元的5%,以此類推編寫一個程序,計算多少年後,Cleo的投資價值才能超過Daphne的投資價值,并顯示兩個人的投資價值。
答案:
#include <iostream> using namespace std; int main() { double daphne_value = 100; double cleo_value = 100; int count = 0; while (cleo_value <= daphne_value) { daphne_value = 100*0.1; cleo_value = daphne_value * 0.05; count ; } cout << "count is "<< count <<endl; cout << "daphne's value is " << daphne_value <<endl; cout << "Cleo's value is " << cleo_value <<endl; return 0; }
5.假設要銷售《c for fool》一書。請編寫一個程序,輸入全年中每個月的銷售量(圖書數量而不是銷售額).程序通過循環,使用初始化為月份字符串的char*數組(或string對象數組) 逐月進行提示,并将輸入的數據存儲在一個int數組中,然後程序計算數組中各元素的總數,并報告這一年的銷售情況。
答案:
#include <iostream> #include <array> using namespace std; int main() { int arry_size = 12; int total = 0; int sales[arry_size] = {0}; string month[arry_size]={" Jan ", " Feb "," Mar "," Apr "," May "," Jun "," Jul "," Aug "," Sept "," Oct "," Nov "," Dec "}; for (int i =0; i< arry_size; i ) { cout<<"Please enter the sales of"<<month[i]<<"is: "; cin>>sales[i]; total = sales[i]; } for(int i = 0; i < arry_size; i ) { cout<<"The sales of "<<month[i]<<" is: "<<sales[i]<<endl; } cout<<"The sales of a year is "<<total<<endl; return 0; }
6.完成編程練習5, 但這一次使用一個二維數組來存儲輸入---3年中每個月的銷售量。程序将報告每年銷售量以及三年的總銷售量。答案:
#include <iostream> using namespace std; int main() { int arry_size = 12; int year_size = 3; int total[year_size] = {0}; int sales[year_size][arry_size] = {0}; string month[arry_size]={" Jan ", " Feb "," Mar "," Apr "," May "," Jun "," Jul "," Aug "," Sept "," Oct "," Nov "," Dec "}; for (int i = 0; i< year_size; i ) { for (int j = 0; j < arry_size; j ) { cout<<"Please enter the sales of"<<month[i]<<"in: " << (i 1) << " years is :"; cin>>sales[i][j]; total[i] = sales[i][j]; } } for(int i = 0; i < arry_size; i ) { cout<<"The sales of "<<(i 1)<<" year is: "<<total[i]<<endl; } cout<<"The sales of three year is "<<(total[0] total[1] total[2])<<endl; return 0; }
7.設計一個名為car的結構,用它存儲下述有關汽車的信息: 生産商(存儲在字符數組或string對象中的字符串)、 生産年份(整數)。編寫一個程序,向用戶詢問有多少輛汽車。随後,程序使用new來創建一個由相應數量的car結構組成的動态數組。 接下來,程序提示用戶輸入每輛車的生産商(可能由多個單詞組成)和年份信息。請注意,這需要特别小心,因為它将交替讀取 數值和字符串(參見第4章)。最後,程序将顯示每個結構的内容。
答案:
#include <iostream> using namespace std; struct car{ string manufacturer; int year; }; int main() { int count = 0; car *car_info =NULL; cout << "How many cars do you wish to catalog?\n"; cin >> count; car_info = new car[count]; for (int i = 0; i < count; i ) { cout << "Please enter the manufacturer of " << (i 1) << " car:"; cin >> car_info[i].manufacturer; cout << "Please enter the year of " << (i 1) << " car:"; cin >> car_info[i].year; } cout<<"Here is your collection:"<<endl; for (int i = 0; i < count; i ) { cout << "The "<< (i 1) << " car's manufacturer is "<<car_info[i].manufacturer; cout << " and the year is "<<car_info[i].year <<endl; } delete []car_info; return 0; }
8.編寫一個程序,它使用一個char數組和循環來每次讀取一個單詞,直到用戶輸入done為止。随後,該程序指出用戶輸入了多少個單詞(不包括done在内)。
下面是該程序的運行情況:
Enter words (to stop, type the word done): anteater birthday category dumpster envy finagle geometry done for sure You entered a toal of 7 words.
您應該在程序中包含頭文件cstring,并使用函數strcmp()來進行比較測試
答案:
#include <iostream> #include <cstring> using namespace std; int main() { int count = 0; char words[50] = {0}; cout << "Enter words (to stop, type the word done)\n"; cin >> words; while(strcmp(words, "done") != 0) { count ; cin >> words; } cout <<"for sure You entered a toal of " << count << " words" << endl; return 0; }
9.編寫一個滿足前一個練習中描述的程序,但是用string對象而不是字符數組。請在程序中包含頭文件string,并使用關系運算符來進行比較測試。
答案:
#include <iostream> #include <string> using namespace std; int main() { int count = 0; string words; cout << "Enter words (to stop, type the word done)\n"; cin >> words; while(words != "done") { count ; cin >> words; } cout <<"for sure You entered a toal of " << count << " words" << endl; return 0; }
10.編寫一個使用嵌套循環的程序,要求用戶輸入一個值,指出要顯示多少行,然後程序将顯示相應的行數的星号,其中第一行包括一個星号,第二行包括兩個星号,以此類推,每一行包含的字符數等于用戶指定的行數,在星号不夠的情況下,在星号前面加上句點。
輸出:
Enter number of rows: 5
. . . . *
. . . * *
. . * * *
. * * * *
* * * * *
答案:
#include <iostream> using namespace std; int main() { int rows = 0; cout << "Enter number of rows: "; cin >> rows; for (int i = 0; i < rows; i ) { for (int j = rows; j > i 1; j--) { cout << ". "; } for (int k = 0; k <= i; k ) { cout << "* "; } cout <<endl; } return 0; }
,更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!