《C++ Primer Plus(第六版)》(7)(第五章 循环和关系表达式 答案)
5.8 复习题
1.入口条件就是在进入循环的时候就检测,出口条件是先进入循环,再检测,至少可以执行一次循环。
for、while属于入口条件检测,do while属于出口条件检测。
2.
01234
int i; for (i = 0; i < 5; i++) cout << i; cout << endl;
3.
0369
12
int j; for (j = 0; j < 11; j += 3) cout << j; cout << endl << j << endl;
4.
6
8
int j = 5; while (++j < 9) { cout << j++ << endl; }
5.
k = 8
int k = 8; do cout << "k = " << k << endl; while (k++ < 5);
6.
for (int i = 1; i <= 64; i *= 2) { cout << i<<" "; } cout << endl;
7.使用{},其实就算只有一条语句,也应该使用{},可以是代码更加清晰,而且说不定什么时候需求就要变了,一条语句通常干不了什么的。
8.都有效。
int x = (1, 024);//x为20,()内返回024,0开头是8进制,即是20. int y; y = 1, 024;//y=1,之后024.就没了
9.cin>>ch,跳过空格、换行符、制表符。cin.get(ch) 和 ch = cin.get()可以读取这些字符
5.9编程题
1.
int m = 0; int n = 0; cin >> m; cin >> n; if (m > n) { int t = m; m = n; n = t; } int total = 0; for (int i = m; i <= n; i++) { total += i; } cout << total <<endl;
2.
5.4程序清单
const int ArSize = 16; long long factorials[ArSize]; factorials[1] = factorials[0] = 1LL; for (int i = 2; i < ArSize; i++) { factorials[i] = i * factorials[i-1]; } for (int i = 0; i < ArSize; i++) { cout << i << " = " << factorials[i] << endl; }修改后:
const int ArSize = 101; array<long double, ArSize> factorials; factorials[1] = factorials[0] = 1; for (int i = 2; i < ArSize; i++) { factorials[i] = i * factorials[i-1]; } for (int i = 0; i < ArSize; i++) { cout << i << " = " << factorials[i] << endl; }结果:
0 = 1
1 = 1
2 = 2
3 = 6
4 = 24
5 = 120
6 = 720
7 = 5040
8 = 40320
9 = 362880
10 = 3.6288e+06
11 = 3.99168e+07
12 = 4.79002e+08
13 = 6.22702e+09
14 = 8.71783e+10
15 = 1.30767e+12
。。。好长
cout << "Please enter:" << endl; int i = -1; int total = 0; while (i != 0) { cin >> i; total += i; cout << "current sum: "<< total << endl; }
4.
const double base = 100; const double interestA = 0.1; const double interestB = 0.05; double moneyA = base; double moneyB = base; int year = 0; while (moneyA >= moneyB) { ++year; moneyA += (base * interestA); moneyB += (moneyB * interestB); } cout << "Year:" << year << " A:" << moneyA << " B:" << moneyB << endl;结果:
Year:27 A:370 B:373.346
string month[12] = { "January","February","March","April", "May","June","July","August", "September","October","November","December"}; int total = 0; int thisYearData[12]; for (int i = 0; i < 12; ++i) { cout << "Please enter " << month[i] <<"'data:" ; cin >> thisYearData[i]; total += thisYearData[i]; } cout << "this year' data: "<< total <<endl;
6.
string month[12] = { "January","February","March","April", "May","June","July","August", "September","October","November","December"}; int total = 0; int yearData[3][12]; int yearTotal[3]; for (int j = 0; j < 3; ++j) { for (int i = 0; i < 12; ++i) { cout << "Please enter the " << j + 1 << " year " << month[i] <<"'data:" ; cin >> yearData[j][i]; total += yearData[j][i]; yearTotal[j] += yearData[j][i]; } } for (int i = 0; i < 3; ++i) { cout << "year:"<< i + 1 << " data:"<< yearTotal[i] << endl; } cout << "all year' data: "<< total <<endl;
7.
struct SCar { string manufacturer; int year; }; int num = 0; cout << "How many cars do you wish to catalog?"; cin >> num; cin.get(); SCar* carArr = new SCar[num]; for (int i = 0; i < num; ++i ) { cout<< "Car #" << i + 1 << ":"<<endl; cout<< "Please enter the make:"; getline(cin, carArr[i].manufacturer); cout << "Please enter the year made:"; cin >> carArr[i].year; cin.get(); } cout << "Here is your collection:" << endl; for (int i = 0 ; i < num; ++i) { cout << carArr[i].year << " " << carArr[i].manufacturer << endl; }
8.
char s[20]; int count = 0; cout << "Enter words (to stop, type the word done):" <<endl; while (true) { cin >> s; if (0 == strcmp(s, "done")) { break; }else{ ++count; } } cout << "You entered a total of " << count << " words" << endl;
9.
string s; int count = 0; cout << "Enter words (to stop, type the word done):" <<endl; while (true) { cin >> s; if (s == "done") { break; }else{ ++count; } } cout << "You entered a total of " << count << " words" << endl;
10.
cout << "Enter number of rows: "; int n = 0; cin >> n; for (int i = 0; i < n; ++i) { for (int j = 0; j < n ; ++j) { if (j < n - i - 1) { cout << "." ; }else{ cout << "*"; } } cout << endl; }
人生如戏,还是戏如人生?微信公众号:传说之路
csdn博客 http://blog.csdn.net/u012175089/article/list/2