C++ Prime Plus 第六版 第五章 循环和关系表达式 编程练习 参考答案 详细版

以下答案本人在linux vscode中均已亲自测试编译通过,完美运行.

5.1

#include<iostream>
using namespace std;
int main()
{
    int i,num1,num2,sum=0;
    cout << "请输入两个整数:";
    cin >> num1 >> num2;
    for(i= num1;i <= num2;i++)
        sum += i;
    cout << num1 << "~" << num2 <<"之间所有的整数和 sum = " << sum << endl;
}

5.2

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

5.3

#include<iostream>
using namespace std;
int main()
{
    int num, sum = 0;
    cout << "请输入一个数字:";
    cin >> num;
    while(num != 0)
    {
        sum += num;
        cout << "到目前为止所有输入数的累积和 sum = " << sum << endl;
        cout << "请输入一个数字:";
        cin >> num;
    }
}

5.4

#include<iostream>
using namespace std;
const double factor1 = 0.10;
const double factor2 = 0.05;
int main()
{
    int year = 0; 
    double Dmoney = 100, Cmoney = 100;
    double Dprofit = Dmoney * factor1;
    while(Dmoney >= Cmoney)
    {
        year++;
        Dmoney += Dprofit;
        Cmoney += Cmoney * factor2;
    }
    cout << "Daphne money:" << Dmoney << endl;
    cout << "Cleo money:" << Cmoney << endl;
    cout << "year:" <<  year << endl;
}

5.5

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string month[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    int Salesvolume[12] = {};
    int sum = 0;
    for(int i=0; i < 12; i++)
    {
        cout << "The Salesvolume of " << month[i] << " :";
        cin >> Salesvolume[i];
        sum += Salesvolume[i];
    }
    cout << "A total of " << sum << " books have been sold this year" << endl;
}

5.6


#include<iostream>
#include<string>
using namespace std;
int main()
{
    string month[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    int Salesvolume[3][12] = {};
    int sum[4] = {};  //s[0]表示3年总销量,s[1]表示第一年总销量,s[2]表示第二年总销量,s[3]表示第三年总销量
    for(int i=0; i < 3; i++)
    {
        cout << "The " << i+1 << " year:" << endl;
        for(int j=0; j < 12; j++)
        {
            cout << "The Salesvolume of " << month[j] << " :";
            cin >> Salesvolume[i][j];
            if(i == 0)
                sum[1] += Salesvolume[i][j];
            if(i == 1)
                sum[2] += Salesvolume[i][j];
            if(i == 2)
                sum[3] += Salesvolume[i][j];
            sum[0] += Salesvolume[i][j];
        }
    }
    cout << "Total sales in the first year :" <<  sum[1] << endl;
    cout << "Total sales in the second year :" <<  sum[2] << endl;
    cout << "Total sales in the third year :" <<  sum[3] << endl;
    cout << "Total sales over three years :" <<  sum[0] << endl;
}

5.7

#include<iostream>
#include<string>
using namespace std;
struct car
{
    string manufacturer; //生产商
    int ProductionYear;  //生产年份
};
int main()
{
    int number;   //记录的数量
    cout << "How many cars do you wish to catclog ? ";
    cin >> number;  
    car * Cars = new car [number];
    for(int i=0; i < number; i++ )
    {
        cout << "Car #" << i+1 << " :" << endl;
        cout << "Please enter the made :";
        cin.get(); //读取上次输入留下的换行符,没有这句以下输入会自动跳过输入
        getline(cin,Cars[i].manufacturer);  
        //cin类中没有处理参数是string类型的方法(函数),因此不能用cin.getline() 或者 cin.get(), 因此用string类整行读取函数getline(cin,str)2
        cout << "Please enter the year made :";
        cin >> Cars[i].ProductionYear;
    }
    cout << "Here is your collection:\n";
    for(int i=0; i < number; i++ )
        cout << Cars[i].ProductionYear << "  " << Cars[i].manufacturer << endl;
    delete [] Cars;
}

5.8

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    const int size = 10;
    char word[size]{};
    int n = 0;
    cout << "Enter words (to stop ,type the word done) :" << endl;
    cin >> word;
    while(strcmp(word,"done") != 0)
    {
        cin >> word;
        n++;
    }
    cout << "Your entered a total of " << n << " words.\n";
}

5.9

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

5.10

#include<iostream>
using namespace std;
int main()
{
    int m;
    cout << "请输入行数:";
    cin >> m;
    for(int i = 0; i < m; i++ )
    {
        for(int k = 0; k < m-1-i; k++)
            cout << ".";
        for(int j = 0; j < i+1; j++)
            cout << "*";
        cout << endl;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值