您的位置:首页 > 编程语言 > C语言/C++

C++ Primer Plus第五版 第五章 编程练习答案

2017-10-19 08:58 471 查看
/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/18
From : C++ Primer Plus第五版第五章编程练习 第1题
Problem : 编写一个要求用户输入两个整数的程序。该程序将计算并输出这两个整数之间(包括这两个整数)所有整数的和。
这里假设先输入较小的整数。例如,如果用户输入的是2和9,则程序将指出2-9之间所有整数的和为44.
*******************************************************************************************************************/
#include<iostream>
using namespace std;
int main()
{
cout << "Please enter two int number: \n";
int num[2];
for (int i=0; i<2;i++)
{
cin >> num[i];
}
if (num[0]>num[1])
{
int temp=0;
temp=num[0];
num[0]=num[1];
num[1]=temp;
}
int sum=0;
for (int i=num[0];i<=num[1];i++)
sum+=i;
cout << "The sum of the int number during " << num[0] << " and " << num[1] << " is " << sum <<endl;
system("pause");
return 0;
}

/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/18
From : C++ Primer Plus第五版第五章编程练习 第2题
Problem : 编写一个要求用户输入数字的程序。每次输入后,程序都将报告到目前为止,所有输入的累计和。当用户输入0时,
程序结束
*******************************************************************************************************************/
#include<iostream>
using namespace std;
int main()
{
cout << "Please enter double number (0 represents the end): \n";
double sum=0;
double data;
cin >> data;
while(data != 0)
{
sum+=data;
cout << "The sum of the data until now is " << sum <<endl;
cin >> data;
}
system("pause");
return 0;
}

/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/18
From : C++ Primer Plus第五版第五章编程练习 第3题
Problem
4000
: 以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;
int main()
{
double saving_of_Daphne=100;
double saving_of_Cleo=100;
int year=0;

while(saving_of_Daphne >= saving_of_Cleo)
{
year++;
saving_of_Daphne=saving_of_Daphne+100*0.1;
saving_of_Cleo=saving_of_Cleo*(1+0.05);
}
cout << "After " << year << ", saving of Cleo is more than Daphne" <<endl;
cout << "Saving of Cleo is " << saving_of_Cleo << " and " <<  "saving of Daphne is " << saving_of_Daphne << endl;
system("pause");
return 0;
}

/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/18
From : C++ Primer Plus第五版第五章编程练习 第4题
Problem : 假设要销售《C++ For Fools》一书。请编写一个程序,输入全年中每个月的销售量(图书数量,而不是销售额)。
程序通过循环,使用初始化为月份字符串的char*数组(或string对象数组)逐月进行提示,并将输入的数据储存在一个int数组中。
然后,程序计算数组中各元素的总数,并报告这一年的销售情况。
*******************************************************************************************************************/
#include<iostream>
#include<string>
using namespace std;
const int MONTH =12;
int main()
{
// 方法一:
char * month[MONTH] ={"January","February","March","April","May","June","July","August","September","October","November","December"};

// 方法二:string数组

string month1 [MONTH]={"January","February","March","April","May","June","July","August","September","October","November","December"};

// 方法三:char 数组
char month2[20][MONTH] ={"January","February","March","April","May","June","July","August","September","October","November","December"};

int sales[MONTH];
int sum=0;
for (int i=0;i<MONTH;i++)
{
cout << "Please enter the sales of " << month[i] << "\t";
cin >> sales[i];
sum+=sales[i];
}
for (int i=0;i<MONTH;i++)
cout << "The sales of " << month[i] << " is :\t" << sales[i] <<endl;
cout << "The sum of the whole year is:\t " << sum <<endl;
system("pause");
return 0;
}

/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/18
From : C++ Primer Plus第五版第五章编程练习 第5题
Problem : 完成编程练习4,但这一次使用一个二维数组来存储输入——3年中每个月的销售量。程序将报告每年的销售量以及
三年的总销售量。
*******************************************************************************************************************/
#include<iostream>
using namespace std;
const int MONTH =12;
const int YEAR  =3;
int main()
{
char * month[MONTH] ={"January","February","March","April","May","June","July","August","September","October","November","December"};

int sales[YEAR][MONTH];
int sum_per_year[3]={0};
int sum=0;
for (int j=0;j<YEAR;j++)
{
for (int i=0;i<MONTH;i++)
{
cout << "Please enter the sales of " << j+1 << "th year, " <<month[i] << "\t";
cin >> sales[j][i];
sum_per_year[j]+=sales[j][i];
}
sum+=sum_per_year[j];
}
for (int i=0;i<YEAR;i++)
cout << "The sum of the " << i+1 << " th year is:\t " << sum_per_year[i]<<endl;
cout << "The sum of the "<< YEAR <<" years is:\t " << sum <<endl;
system("pause");
return 0;
}
/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/18
From : C++ Primer Plus第五版第五章编程练习 第6题
Problem : 设计一个名为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>
using namespace std;
struct car
{
char maker[20];
int year;
};
int main()
{
cout << "How many cars do you wish to catalog? ";
int num;
(cin >> num).get();
car *p =new car[num];
for (int i=0;i<num;i++)
{
cout << "Car #" << i+1 << ":\n";
cout << "Please enter the make: ";
cin.getline(p[i].maker,20);

cout << "Please enter the year made: ";
(cin >> p[i].year).get();
}
cout << "Here is your collection: \n";
for (int i=0;i<num;i++)
cout << p[i].year << "\t" << p[i].maker << endl;
system("pause");
delete p;
return 0;
}
/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/18
From : C++ Primer Plus第五版第五章编程练习 第7题
Problem : 编写一个程序,它使用一个char数组和循环来每次读取一个单词,直到用户输入done为止。随后,该程序指出用户输入
了多少个单词(不包括done在内)。下面是该程序的运行情况:
Enter words (to stop, type the word done):
anteater birthday category dumpster
envy finagle geometry done for sure
You entered a toal of 7 words.
您应该在程序中包含头文件cstring,并使用函数strcmp()来进行比较测试
*******************************************************************************************************************/
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char str[20];
cout << "Enter words (to stop, type the word done): \n";
int num=0;
while(cin >> str && strcmp(str,"done"))
num++;
cout << "You entered a toal of " << num <<" words. \n";
system("pause");
return 0;
}
/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/18
From : C++ Primer Plus第五版第五章编程练习 第8题
Problem : 编写一个满足前一个练习中描述的程序,但使用string对象而不是
字符数组。请在程序中包含头文件string,并使用关系运算符来进行比较测试
*******************************************************************************************************************/
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
cout << "Enter words (to stop, type the word done): \n";
int num=0;
while(cin >> str && (str!="done"))
num++;
cout << "You entered a toal of " << num <<" words. \n";
system("pause");
return 0;
}
/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/18
From : C++ Primer Plus第五版第五章编程练习 第9题
Problem :  编写一个使用嵌套循环的程序,要求用户输入一个值,指出要显示多少行。然后,程序将显示相应的行数的星号,其中
第一行包括一个星号,第二行包括两个星号,依次类推。每一行包含的字符数等于用户指定的行数,在星号不够的情况下,在星号
前面加上句点。该程序的运行情况如下:
Enter number of rows: 5
....*
...**
..***
.****
*****
*******************************************************************************************************************/
#include<iostream>
using namespace std;
int main()
{
cout << "Enter number of rows: \n";
int row;
cin >> row;
for (int i=0; i<row ;i++)
{
for (int j=0; j<row-i-1 ;j++)
{
cout << ".";
}
for (int j=row-i-1; j<row ;j++)
{
cout << "*";
}
cout << endl;
}
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: