zoukankan      html  css  js  c++  java
  • c++ primer plus 第五章 课后题答案

    #include <iostream>
    using namespace std;
    
    int main()
    {
        int num_1,num_2,sum=0;
    
        cout << "Please enter two number: ";
        cin >> num_1;
        cin >> num_2;
    
        if (num_1 > num_2)
        {
            int a;
            a = num_1;
            num_1 = num_2;
            num_2 = a;
        }
    
        for (int i = num_1; i <= num_2; i++)
        {
            sum += i;
        }
        cout << "The sum of num_1-num_2 is " << sum << endl;
    
        system("pause");
    }

      

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

     

    #include <iostream>
    #include <array>
    using namespace std;
    
    int main()
    {
        int a,b=0;
        cout << "Please enter a number other than zero: ";
        do
        {
            cin >> a;
            if (a == 0)
                cout << "Stop summing!
    ";
            else
            {
                b = b + a;
                cout << "The sum of the numbers already entered is: " << b << endl;
            }
            
        } while (a != 0);
        system("pause");
    }

    #include <iostream>
    using namespace std;
    const float lixi1 = 0.1;
    const float lixi2 = 0.05;
    
    int main()
    {
        float Daphne_i = 100, Cleo_i = 100, Daphne = Daphne_i, Cleo = Cleo_i;
        int year = 0;
    
        while (Daphne >= Cleo)
        {
            Daphne = Daphne + Daphne_i * lixi1;
            Cleo = Cleo + Cleo * lixi2;
            year += 1;
    //        cout << Daphne << "," << Cleo<<"--"<< year<<endl;
        } 
        cout << "In " << year << "th years, Cleo's funds exceed Daphne.
    At this time, the funds of Cleo are " << Cleo << ", and the funds of Daphne are " << Daphne << ".
    ";
        system("pause");
    }

    #include <iostream>
    using namespace std;
    const int month = 12;
    
    int main()
    {
        int num[month];
        int sum = 0;
        const char* months[month] = { "January","February","March","April","May","June","July","August","September","October","November","December" };
    
        cout << "Please enter the monthly sales volume:
    ";
        for (int i = 0; i < month; ++i)
        {
            cout << months[i] << " : ";
            cin >> num[i];
            sum += num[i];
        }
    
        cout << "The total sales this year is " << sum << ".
    ";
    
        system("pause");
    }

    #include <iostream>
    using namespace std;
    const int year = 3;
    const int month = 12;
    
    int main()
    {
        int num[year][month];
        int sum[4] = { 0,0,0,0 };
        const char* months[month] = { "January","February","March","April","May","June","July","August","September","October","November","December" };
    
        for (int j = 0; j < year; ++j)
        {
            cout << "Please enter the monthly sales volume of " << j + 1 << "th year:
    ";
            for (int i = 0; i < month; ++i)
            {
                cout << months[i] << " : ";
                cin >> num[j][i];
                sum[j] += num[j][i];
            }
            cout << "The total sales of " << j+1 << "th year is "<< sum[j] << ".
    ";
            sum[year] = sum[year] + sum[j];
        }
        
        cout << "The total sales of " << year << " years are "<<sum[year] << ".
    ";
    
        system("pause");
    }

    #include <iostream>
    #include <string>
    using namespace std;
    
    struct car
    {
        string make;
        int year;
    };
    
    int main()
    {
        int num;
    
        cout << "How many cars do you wish to catalog?";
        cin >> num;
        car *all_car = new car[num];
    
        for (int i = 0; i < num; ++i)
        {
            cout << "Car #" << i+1 << endl;
            cout << "Please enter the make : ";
            cin.ignore();
            getline(cin, all_car[i].make);
            cout << "Please enter the year made : ";
            cin >> all_car[i].year;
        }
    
        cout << "Here is your collection:
    ";
        for (int i = 0; i < num; ++i)
        {
            cout << all_car[i].year << " " << all_car[i].make << endl;
        }
        
    delete [] all_car; system(
    "pause"); }

    #include <iostream>
    #include <cstring>
    using namespace std;
    const int num_words = 10;
    
    int main()
    {
        char words[25];
        int s = 0;
        bool flag = true;
    
        cout << "Enter words (to stop, tpye the word done) :
    ";
        for (int i = 0; i < num_words; ++i)
        {
            cin >> words;
            if (flag && !strcmp(words, "done"))
                flag = false;
            if (flag && strcmp(words, "done"))
                s += 1;
    
        }
    
        cout << "Your entered a total of " << s << " words.
    ";
    
        system("pause");
    }

    #include <iostream>
    #include <string>
    using namespace std;
    const int num_words = 10;
    
    int main()
    {
        const string sstop_word = "done";
        string str;
        int s = 0;
        bool flag = true;
    
        cout << "Enter words (to stop, tpye the word done) :
    ";
        for (int i = 0; i < num_words; ++i)
        {
            cin >> str;
            if (flag && str== sstop_word)
                flag = false;
            if (flag && !(str == sstop_word))
                s += 1;
    
        }
    
        cout << "Your entered a total of " << s << " words.
    ";
    
        system("pause");
    }

    #include <iostream>
    using namespace std;
    
    int main()
    {
        int num;
        cout << "Enter number of rows: ";
        cin >> num;
    
        for (int i = 0; i < num; ++i)
        {
            for (int j = 1; j < (num - i); ++j)
                cout << ".";
            for (int j = 0; j < i; ++j)
                cout << "*";
            cout << endl;
        }
    
        system("pause");
    }
  • 相关阅读:
    MongoDB配置及解答mongodb cmd中安装及连接数据库 mongodb无法安装及连接数据库解答
    类封装版学生管理系统,连接数据库,增删改查,拿去用,不谢。
    python学生管理系统连接数据库版,很详细,这个是用函数版的增删改查,拿去用,不谢。
    京东双十一
    Ubuntu中以root权限运行软件sudo命令不输入密码
    Ubuntu 18.04中sudo运行的程序无法切换输入法中文输入问题
    UVA12558 埃及分数
    NOI1999 生日蛋糕
    Tarjan
    AC自动机
  • 原文地址:https://www.cnblogs.com/CJT-blog/p/10241486.html
Copyright © 2011-2022 走看看