C++PrimerPlus(第6版)中文版 第5章 循环和关系表达式 编程练习 参考答案

自己编写的参考答案,在VS2019中都可以编译通过,不是标准答案,也不是最优答案,仅供参考

1.编写一个要求用户输入两个整数的程序。该程序将计算并输出这两个整数之间(包括这两个整数)所有整数的和。这里假设先输入较小的整数。例如,如果用户输入的是2和9,则程序将指出2~9之间所有整数的和为44.

#include< iostream >
using namespace std;
int main()
{
int a, b;
cout << “请输入两个整数:”;
cin >> a>>b;
int sum=0;
for (int c = a; c <= b; c++)
{
sum += c;
}
cout << a << “~” << b << “之间所有整数的和为” << sum;
}

2.使用array对象(而不是数组)和long double(而不是long long)重新编写程序清单5.4,并计算100!的值。

#include< iostream >
#include< array >
using namespace std;
const int arSize = 101;
int main()
{
array<long double, arSize> factorial;
factorial[0] = 1;
factorial[1] = 1;
for (int i = 2; i < arSize; i++)
{
factorial[i] = i * factorial[i - 1];
}
for (int j = 0; j < 16; j++)
{
cout << j << "! = " << factorial[j]<<endl;
}
cout<< arSize-1 << "! = " << factorial[arSize-1] << endl;
}

3.编写一个要求用户输入数字的程序。每次输入后,程序都将报告到目前为止,所有输入的累计和。当用户输入0时,程序结束。

#include< iostream >
using namespace std;
int main()
{
float a = 0;
float sum = 0;
cout << “请输入一个数:”;
cin >> a;
while (a!=0)
{
sum += a;
cout << “目前总和为:” << sum<<endl;
cout << “请输入一个数:”;
cin >> a;
}
}

4.Daphne以10%的单利投资了100美元。也就是说,每一年的利润都是投资额的10%,即每年10美元:

利息=0.10*原始存款

而Cleo以5%的复利投资了100美元。也就是说,利息是当前存款(包括获得的利息)的5%,:

利息=0.05*当前存款

Cleo在第一年投资100美元的盈利是5%——得到105美元。下一年的盈利是105美元的5%——即5.25美元,依此类推。请编写一个程序,计算多少年后,Cleo的投资价值才能超过Daphne的投资价值,并显示此时两个人的投资价值。

#include< iostream >
using namespace std;
const float daphneInterestRate = 0.1;
const float cleoInterestRate = 0.05;
int main()
{
float daphneSum;
float cleoSum;
int i ;
for (daphneSum = 100, cleoSum = 100,i=0; cleoSum<= daphneSum; i++)
{
daphneSum += (100 * daphneInterestRate);
cleoSum += (cleoSum * cleoInterestRate);
}
cout << “经过”<<i<<“年后,Cleo的投资价值为:”<< cleoSum<<",Daphne的投资价值为:"<< daphneSum;
}

5.假设要销售《C++ For Fools》一书。请编写一个程序,输入全年中每个月的销售量(图书数量,而不是销售额)。程序通过循环,使用初始化为月份字符串的char*数组(或string对象数组)逐月进行提示,并将输入的数据存储在一个int数组中。然后,程序计算数组中各元素的总数,并报告这一年的销售情况。

#include< iostream >
using namespace std;
const int monthNumber = 12;
const char* months[monthNumber] = { “January”,“February”,“March”,“April”,“May”,“June”
,“July”,“August”,“September”,“October”,“November”,“December” };
int main()
{
int *numberPerMonth = new int[monthNumber];
int sum = 0;
for (int i = 0; i < monthNumber; i++)
{
cout<<“请输入”<<months[i]<<“的销售数额:”;
cin >> numberPerMonth[i];
sum += numberPerMonth[i];
}
cout << “全年销售总数为:” << sum;
delete numberPerMonth;
}

6.完成编程练习5,但这一次使用一个二维数组来存储输入——3年中每个月销售量。程序将报告每年销售量以及三年的总销售额。

#include< iostream >
using namespace std;
const int monthNumber = 12;
int main()
{
int(* p)[monthNumber] = new int[3][monthNumber];
for (int i = 0; i < 3; i++)
{
cout << “请输入第” << i + 1 << “年的销售额:”;
for (int j = 0; j < monthNumber; j++)
{
cin >> p[i][j];
}
}
int sum = 0;
int sumAll = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < monthNumber; j++)
{
sum += p[i][j];
}
cout << “第” << i + 1 << “年的销售额为:” << sum<<endl;
sumAll += sum;
sum = 0;
}
cout << “三年总销售额为:” << sumAll << endl;
delete[] p;
}

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

#include< iostream >
#include< string >
using namespace std;
int main()
{
struct Car
{
string manufacturerName;
int year;
};
int carNumber;
cout << "How many cars do you wish to catalog? ";
(cin >> carNumber).get();
Car* p = new Car[carNumber];
for (int i = 0; i < carNumber; i++)
{
cout << “Car #” << i + 1 << “:”<<endl;
cout << "Please enter the make: ";
getline(cin,p[i].manufacturerName);
cout << "Please enter the year made: ";
(cin >> p[i].year).get();
}
cout << “Here is your collection:\n”;
for (int j = 0; j < carNumber; j++)
{
cout << p[j].year << " " << p[j].manufacturerName<<endl;
}
delete []p;
}

8.编写一个程序,它使用一个char数组和循环来每次读取一个单词,直到用户输入done为止。随后,该程序指出用户输入了多少个单词(不包括done在内)。下面是该程序的运行情况:

Enter words (to stop,type the word done):

anterter birthday category dumpster

envy finagle geometry done for sure

you enter a total of 7 words.

#include< iostream >
using namespace std;
const int number = 20;
int main()
{
char words[number];
int wordsNumber = 0;
cout << “Enter words (to stop,type the word done):” << endl;
cin >> words;
while (strcmp(words,“done”))
{
wordsNumber++;
cin >> words;
}
cout << “you enter a total of " << wordsNumber << " words.”;
}

9.编写一个满足前一个练习中描述的程序,但使用string对象而不是字符数组。请在程序中包含头文件string,并使用关系运算符来进行比较测试。

#include< iostream >
#include< string >
using namespace std;
int main()
{
string words;
int wordsNumber = 0;
cout << “Enter words (to stop,type the word done):” << endl;
cin >> words;
while (words!=“done”)
{
wordsNumber++;
cin >> words;
}
cout << “you enter a total of " << wordsNumber << " words.”;
}

10.编写一个使用嵌套循环的程序,要求用户输入一个值,指出要显示多少行。然后,程序将显示相应行数的星号,其中第一行包括一个星号,第二行包括两个星号,依此类推。每一行包含的字符数等于用户指定的行数,在星号不够的情况下,在星号前面加上句点。该程序的运行情况如下:

Enter number of rows: 5

. . . . *

. . . * *

. . * * *

. * * * *

* * * * *

#include< iostream >
using namespace std;
int main()
{
cout << "Enter number of rows: ";
int rowNumber;
cin >> rowNumber;
for (int i = 1; i <= rowNumber; i++)
{
for (int j = 1; j <= rowNumber-i; j++)
{
cout << ". ";
}
for (int j = 1; j <= i; j++)
{
cout << "* ";
}
cout << endl;
}
}

发布了4 篇原创文章 · 获赞 0 · 访问量 56

猜你喜欢

转载自blog.csdn.net/HuLaTangDuoLaZi/article/details/104116470