C++ Primer Plus(第六版)中文版第五章课后答案

/*
//5.9.1
#include<iostream>
using namespace std;
int Sum(int m, int n) {
    int sum = 0;
    for (int i = m;i <= n;i++) {
        sum += i;
}
    return sum;
};
int main() {
    int m, n;
    cout << "Enter two number m,n :";
    cin >> m;
    cin >> n;
    cout << "The sum is : " << Sum(m, n) << endl;
    return 0;
}
*/

/*
//5.9.2
#include<iostream>
#include<array>
using namespace std;
int main() {
    const int ArSize = 101;
    array<long double, ArSize>factorials;
    factorials[0] = factorials[1] = 1L;
    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;
    }
    return 0;
}
*/
/*
//5.9.3
#include<iostream>
using namespace std;
int main() {
    float a=1;
    float b = 0;
    while (a!=0) {
        cout << "Enter a number: ";
        cin >> a;
        b = b + a;
        cout << "Now the sum numbers is " << b << endl;
    }
    return 0;
}
*/
/*
//5.9.4
#include<iostream>
using namespace std;
int main() {
    int n=1;
    float Daphne = 110;
    float Cleo = 105;
    while (Cleo < Daphne) {
        Daphne = Daphne + 10;
        Cleo = Cleo * (1.05);
        n += 1;
    }
    cout << n << endl;
    cout << Daphne << endl;
    cout << Cleo << endl;
    return 0;
}
*/
/*
//5.9.5
#include<iostream>
using namespace std;
int main() {
    string sale[] = {"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};
    int Sale[12];
    int sum=0;
    for (int i = 0;i < 12;i++) {
        cout << "Please input the " << sale[i] << "'s sale: ";
        cin >> Sale[i];
        sum=sum+Sale[i];
    }
    cout << "This year's sale is: " << sum << endl;
    return 0;
}
*/
/*
//5.9.6
#include<iostream>
using namespace std;
int main() {
    string sale[]= {"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" };
    int Sale[3][12];
    int sum = 0;
    for (int j = 0;j < 3;j++) {
        for (int i = 0;i < 12;i++) {
            cout << "Please input the " << sale[i] << "'s sale of the " << j+1 << " year: ";
            cin >> Sale[j][i];
            sum = sum + Sale[j][i];
        }
    }
    cout << "This year's sale is: " << sum << endl;
    return 0;
}
*/

/*
//5.9.7
#include <iostream>
#include<string>
using namespace std;

struct car {
    string manufacturer;
    int year;
};

int main() {
    int car_num;
    car* cars;

    cout << "How many cars do you wish to catalog?";
    cin >> car_num;
    cin.get();//读取字符
    cars = new car[car_num];

    for (int i = 0; i < car_num; i++) {
        cout << "Car #" << (i + 1) << endl;
        cout << "Please enter the make:";
        getline(cin, cars[i].manufacturer);
        cout << "Please enter the year made:";
        cin >> cars[i].year;
        cin.get();
    }

    cout << "Here is your collection:" << endl;
    for (int i = 0; i < car_num; i++) {
        cout << cars[i].year << " " << cars[i].manufacturer << endl;
    }
    delete[] cars;
    return 0;
}
*/

/*
//5.9.8
#include<iostream>
#include<string>
using namespace std;
int main() {
    int sum = 0;
    char words[256];
    cout << "Enter words (to stop, type the word done):" << endl;
    while (strcmp("done", words) != 0) {
        sum += 1;
        cin >> words;
    }
    cout << "You entered a total of " << sum-1 << " words." << endl;
    return 0;
}
*/
/*
//5.9.9
#include<iostream>
#include<string>
using namespace std;
int main() {
    int sum = 0;
    string words;
    cout << "Enter words (to stop, type the word done):" << endl;
    while (words != "done") {
        sum += 1;
        cin >> words;
    }
    cout << "You entered a total of " << sum - 1 << " words." << endl;
    return 0;
}
*/
/*
//5.9.10
#include<iostream>
using namespace std;
int main() {
    int m;
    cout << "Please input a number: ";
    cin >> m;
    for(int i=0;i<m;i++){
        int n = 0;
        for (int j = 0;j < m - i-1;j++) {
            cout << " . ";
            n += 1;
        }
        for (int j = 0;j < m - n;j++) {
            cout << " * ";
        }
        cout << endl;
    }
    return 0;
}
*/

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值