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