C++ Primier Plus(第六版) 第五章 循环和关系表达式编程练习答案
1.编写一个要求用户输入两个整数的小程序。该程序将计算并输出两个整数之间(包括这两个整数)所有整数的和。这里假设先输入较小的整数。例如,用户输入的是2和9,则程序将指出2~9之间所有整数的和为44.
该题使用一个for循环就可以解决,代码如下:
// ex1.cpp -- calculate the sum of between two integers' all integer
#include<iostream>
int main()
{
using namespace std;
int i,j;
int sum = 0;
cout << "Enter two integers: ";
cin >> i >> j;
for(;i<=j;i++)
sum += i;
cout << "The sum of between two integers' all integers is ";
cout << sum << endl;
return 0;
}
运行结果如下:
2. 使用array对象(而不是数组)和long double(而不是long long)重新编写程序清单5.4,并计算100!的值。
该题要求使用array对象,数据类型为long double,
array<long double,100> factories
代码如下:
// ex2.cpp -- calculate 100!
#include<iostream>
#include<array>
const int arsize = 101;
int main()
{
using namespace std;
array<long double,arsize> factories;
factories[0] = 1;
factories[1] = 1;
for(int i = 2; i < arsize; ++i)
factories[i] = factories[i-1] * i;
cout << "100! = " << factories[100] << endl;
return 0;
}
运行结果如下:
3. 编写一个要求用户输入数字的程序。每次输入后,程序都将报告到目前为止,所有输入的累计和,当用户输入0时,程序结束。
本题不知道循环次数,显然应该用while循环。
代码如下:
// ex3.cpp -- calculate sum
#include<iostream>
int main()
{
using namespace std;
double i;
double sum = 0;
cout << "Enter a figure, input 0 will end.\n";
cin >> i;
while(i)
{
sum += i;
cout << "sum = " << sum << endl;
cin >> i;
}
return 0;
}
运行结果如下:
4. Daphne以10%的单利投资了100美元。也就是说,每一年的利润都是投资额的10%,即每年10美元:
而Cleo以5%的复利投资了100美元。也就是说,利息是当前存款(包括获得的利息)的5%:
Cleo在第一年投资100美元的盈利是5%——得到了105美元。下一年的盈利是105美元的5%——即5.25美元,依此类推。请编写一个程序,计算多少年之后,Cleo的投资价值才能超过Daphne的投资价值,并显示此时两个人的投资价值。
本题首先分别计算两人的盈利:
Daphne: profit_d = 100 + 10*year;
Cleo: profit_c = 100*1.05^year;
写成循环,代码如下:
// ex4.cpp -- calculate profit
#include<iostream>
const int money_init = 100;
int main()
{
using namespace std;
double profit_c = money_init;
int profit_d = money_init;
int year = 0;
while(profit_c<=profit_d)
{
++ year;
profit_d += 10;
profit_c += 0.05 * profit_c;
}
cout << "After " << year << " years, ";
cout << "Cleo over Daphne.\n";
cout << "Cleo's profit: " << profit_c << endl;
cout << "Daphne's profit: " << profit_d << endl;
return 0;
}
运行结果如下:
5. 假设要销售《C++ForFools》一书。请编写换一个程序,输入全年中每个月的销售量(图书数量,而不是销售额)。程序通过循环,使用初始化为月份字符串的char*数组(或者string对象数组进行逐月提示),并将输入的数据存储在一个int数组中。然后程序计算数组中各元素的总数,并报告这一年的销售情况。
本题使用string对象数组构建十二个月的字符,然后用for循环来进行输入和数据处理,代码如下:
// ex5.cpp --calculate a year sales of book
#include<iostream>
#include<string>
const int Months = 12;
int main()
{
using namespace std;
string month[Months]={"January", "February","March","April","May",
"June","July","August","September","October","November","December"};
// const char *month[Months] = {"January", "February","March","April","May",
// "June","July","August","September","October","November","December"};
long sum = 0;
int sale[Months];
for(int i = 0; i < Months; ++i)
{
cout << "Enter the amount of sale in " << month[i] << ": ";
cin >> sale[i];
sum += sale[i];
}
cout << "The sale volume of this year is " << sum << endl;
return 0;
}
注:使用char * 定义数组时初始化时,应该加const.
运行结果如下:
6. 完成编程练习5,但这一次使用一个二位数组来输入——3年中每个月的销售量。程序将报告每年销售量以及三年的总销售量。
本题涉及到二维数组的定义和循环遍历。
代码如下:
// ex6.cpp --calculate three year sales of book
#include<iostream>
#include<string>
const int Months = 12;
const int Years = 3;
int main()
{
using namespace std;
string month[Months]={"January", "February","March","April","May",
"June","July","August","September","October","November","December"};
const char* year[Years] = {"2019", "2020", "2021"};
// const char *month[Months] = {"January", "February","March","April","May",
// "June","July","August","September","October","November","December"};
long sum_year = 0;
long sum = 0;
int sale[Months][Years];
for(int i = 0; i < Years; ++i)
{
for(int j = 0; j < Months; ++j)
{
cout << "Enter the amount of sale in " << year[i] << ",";
cout << month[j] << ": ";
cin >> sale[i][j];
sum_year += sale[i][j];
}
cout << "The sales in " << year[i] << ": " << sum_year << endl;
sum += sum_year;
sum_year = 0;
}
cout << "The sales of three years: " << sum << endl;
return 0;
}
运行结果如下:
Enter the amount of sale in 2019,January: 1000
Enter the amount of sale in 2019,February: 1001
Enter the amount of sale in 2019,March: 1002
Enter the amount of sale in 2019,April: 1003
Enter the amount of sale in 2019,May: 1004
Enter the amount of sale in 2019,June: 1005
Enter the amount of sale in 2019,July: 1006
Enter the amount of sale in 2019,August: 1007
Enter the amount of sale in 2019,September: 1008
Enter the amount of sale in 2019,October: 1009
Enter the amount of sale in 2019,November: 1010
Enter the amount of sale in 2019,December: 1011
The sales in 2019: 12066
Enter the amount of sale in 2020,January: 2001
Enter the amount of sale in 2020,February: 2002
Enter the amount of sale in 2020,March: 2003
Enter the amount of sale in 2020,April: 1004
Enter the amount of sale in 2020,May: 2005
Enter the amount of sale in 2020,June: 2006
Enter the amount of sale in 2020,July: 2007
Enter the amount of sale in 2020,August: 2008
Enter the amount of sale in 2020,September: 2009
Enter the amount of sale in 2020,October: 2010
Enter the amount of sale in 2020,November: 2011
Enter the amount of sale in 2020,December: 2012
The sales in 2020: 23078
Enter the amount of sale in 2021,January: 3001
Enter the amount of sale in 2021,February: 3002
Enter the amount of sale in 2021,March: 3003
Enter the amount of sale in 2021,April: 3004
Enter the amount of sale in 2021,May: 3005
Enter the amount of sale in 2021,June: 2006
Enter the amount of sale in 2021,July: 3007
Enter the amount of sale in 2021,August: 3008
Enter the amount of sale in 2021,September: 3009
Enter the amount of sale in 2021,October: 3010
Enter the amount of sale in 2021,November: 3011
Enter the amount of sale in 2021,December: 3012
The sales in 2021: 35078
The sales of three years: 70222
7. 设计一个名为car的结构,用它存储下述有关汽车的信息:生产商(存储在字符数组或者string对象的字符串)、生产年份(整数)。编写一个程序,向用户询问有多少辆汽车。随后,程序使用new来创建一个由相应数量的car结构组成的动态数组。接下来,程序提示用户每辆车的生产商(可能由多个单词组成)和年份信息。请注意,这需要特别小心,因为它将交替读取数值和字符串(参见第4章)。最后,程序将显示每个结构的内容。该程序的运行情况如下:
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
本题首先定义一个car结构,使用new创建一个动态数组,然后按照题目要求进行输入和输出。
- 注意string输入一行时用getline(cin,string),且回车符会被读取
- 在数字与字符串交替读取时,注意读取数字会保留回车符,因此需要用cin.get()
代码如下:
// ex7.cpp -- struct,new,for
#include<iostream>
#include<string>
#include<cstring>
struct car
{
std::string make;
int year;
};
int main()
{
using namespace std;
char temp[20];
int temp_y;
int n;
cout << "How many cars do you wish to catalog? ";
(cin >> n).get();
car* Car = new car[n];
for(int i = 0; i < n; ++i)
{
cout << "Car #" << (i+1) << ":\n";
cout << "Please enter the make: ";
getline(cin,Car[i].make);
cout << "Please enter the year made: ";
(cin >> Car[i].year).get();
}
for(int i = 0; i < n; ++i)
cout << (Car+i)->year << " " << Car[i].make << endl;
return 0;
}
运行结果如下:
8. 编写一个程序,它使用一个char数组和循环来每次读取一个单词,直到用户输入done为止。随后,该程序指出用户输入了多少个单词(不包括done在内)。下面是程序的运行情况:
Enter words(to stop, typer the word done):
anteater birthday category dumpster
envy finagle geometry done for sure
You entered a total of 7 words.
您应在程序中包含头文件cstring,并使用函数strcmp来进行比较测试。
本题每次读取一个单词的意思,笔者刚开始误解了,以为要自己写一个while循环,按一个字符一个字符输入,遇到空格 ' '
停止,重新开始读取下一个单词,存在许多问题,如临时存储数组的更新等等,导致程序一直调试错误,这里挖个坑吧,等把C++这本书学会了,再来用这次的理解做一下这道题。目前使用cin>>直接按照单词读取,实际上题目也是这么要求的,代码如下:
// ex8.cpp -- get the number of enter words
#include<iostream>
#include<cstring>
int main()
{
using namespace std;
char temp[20];
int count = 0;
int i = 0;
cout << "Enter words(to stop, type the word done): \n";
cin >> temp;
while(strcmp(temp,"done")) // or while(strcmp(temp,"done")!=0)
{
/* use while or other to get cin >> temp*/
// cin >> temp[i];
// while(temp[i] != ' ')
// {
// i++;
// cin >> temp[i];
// }
// i = 0;
cin >> temp;
count++;
}
cout << "You entered a total of " << count << " words.\n";
return 0;
}
注:准确的分析题目比编程更重要。
运行结果如下:
9. 编写一个满足前一个练习中描述的程序,但使用string 对象而不是字符数组。请在程序中包含头文件string,并使用关系运算符来进行比较测试。
本题与第八题类似,需要修改的代码就是单词声明,与while的test-conditon, 代码如下:
// ex9.cpp -- get the number of enter words by using string
#include<iostream>
#include<string>
int main()
{
using namespace std;
string word;
int count = 0;
int i = 0;
cout << "Enter words(to stop, type the word done): \n";
cin >> word;
while(word != "done")
{
cin >> word;
count++;
}
cout << "You entered a total of " << count << " words.\n";
return 0;
}
运行结果如下:
10. 编写一个使用循环嵌套的程序,要求用户输入一个值,指出要显示多少行。然后,程序将显示相应行数的星号,其中第一行包括一个星号,第二行包括两个星号,依此类推。每一行包含的字符数等于用户指定的行数,在星号不够的情况下,在星号的前面加上句点。该程序的运行情况如下:
Enter number of rows: 5
....*
...**
..***
.****
*****
根据图形分析,首先需要一个for循环输出row行字符,在上述for循环里嵌套for循环输入row-当前行数的.
,接着输出当前行数个的*
。代码如下:
// ex10.cpp -- draw figure
#include<iostream>
int main()
{
using namespace std;
int rows;
cout << "Enter number of rows: ";
cin >> rows;
for(int i = 1; i <= rows; ++i)
{
for(int j = rows -i; j > 0; --j)
{
cout <<".";
}
for(int k = 1; k <=i; ++k)
cout <<"*";
cout << endl;
}
return 0;
}
运行结果如下: