声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便。
(具体方式参见第3章模板)
1. 编写一个要求用户输入两个整数的程序。该程序将计算并输出这两个整数之间(包括这两个整数)所有整数的和。
#include <iostream> using namespace std; void cprimerplus_exercise_5_1() { cout << "Please input two integers!" <<endl; int a , b, c; int sum = 0; cout << "Please input the smaller one:"; cin >> a; c = a; cout <<"Please input the larger one:"; cin >> b; for ( a; a <= b; a++) { sum += a; } cout << "The sum of numbers between " << c << " and " << b << " is "<< sum << endl; }
2.使用array对象(不是数组)和long double(不是long long)重新编写程序清单5.4,并计算100!的值。(编译器不支持array)
#include <iostream> #include <array> using namespace std; const int ArSize = 100; void cprimerplus_exercise_5_2() //have a problem { array<long double, 100>arr; arr[1] = arr[0] = 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; } }
3.编写一个要求用户输入数字的程序。每次输入后,程序都将报告到目前为止,所有输入的累积和。当用户输入0是结束程序。
#include <iostream> using namespace std; void cprimerplus_exercise_5_3() { int a = 0; cout << "Please input a number:"; cin >> a; int sum = 0; int cnt = 0; while (a != ‘\0‘) { sum += a; ++ cnt; cout << "utile now, the total sum of "<< cnt <<" numbers is " << sum << endl; cout << "Please input a number:"; cin >> a; } }
4. Daphne以10%的单利投资了100美元。也就是说,每一年的利润都是投资额的10%,即每年10美元;而Cleo以5%的复利投资100美元,也就是说,利息是当前存款(包括获得的利息)的5%;请编写一个程序,计算多少年以后,Cleo的投资价值才能超过Daphne的投资价值,并显示此时两个人的投资价值。
#include <iostream> using namespace std; const double simplerate = 0.1; const double compoundrate = 0.05; const int principal = 100; void cprimerplus_exercise_5_4() { double sum1 = principal; double sum2 = 0.0; int year = 0; while (sum2 < sum1) { ++year; sum1 +=10; sum2 = (principal + sum2) * compoundrate + sum2; } cout << "After" << year << "years, Cleo‘s investment income can surpass Daphne!" <<endl; cout << "At the time, Cleo‘s income is "<< sum2 << " , while Daphne‘s income is " << sum1 << endl; //system("pause"); }
5. 假设要销售《C++ For Fools》一书。请编写一个程序,输入全年中每个月的销售量(图书数量,而不是销售额)。程序通过循环,使用初始化为月份字符串的char*数组(或string对象数组)逐月进行提示,并将输入的数据储存在int数组中。然后,程序计算数组中个元素的总数,并报告这一年的销售情况。
#include <iostream> using namespace std; void cprimerplus_exercise_5_5() { const char* months[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; int salesnumber[12], sum = 0; for (int i = 0; i < 12; i++) { cout << "Please input the " << *(months + i) << " sales numbers:"<< endl; cin >> salesnumber[i]; cin.get(); sum += salesnumber[i]; } cout << "The total sales number of this year is " << sum << endl; }
6.完成编程练习5,当这一次使用一个二维数组来储存输入——3年中每个月的销售量。程序将报告每年销售量以及三年的总销售量。
#include <iostream> using namespace std; void cprimerplus_exercise_5_6() { const char* months[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; const char* years[3] = { "First year", "Second year", "Third year"}; int salesnumber[3][12], sum = 0, tmp =0, year_sale[3]; for (int i = 0; i < 3; i++) { cout << "Please input the " << *(years + i) << " years every month‘s numbers:"<< endl; for (int j = 0; j < 12; j++) { cout << *( months + j) << " sales number is :"; cin >> salesnumber[i][j]; cin.get(); tmp += salesnumber[i][j]; } year_sale[i] = tmp; tmp = 0; sum += year_sale[i]; } for (int i = 0; i < 3; i++) { cout << *(years + i) << "year sales number is " << year_sale[i] <<endl; } cout << "The total sales number of this year is " << sum << endl; }
7.设计一个名为car的结构,用它储存下述有关汽车的信息;生产商(储存在字符数组或string对象中的字符串)、生产年份(整数)。编写一个程序,向用户询问有多少辆汽车。随后,程序使用new来创建一个有相应数量的car结构组成的动态数组。接下来,程序提示用户输入每辆车的生产商(可能有多个单词组成)和年份信息。请注意:这需特别小心,因为它将交替读取数值和字符串。最后,程序将显示每个结构的内容。
#include <iostream> #include <string> using namespace std; void cprimerplus_exercise_5_7() { struct car { string carmaker; //char carmaker[20]; int makeyear; }; cout << "How many cars do you wish to catalog?"; int num; cin >> num; cin.get(); car *cars = new car[num]; for (int i = 0; i < num; i++) { cout << "Car #" << i+1 << ":"<< endl; cout << "Please enter the maker:"; //cin >> cars[i].carmaker; getline(cin, cars[i].carmaker); cout << "Please enter the year made:" ; cin >> cars[i].makeyear; cin.get(); } cout << "Here is your collection:" << endl; for (int i = 0; i< num; i++) { cout << cars[i].makeyear << ‘\t‘ << cars[i].carmaker << endl; } }
8.编写一个程序,它使用一个char数组和循环来每次读取一个单词,直到用户输入done为止。随后,该程序指出用户输入多少个单词(不包括done在内)。你应在程序中包含头文件cstring.h,并使用函数strcmp()来进行比较测试。
#include <iostream> #include <cstring> using namespace std; void cprimerplus_exercise_5_8() { cout << "Enter words (to stop, type the word ‘done‘):"<<endl; char words[20]; cin >> words; int cnt = 0; while (strcmp(words,"done") != 0) { ++cnt; cin >> words; } cout << "you entered a total of " << cnt << " words." << endl; }
9.编写一个满足前一个练习中描述的程序,但使用string对象而不是字符数组。请在程序中包含头文件string,并使用关系运算符来进行比较测试。
#include <iostream> #include <string> using namespace std; void cprimerplus_exercise_5_9() { cout << "Enter words (to stop, type the word ‘done‘):"<<endl; string words; cin >> words; int cnt = 0; while (words != "done") { ++cnt; cin >> words; } cout << "you entered a total of " << cnt << " words." << endl; }
10.编写一个使用嵌套循环的程序,要求用户输入一个值,指出要显示多少行。然后,程序将显示相应行数的星号,其中第一行包括一个星号,以此类推。每一行包含的字符数等于用户指定的行数,在星号不够的情况下,在星号前面加上句号。
#include <iostream> using namespace std; void cprimerplus_exercise_5_10() { cout << "Enter number of rows:"; int rows; cin >> rows; cin.get(); for (int i = 1; i <= rows; i++) { for( int j = 0; j < rows - i; j++) { cout << "."; } for (int k = 0; k < i; k++) { cout << "*"; } cout << endl; } }
c++primerplus(第六版)编程题——第5章(循环和关系表达式)