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

第四题:利率问题我查了百度 单利的意思就是说比如你投资100 利率10% 那么你每年得到的钱都是10

而复利的意思是利滚利比如投资100那么第一年是105 第二年会更多第三年还会再加更具你的钱多少来增加 单利永远都是固定的数目

在第八题和第九题也可以使用&&这两个运算符来使用不过我们还没有学到这个知识暂时不使用这个知识

就用目前学过的知识来完成

第一题

输入的时候请按这样的格式:2 9要加空格2空格9

#include<iostream>

int main()
{
    using namespace std;
    int i, num, count{0};

    cout << "输入两个整数: ";
    cin >> i >> num;

    for (int number = i; number <= num; number++)
        count += number;
    cout << count;
    return 0;
}

第二题

#include<iostream>
#include<array>

const int ArSize = 100;

int main()
{
    std::array<long double, ArSize + 1>factorials;
    using namespace std;
    factorials[0] = factorials[1] = 1L;

    cout << 0 << "! = " << factorials[0] << endl;
    cout << 1 << "! = " << factorials[1] << endl;

    for (int i = 2; i <= ArSize; i++)
    {
        factorials[i] = i * factorials[i - 1];
        cout << i << "! = " << factorials[i] << endl;
    }

    return 0;
}

第三题

#include<iostream>
int main()
{
    int num;
    int count{ 0 };

    std::cin >> num;

    while (num != 0)
    {
        count += num;
        std::cout  << "输入和:" << count << std::endl;
        std::cin >> num;
    }

    return 0;
}

第四题

#include<iostream>

const double num_daphne{ 0.10 };
const double num_cleo{ 0.05 };
const int num = 100;

int main()
{
    using namespace std;
    double i_cleo{ 100 };
    double i_daphne{ 100 };
    int i = 0;

    while (i_daphne >= i_cleo)
    {
        i_daphne += num * num_daphne;
        i_cleo += i_cleo * num_cleo;
        i++;
    }

    cout << i << "年后Cleo投资价值超过Daphne\n"
         << "Cleo = " << i_cleo << " Daphne = "
         << i_daphne;

    return 0;
}

第五题

#include<iostream>
#include<string>
#include<array>

const int month = 12;

int main()
{
    using namespace std;
    string str_your[month]
    {
        "January",//1
        "February",//2
        "March",//3
        "April",//4
        "May",//5
        "June",//6
        "July",//7
        "August",//8
        "September",//9
        "October",//10
        "November",//11
        "December"//12
    };

    array<int, month>array;
    int num_ber = 0;

    for (unsigned i = 0; i < month; i++)
    {
        cout << str_your[i] << ": ";
        cin >> array[i];
        num_ber += array[i];
    }

    cout << "这一年销售了:" << num_ber << "本书\n";

    return 0;
}

第6题

#include<iostream>
#include<string>
#include<array>
 
const int ArSize = 12;
const int year = 3;
 
int main()
{
    using namespace std;
    int arr[year]{ 0 };
    int array[year][ArSize];
    string str_your[ArSize]
    {
        "January","February","March",
        "April","May","June",
        "July","August","September",
        "October","November","December"
    };
    
    for (unsigned i = 0; i < year; i++)
    {
        for (unsigned j = 0; j < ArSize; j++)
        {
            cout << i + 1 << " year " << str_your[j] << " sales: ";
            cin >> array[i][j];
            arr[i] += array[i][j];
        }
    }
    //这里你们可以自己改成循环来完成
    unsigned int i = 0;
    cout << "clause " << 1 << " year sales: " << arr[0] << endl;
    cout << "clause " << 2 << " year sales: " << arr[1] << endl;
    cout << "clause" << 3 << " year salse: " << arr[2] << endl;
    cout << 3 << " year salse: " << arr[0] + arr[1] + arr[2];
 
    return 0;
}

第七题

#include<iostream>
#include<string>

struct car
{
    std::string manufact;
    int num_year;
};

const int car_num = 3;

int main()
{
    using namespace std;
    car* car_year = new car[car_num];

    cout << "How many cars do you wish to catalog? 2\n";

    for (int i = 0; i < car_num; i++)
    {
        cout << "Car #" << i + 1 << ":\n" << "Please enter the manufacturer: ";
        getline(cin, car_year[i].manufact);
        cout << "Please enter the year of manufacture: ";
        cin >> car_year[i].num_year;//进行输入数字时按下Enter会留下换行 在getline()读取时会读取到留下的换行字符  
        cin.get();//进行丢弃字符处理(C处理方式)
    }

    for (int i = 0; i < car_num; i++)
    {
        cout << car_year[i].num_year << " " 
             << car_year[i].manufact << endl;
    }


    delete[] car_year;
    return 0;
}

第八题

#include<iostream>
#include<cstring>

int main()
{
    using namespace std;
    char str[80];
    char str1[80];
    int count = 0;

    cout << "Enter words (to stop, type the word done)\n";

    cin >> str;
    while (strcmp(str,"done") != 0)
    {
        count++;
        cin >> str;//利用其每次读取单词的特性
    }

    cout << "You entered a total of " << count << " words.";

    return 0;
}

第九题

#include<iostream>
#include<string>
int main()
{
    using namespace std;
    string str;
    int count = 0;

    cout << "Enter words (to stop,type the word done):\n";

    cin >> str;
    while (str != "done")
    {
        cin >> str;
        count++;
    }

    cout << "You enteren a total of  " << count << " words.";
    return 0;
}

第十题

#include<iostream>
int main()
{
    using namespace std;
    int count;

    cout << "Enter number of rows: ";
    cin >> count;
    int i,j;

    for (i = 1; i <= count; i++)
    {
        for (j = 1; j <= count - i; j++)
            cout << ".";
        for (int g = j; g <= count; g++)
            cout << "*";

        cout << endl;
    }

    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值