• c++primer 第五章编程练习答案


    5.9.1

    #include<iostream>
    int main() {
        using namespace std;
        int one, two, temp, sum = 0;
        cout << "input first interger: ";
        cin >> one;
        cout << "input second interger: ";
        cin >> two;
        for (temp = one; temp <= two; ++temp)
            sum += temp;
        cout << "Add from " << one << " to " << two << " is " << sum;
    }

    5.9.2

    #include<iostream>
    #include<array>
    
    const int ArSize = 16;
    
    int main() {
        using namespace std;
        array<long double, ArSize> factorials;
        factorials[1] = factorials[0] = 1.0;
        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.9.3

    #include<iostream>
    
    int main() {
        using namespace std;
        int number, sum = 0;
        cout << "Please input interger number,end with 0." << endl;
        cin >> number;
        while (number) {
            sum += number;
            cout << "Now,sum is " << sum << endl;
            cin >> number;
        }
    }

    5.9.4

    #include<iostream>
    
    const double interest_C = 0.05;
    const double interest_D = 0.10;
    
    int main() {
        using namespace std;
        double deposit_D, deposit_C;
        deposit_C = deposit_D = 100.0;
        int year = 0;
        do {
            deposit_C *= 1.05;
            deposit_D += (0.1 * 100);
            ++year;
        } while (deposit_C <= deposit_D);
        cout << "deposit_C is " << deposit_C << endl;
        cout << "deposit_D is " << deposit_D << endl;
        cout << year << " years C's deposit more than D";
    }

    5.9.5

    #include<iostream>
    #include<string>
    
    int main() {
        using namespace std;
        string months[12] = {
            "一月",
            "二月",
            "三月",
            "四月",
            "五月",
            "六月",
            "七月",
            "八月",
            "九月",
            "十月",
            "十一月",
            "十二月",
        };
        int sales[12];
        int sum = 0;
        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;
    }

    5.9.6

    #include<iostream>
    #include<string>
    
    int main() {
        using namespace std;
        string months[12] = {
            "一月",
            "二月",
            "三月",
            "四月",
            "五月",
            "六月",
            "七月",
            "八月",
            "九月",
            "十月",
            "十一月",
            "十二月",
        };
        int sales[3][12];
        int sum_years[3] = { 0,0,0 };
        int sum = 0;
        for (int i = 0; i < 3; ++i) {
            for (int j = 0; j < 12; j++) {
                cout << "请输入第" << i + 1 << "年," << "" << months[j] << "的销售额: ";
                cin >> sales[i][j];
            }
        }
        for (int i = 0; i < 3; i++) {
            cout << "" << i+1 << " 年销售额为:";
            for (int j = 0; j < 12; j++) {
                sum_years[i] += sales[i][j];
            }
            cout << sum_years[i] << endl;
        }
        cout << "三年的总销售额为:";
        for (int i = 0; i < 3; i++) {
            sum += sum_years[i];
        }
        cout << sum;
    }

    5.9.7

    #include<iostream>
    #include<string>
    
    using namespace std;
    struct car
    {
        string manufacturer;
        int production_date;
    };
    
    int main() {
        int car_number;
        cout << "How many cars do you have? ";
        cin >> car_number;
        car *cars = new car[car_number];
        for (int i = 0; i < car_number; i++) {
            cout << "Car # " << i + 1 << endl;
            cout << "Please enter the make: ";
            cin >> cars[i].manufacturer;
            cout << "Please enter the year made: ";
            cin >> cars[i].production_date;
        }
        cout << "Here is your collection:
    ";
        for (int i = 0; i < car_number; i++) {
            cout << cars[i].production_date << "	" << cars[i].manufacturer << endl;
        }
    }

    5.9.8

    #include<iostream>
    #include<cstring>
    
    int main() {
        using namespace std;
        char charr[20];
        cout << "Enter words (to stop,type the word done):
    ";
        int sum;
        for (sum=0;strcmp(charr,"done");sum++)
        {
            cin >> charr;
        }
        cout << "for sure
    ";
        cout << "You enter a total of " << sum-1 << " words";
    }

    5.9.9

    #include<iostream>
    #include<string>
    
    int main() {
        using namespace std;
        string charr;
        cout << "Enter words (to stop,type the word done):
    ";
        int sum;
        for (sum = 0; charr!="done"; sum++)
        {
            cin >> charr;
        }
        //cout << "for sure
    ";
        cout << "You enter a total of " << sum - 1 << " words";
    }

    5.9.10

    #include<iostream>
    
    int main() {
        using namespace std;
        int rows;
        cout << "Enter number of rows: ";
        cin >> rows;
        char **charr = new char*[rows];//开辟行
        for (int i = 0; i < rows; i++)
        {
            *(charr+i) = new char[rows];//开辟列
        }
        for (int i = 0; i < rows; i++) {
            for (int j = rows - 1; j >= 0; j--) {
                if ((j + i) >= 4)
                    charr[i][j] = '*';
                else
                    charr[i][j] = '.';
            }
        }
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < rows; ++j)
                cout << charr[i][j];
            cout << endl;
        }
        //cout << charr[2][0];
    }
  • 相关阅读:
    单一index.php实现PHP任意层级文件夹遍历(原创自Zjmainstay)
    php读取文件内容至字符串中,同时去除换行、空行、行首行尾空格(原创自Zjmainstay)
    php获取页面并切割页面div内容
    jQuery单击双击实现链接的提取、插入与删除
    PHP 利用AJAX获取网页并输出(原创自Zjmainstay)
    php 数组首字符过滤功能
    点击图片添加文件在Chrome中使用的兼容问题
    php读取txt文件组成SQL并插入数据库(原创自Zjmainstay)
    为博客园添加标签云动画
    jQuery动态增删改查表格信息,可左键/右键提示(原创自Zjmainstay)
  • 原文地址:https://www.cnblogs.com/zhang293/p/7535431.html
Copyright © 2020-2023  润新知