C++ Primer Plus 第五章编程题练习
第一题
题目描述
编写一个要求用户输入两个整数的程序。该程序将计算并输出这两个整数之间(包括这两个整数) 所有整数的和。这里假设先输入较小的整数。例如,如果用户输入的是2和9,则程序将指出2~9之间所有整数的和为44。
代码片段
#include <iostream>
using namespace std;
int main()
{
int a,b;
cout<<"Please enter two numbers, the first is larger than another.\nThe first: ";
cin>>a;
cout<<"The another: ";
cin>>b;
long long sum=0;
for(int i=a;i<=b;i++)
sum+=i;
cout<<"The sum between "<<a<<" and "<<b<<" is "<<sum<<endl;
while(cin.get()!=EOF);
return 0;
}
第二题
题目描述
使用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>factorials;
factorials[1] = factorials[0] = 1;
for (int i = 2; i < ArSize; i++)
factorials[i] = i * factorials[i - 1];
cout<<fixed;
cout.precision(0);
for (int i = 0; i < ArSize; i++)
cout << i << "! = " << factorials[i] << endl;
while(cin.get()!=EOF);
return 0;
}
第三题
题目描述
编写一个要求用户输入数字的程序。每次输入后,程序都将报告到目前为止,所有输入的累计和。 当用户输入0时,程序结束。
代码片段
#include <iostream>
using namespace std;
int main()
{
long long sum=0,a;
cout<<"Please enter a number, i will give you all of your numbers's sum(0 to quit).\n";
while(cin>>a)
{
sum+=a;
if(!a)
{
cout<<"Done!\n";
break;
}
else
cout<<"The sum is "<<sum<<endl<<"(0 to quit)"<<"Please enter the next number:";
}
while(cin.get()!=EOF);
return 0;
}
第四题
题目描述
Daphne以10%的单利投资了100 美元。也就是说,每一年的利润都是投资额的10%,即每年10美元:利息 = 0.10 X 原始存款 而Cleo以5%的复利投资了100美元。也就是说,利息是当前存款(包括获得的利息)的5%, 利息 = 0.05 X 当前存款 Cleo在第一年投资100美元的盈利是5%得到了 105 美元。下一年的盈利是105美元的5%即 5.25美元,依此类推。请编写一个程序,计算多少年后,Cleo 的投资价值才能超过Daphne的投资价值, 并显示此时两个人的投资价值。
代码片段
#include <iostream>
#include<string>
using namespace std;
int main()
{
int daphne=100;
float cleo=100;
float da=0.1,cl=0.05;
int i;
for(i=0;cleo<=daphne;i++)
{
daphne+=10;
cleo*=1.05;
cout<<i+1<<" years later:\nDaphne: "<<daphne<<" Cleo: "<<cleo<<endl;
}
cout<<i<<" years later, Cleo's value is higher than Daphne."<<endl;
while(cin.get()!=EOF);
return 0;
}
/* Daphne以10%的单利投资了100 美元。也就是说,每一年的利润都是投资额的10%
,即每年10美元:利息 = 0.10 X 原始存款
而Cleo以5%的复利投资了100美元。也就是说,利息是当前存款(包括获得的利息)的5%,
利息 = 0.05 X 当前存款
Cleo在第一年投资100美元的盈利是5%得到了 105 美元。下一年的盈利是105美元的5%即
5.25美元,依此类推。请编写一个程序,计算多少年后,Cleo 的投资价值才能超过Daphne的投资价值,
并显示此时两个人的投资价值。 */
第五题
题目描述
假设要销售《C++ For Fools》一书。 请编写一个程序,输入全年中每个月的销售量(图书数量,而 不是销售额)。程序通过循环,使用初始化为月份字符串的char *数组(或string对象数组)逐月进行提示, 并将输入的数据储存在一个int数组中。然后,程序计算数组中各元素的总数,并报告这一年的销售情况。
代码片段
#include <iostream>
#include <string>
#include <array>
using namespace std;
int main()
{
string month[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
array<int, 12> a;int sum=0;
for (int i = 0; i < 12; i++)
{
cout << "Please enter " << month[i] << "'s sales: ";
cin >> a[i];
sum+=a[i];
}
cout<<"This year's sales is "<<sum<<'.'<<endl;
while (cin.get() != EOF)
;
return 0;
}
第六题
题目描述
完成编程练习5,但这一次使用一个二维数维来存储输入3年中每个月的销售量。程序将报告 每年销售量以及三年的总销售量。
代码片段
#include <iostream>
#include <string>
#include <array>
using namespace std;
int main()
{
string month[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
array<int,12>a[3];
int allsum = 0, yearsum;
for (int i = 0; i < 3; i++)
{
yearsum=0;
cout << "Please enter the sales of year " << i + 1 << endl;
for (int j = 0; j < 12; j++)
{
cout << "Please enter " << month[j] << "'s sales: ";
cin >> a[i][j];
allsum += a[i][j];
yearsum+=a[i][j];
}
cout<<"This year's sales is "<<yearsum<<'.'<<endl;
}
cout<<"All year's sales is "<<allsum<<'.'<<endl;
while (cin.get() != EOF)
;
return 0;
}
/* 完成编程练习5,但这一次使用一个二维数维来存储输入3年中每个月的销售量。程序将报告
每年销售量以及三年的总销售量。 */
第七题
题目描述
设计一个名为car的结构,用它存储下述有关汽车的信息:生产商(存储在字符数组或string 对象 中的字符串)、生产年份(整数)。编写一个程序,向用户询问有多少辆汽车。随后,程序使用new来创建 一一个由相应数量的car结构组成的动态数组。接下来,程序提示用户输入每辆车的生产商(可能由多个单 词组成)和年份信息。请注意,这需要特别小心,因为它将交替读取数值和字符串(参见第4章)。最后, 程序将显示每个结构的内容。该程序的运行情况如下:
How many cars do you wish to catalog? 2
Car #1:
Please enter the make: Hudson Hornet
PIease enter the year made: 1952
Car #2:
PTease 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;
struct car
{
string brand;
int year;
};
int main()
{
int n;
cout << "How many cars do you wish to catalog? ";
cin >> n;
car *ps = new car[n];
for (int i = 0; i < n; i++)
{
cin.get();
cout << "Car #" << i + 1 << ":\n";
cout << "Please enter the make: ";
getline(cin, ps[i].brand);
cout << "PIease enter the year made: ";
cin >> ps[i].year;
}
cout << "Here is your collection: \n";
for (int i = 0; i < n; i++)
cout << ps[i].year << ' ' << ps[i].brand << endl;
while (cin.get() != EOF)
;
return 0;
}
/* 设计一个名为car的结构,用它存储下述有关汽车的信息:生产商(存储在字符数组或string 对象
中的字符串)、生产年份(整数)。编写一个程序,向用户询问有多少辆汽车。随后,程序使用new来创建
一一个由相应数量的car结构组成的动态数组。接下来,程序提示用户输入每辆车的生产商(可能由多个单
词组成)和年份信息。请注意,这需要特别小心,因为它将交替读取数值和字符串(参见第4章)。最后,
程序将显示每个结构的内容。该程序的运行情况如下:How many cars do you wish to catalog? 2
Car #1:
Please enter the make: Hudson Hornet
PIease enter the year made: 1952
Car #2:
PTease enter the make: Kaiser
please enter the year made: 1951
Here is your collection:
1952 Hudson Hornet
1951 Kaiser */
第八题
题目描述
编写一个程序,它使用一个char数组和循环来每次读取一个单词,直到用户输入done为止。随后. 该程序指出用户输入了多少个单词(不包括done在内)。下面是该程序的运行情况: 您应在程序中包含头文件cstring,并使用函数strcmp( )来进行比较测试。
Enter words (enter the word——done to stop):
anteater birthday category dumpster
envy finag1e geometry done for sure
You've entered a total of 7 words.
代码片段
#include <iostream>
#include<cstring>
using namespace std;
int main()
{
char word[40]={0};int count=0;
cout<<"Enter words (enter the word——done to stop):\n";
for(count=0;strcmp(word,"done");count++)
cin>>word;
cout<<"You've entered a total of "<<count-1<<" words.\n";
while(cin.get()!=EOF);
return 0;
}
第九题
题目描述
编写一个满足前一个练习中描述的程序,但使用string对象而不是字符数组。请在程序中包含头文 件string,并使用关系运算符来进行比较测试。
代码片段
#include <iostream>
#include<string>
using namespace std;
int main()
{
string word;int count=0;
cout<<"Enter words (enter the word——done to stop):\n";
for(count=0;word!="done";count++)
cin>>word;
cout<<"You've entered a total of "<<count-1<<" words.\n";
while(cin.get()!=EOF);
return 0;
}
第十题
题目描述
编写一个使用嵌套循环的程序,要求用户输入一个值,指出要显示多少行。然后,程序将显示相 应行数的星号,其中第一行包括一个星号,第二行包括两个星号,依此类推。每一行包含的字符数等于用 户指定的行数,在星号不够的情况下,在星号前面加上句点。该程序的运行情况如下:
Enter number of rows: 5
....*
...**
..***
.****
*****
代码片段
#include <iostream>
using namespace std;
void printdot(int);
void printstar(int);
int main()
{
cout<<"Enter number of rows: ";int n;cin>>n;
for(int i=0;i<n;i++)
{
printdot(n-i-1);
printstar(i+1);
cout<<endl;
}
while(cin.get()!=EOF);
return 0;
}
void printdot(int a)
{
for(int i=0;i<a;i++)
cout<<'.';
}
void printstar(int a)
{
for(int i=0;i<a;i++)
cout<<'*';
}