c++ primer plus(第6版)中文版 第五章编程练习答案

来源:互联网 发布:快递哥软件 编辑:程序博客网 时间:2024/06/05 06:43

第五章编程练习答案

5.1输入2个整数,输出这2个之间(包括这两个)所有整数和

//5.1输入2个整数,输出这2个之间(包括这两个)所有整数和#include <iostream>using namespace std;int main () {     intn1,n2,sum=0;cout << "输入区间:";cin >> n1 >> n2;if(n1>n2)  swap(n1,n2);for (int i = n1; i <= n2; ++i)sum += i;cout << "[" << n1 << ", " << n2 << "]区间总和为" << sum << endl;} 

5.2用array和long double编写程序计算100!的和

//5.2用array和long double编写程序计算100!的和(array为c++11新加标准)#include <iostream>#include <array>using namespace std;int main () {     array<long double, 100>factorials;factorials[1] = factorials[0] = 1.0L;for (unsigned i = 2; i <= 100; ++i) factorials[i] = i * factorials[i-1];for (unsigned i = 0; i <= 100; ++i)cout << i << "! = "<< factorials[i] << endl;} 

5.3编写一个整数输入程序,每输入一个数,显示累计和,输入0停止

//5.3编写一个整数输入程序,每输入一个数,显示累计和,输入0停止#include <iostream>using namespace std;int main () {int n,sum = 0;cout << "输入数字:";do {cin >> n;sum += n;cout << "累计和为:" << sum << endl;} while (n != 0);} 

5.4Daphne以利息=0.10*原始存款存100美元,Cleo以利息=0.05*当前存款存100美元,求多少年后Cleo的财富将超过Daphne,并显示各自的价值

//5.4Daphne以利息=0.10*原始存款存100美元,Cleo以利息=0.05*当前存款存100美元,//    求多少年后Cleo的财富将超过Daphne,并显示各自的价值#include <iostream>using namespace std;int main () {     const double interest1 = 0.10,interest2 = 0.05;const double Base = 100;unsignedyears = 0;doubleDaphneTotal = Base, CleoTotal = Base;cout << "年份" << "   Daphne" << "   " << "Cleo" << endl;do {DaphneTotal += interest1*Base;CleoTotal += CleoTotal *interest2;++years;cout << years << ":     " << DaphneTotal << "     " << CleoTotal << endl;} while (DaphneTotal >= CleoTotal);cout << years << "年后," << "Cleo的财富将超过Daphne,前者将达到$" << CleoTotal << "、后者$" << DaphneTotal << endl;} 

5.5编写一个程序输入每个月的书销售情况,最后显示一年总共销售。

//5.5编写一个程序输入每个月的书销售情况,最后显示一年总共销售。#include <iostream>#include <string>using namespace std;int main () {    unsignedsales[12],sum=0;const string months[] = {"1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"};cout << "输入每月销售量。" << endl;for (int i = 0; i < 12; ++i) {cout << months[i] << ":";cin >> sales[i];}for (int i = 0; i < 12; ++i) {sum += sales[i];}cout << "全年销售总量" << sum << endl;} 

5.6在5.5的基础上,使用二维数组,计算3年每年的总共销售量和总量

//5.6在5.5的基础上,使用二维数组,计算3年每年的总共销售量和总量#include <iostream>#include <string>using namespace std;int main () {     unsignedsales[3][12],sum=0,allsum=0;const string months[] = {"1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"};for (int i = 0; i < 3; ++i) {cout << "输入第" << i + 1 << "年每月销售量。"<< endl;for (int j = 0; j < 12; ++j) {cout << months[j] << ":";cin >> sales[i][j];}}for (int i = 0; i < 3; ++i) {for (int j = 0; j < 12; ++j) {sum += sales[i][j];}allsum += sum;cout << "第" << i + 1 << "年销售总量" << sum << endl;}cout << "3年销售总量" << allsum << endl;} 

5.7设计一个car结构(包括生产商和生产日期),先询问有几辆车,然后由用户输入,最后显示每个结构体内容

//5.7设计一个car结构(包括生产商和生产日期),先询问有几辆车,然后由用户输入,最后显示每个结构体内容#include <iostream>#include <string>using namespace std;struct Car {string maker;unsignedyear;};int main () {     cout << "How many cars do you wish to catalog? ";unsignednum;cin >> num;cin.get();Car*CarsList = new Car [num];for (unsigned i = 0; i < num; ++i) {cout << "Car #" << i+1 << ": " << endl;cout << "Please enter the make: ";getline(cin, CarsList[i].maker);cout << "Please enter the year made: ";cin >> CarsList[i].year;cin.get();}cout << "Here is your collection: " << endl;for (unsigned i = 0; i < num; ++i) cout << CarsList[i].year << " " << CarsList[i].maker << endl;delete [] CarsList;} 

5.8输入一句话(以done结束),显示总共的词数(用char数组)

//5.8输入一句话(以done结束),显示总共的词数(用char数组)#include <iostream>#include <cstring>using namespace std;int main () {     cout << "Enter words (to stop, type the word done): ";const char* const Done = "done";int n=0;char word[30];while (strcmp(Done, word)){n++;cin >> word;} cout << "You entered a total of " << n-1 << " words." << endl;} 

5.9输入一句话(以done结束),显示总共的词数(用string对象)

//5.9输入一句话(以done结束),显示总共的词数(用string对象)#include <iostream>#include <cstring>using namespace std;int main () {     cout << "Enter words (to stop, type the word done): ";string Done = "done";int n=0;string word;while (Done!=word){n++;cin >> word;} cout << "You entered a total of " << n-1 << " words." << endl;} 

5.10输出图形,n行n列(第i(i<n)行有m-n个.和m个*)

//5.10输出图形,n行n列(第i(i<n)行有m-n个.和m个*)#include <iostream>using namespace std;int main () {int n;cout << "Enter number of rows : " ;cin >> n;for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){if(j<=n-i)  cout << ".";else cout << "*";}cout << endl;}}


0 0