给个赞或者评论收藏不,亲亲
编辑
复习题
代码均编译运行通过
复习题
PS:Ctrl+k+c批量注释,Ctrl+k+u批量解注释
Shift+Alt选取竖行
//5.8 //1 输入条件循环在进入输入循环体之前评估测试表达式。如果条件最初为flase,则循 退出条件循环在处理循环体之后评估测试表达式,即使条件最初为flase,循环也会 for和while都是输入条件循环,do while是退出条件循环。 //2 将打印: 01234 //3 将打印: 0369 12 //4 将打印: 6 8 //5 将打印: k = 8 //6 使用 *= 运算符最简单: for (int num = 1; num <= 64; num *= 2) cout << num << " "; //7 将语句放在一个大括号中将形成一个复合语句或代码块。 //8 第一个语句将20赋给x,第二个语句将1赋给y //9 cin>>ch将跳过空格,换行符,和制表符,其他两种格式将读取这些字符。
编程练习
//第五章编程练习 #include<iostream> #include<array> #include<string> #include<cstring> using namespace std; //practice 1 void p5_1() { int a = 0; int b = 0; cout << "Enter two number to add all number between them:please enter the smaller one first:\n"; cin >> a; cin >> b; int sum=0; for (a; a < b + 1; a++) sum += a; cout<<"The sum of them is: "<<sum<<endl; } //practice 2 const int Arsize = 101; void p5_2() { array<long double, Arsize>arr = {}; arr[1] = arr[0] = 1.0; 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; } //practice 3 void p5_3() { int num = 0; int sum = 0; while(1) { cout << "Enter a number (0 to exit):"; cin >> num; if (num == 0) break; sum += num; cout << "Untill now,the sum of the number you inputed is: " << sum << endl; } cout << "Done!\n"; } //practice 4 void p5_4() { int investment = 100; double profit1 = 110; double profit2 = 105; int i; for(i=1;profit2<=profit1;i++) { profit1 += 10; profit2 += (profit2 * 0.05); } cout << "复利投资收入高于单利在第n年,n=" << i << endl; cout << "此时复利收入为" << profit2 << ",单利收入为" << profit1 << endl; } //practice 5 void p5_5() { string month[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "Septempber", "October", "November", "December" }; int sell[12] = {}; int sum=0; for (int i=0;i<12;i++) { cout << month[i] << ": "; cin >> sell[i]; sum += sell[i]; } cout << "全年总销售量是:" << sum << endl; } //practice 6 void p5_6() { string month[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "Septempber", "October", "November", "December" }; int sell[3][12]; int sum[3] = {}; int i, j; for (i = 0; i < 3; i++) { cout << "The sell on " << i+1 << " year:\n"; for (j = 0; j < 12; j++) { cout << month[j] << ": "; cin >> sell[i][j]; sum[i] += sell[i][j]; } cout << "The sum on " << i + 1 << " year: " << sum[i] << endl; } cout << "三年销售总量:" << sum[0] + sum[1] + sum[2] << endl; } //practice 7 void p5_7() { struct car { string company; int time=0; }; int num = 1; cout << "How many cars do you wish to catalog? "; (cin >> num).get(); car * pc=new car[num]; for(int i=0;i<num;i++) { cout << "Car #" << i + 1 << ":\n"; cout << "Please enter the company: "; getline(cin,pc[i].company); cout << "Please enter the year made: "; (cin >> pc[i].time).get(); } cout << "Here is your collection: \n"; for(int i=0;i<num;i++) cout << pc[i].time << " " << pc[i].company << endl; } //practice 8 void p5_8() { char word[20]; unsigned int num_word = 0; cout << "Enter words(to stop,type the word done): " << endl; while (cin >> word) if (strcmp(word, "done")) num_word++; else break; cout << "You entered a total of " << num_word << " words."; } //practice 9 void p5_9() { string word; unsigned n_word = 0; cout << "Enter words(to stop,type the word done): " << endl; while (cin >> word) if (word!="done") n_word++; else break; cout << "You entered a total of " << n_word << " words."; } //practice 10 void p5_10() { int num = 0; cout << "Enter number of rows: "; cin >> num; for (int i = 1; i <= num; i++) { for (int j = num - i; j > 0; j--) cout << "."; for (int m = i; m > 0; m--) cout << "*"; cout << endl; } } int main() { p5_10(); return 0; }