C++ primer plus 第六版 第五章 编程练习答案

第五章 编程练习答案

1.

#include

int main()
{
    using namespace std;
    int i, a, b, sum;

    cout << "Enter the smaller number: " ;
    cin >> a;
    cout << "Enter the biger number: " ;
    cin >> b;   
    sum = 0;
    for(i = a ;i <= b ;i++)
        sum += i;
    cout << "The sum is: " << sum << endl; 
    return 0;

}

2.

第二题要使用模板类array(C++11),编译器版本比较老不支持

3.

#include

int main()
{
    using namespace std;
    int n, sum = 0;
    cin >> n;
    while(n!=0){
        sum += n;       
    cout << "The sum is: " << sum << endl; 
    cin >> n;       
    }   
    return 0;   
}

4.

#include

int main()
{
    using namespace std;
    const double money = 100;
    const double D_interest = 0.1;
    const double C_interest = 0.05;
    int i;
    double Daphne, Cleo;
    Daphne = money;
    Cleo = money;

    for(i=0 ;Daphne>=Cleo ;i++)
    {
        Daphne += D_interest * money;
        Cleo += C_interest * Cleo;
    }
    cout << "Year:" << i << endl;
    cout << "Daphne's money: " << Daphne << endl;
    cout << "Cleo's money: " << Cleo << endl;
    return 0;   
}

5.

#include
#include 

int main()
{
    using namespace std;

    int i, sum = 0;
    int sales[12];
    const string months[] = {
    "1月", "2月", "3月", "4月", "5月", 
    "6月", "7月", "8月", "9月", "10月", "11月", "12月"};

    cout << "输入每月销售量: " << endl;
    for(i = 0; i < 12; i++)
    {
        cout << months[i] << ":";
        cin >> sales[i];
        sum += sales[i];
    }
    cout << "这一年的销售数量:" << sum << endl; 
    return 0;
}

6.

#include
#include 

int main()
{
    using namespace std;

    int i, j, all = 0;
    int sum[3]={
    0, 0, 0}; 
    int sales[3][12];//二维数组 
    const string years[] = {
    "第一年", "第二年", "第三年"};
    const string months[] = {
    "1月", "2月", "3月", "4月", "5月", 
    "6月", "7月", "8月", "9月", "10月", "11月", "12月"};

    cout << "输入三年中每月销售量: " << endl;
    for(j = 0;j < 3; j++){
        for(i = 0;i < 12; i++){
            cout << years[j] << months[i] << "销售量:";
            cin >> sales[j][i];
            sum[j] += sales[j][i];
        }
        all += sum[j];
    }

    for(j = 0;j < 3; j++)
        cout << years[j] << "销售总量:" << sum[j] << endl;
    cout << "这三年的销售数量:" << all << endl;

    return 0;
}

7.

#include
struct car
{
    char name[20];
    int year;

};

int main()
{
    using namespace std;
    int i, n;
    cout << "How many cars do you wish to catalog?";
    cin >> n;
    cin.get();

    car* ps = new car[n];//创建动态数组   

    for(i = 0; i < n; i++){
        cout << "Car #" << i+1 <<": \n";
        cout << "Please enter the make: ";
        cin.get(ps[i].name, 20);//读取字符串 
        cout << "Please enter the year made: ";
        cin >> ps[i].year;
        cin.get();//读取数值                    
    }

    cout << "Here is your collection: \n";
    for(i = 0; i < n; i++)
        cout << ps[i].year << " " << ps[i].name << endl;

    delete ps;//释放指针指向的内存 
    return 0;

}

8.

#include
#include

int main()
{
    using namespace std;
    char ch[30];
    int count = 0;
//const char* const Done = "done";另一种方法,设定常量字符 

    cout << "Enter words (to stop, type the word done):" << endl;
    while(strcmp(ch, "done"))
    {
        count++;
        cin >> ch;
    }

    cout << "You entered a total of " << count-1 << " words." << endl;
    return 0;

}

9.

#include
#include

int main()
{
    using namespace std;
    string ch;
    int count = 0;
    string Done = "done";

    cout << "Enter words (to stop, type the word done):" << endl;
    while(Done != ch)
    {
        count++;
        cin >> ch;

    }

    cout << "You entered a total of " << count-1 << " words." << endl;
    return 0;

}

10.

#include

int main()
{
    using namespace std;
    int i, j, n;

    cout << "Enter number of rows: ";
    cin >> n;

    for(i = 1; i<=n; i++){
        for(j=0;jcout << ".";
        for( ; jcout << "*" ;
        cout << endl;   
    }
    return 0;
}

你可能感兴趣的:(C++,课后练习,C++,编程练习题)