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!\n";
        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.\nAt this time, the funds of Cleo are " << Cleo << ", and the funds of Daphne are " << Daphne << ".\n";
    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:\n";
    for (int i = 0; i < month; ++i)
    {
        cout << months[i] << " : ";
        cin >> num[i];
        sum += num[i];
    }

    cout << "The total sales this year is " << sum << ".\n";

    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:\n";
        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] << ".\n";
        sum[year] = sum[year] + sum[j];
    }
    
    cout << "The total sales of " << year << " years are "<<sum[year] << ".\n";

    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:\n";
    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) :\n";
    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.\n";

    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) :\n";
    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.\n";

    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");
}
posted @ 2019-01-08 21:16  深夜十二点三十三  阅读(588)  评论(0编辑  收藏  举报