1. 程式人生 > >C++程式設計(第二版)譚浩強----程式題課後習題答案第三章

C++程式設計(第二版)譚浩強----程式題課後習題答案第三章

2.

複製程式碼
#define _USE_MATH_DEFINES
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
    float r, h, l, S, S1, V, V1;
    cin >> r >> h;
    l = 2 * M_PI * r;
    S = M_PI * r * r;
    S1 = 4 * M_PI * r * r;
    V = 4 / 3 * M_PI * r * r * r;
    V1 
= S * h; cout << setiosflags(ios::fixed) << setprecision(2); cout << "圓周長 l = " << l << '\t' << "圓面積 S = " << S << endl; cout << "圓球表面積 S1 = " << S1 << '\t' << "圓球體積 V = " << V << endl; cout << "
圓柱體積 V1 = " << V1 << endl; return 0; }
複製程式碼

結果:

3.

複製程式碼
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    float F, c;
    cout << "華氏溫度 F = ";
    cin >> F;
    cout << setiosflags(ios::fixed) << setprecision(2);
    c = (5.0 / 9.0) * (F - 32
); cout << "攝氏溫度c = " << c << endl; return 0; }
複製程式碼

結果:

 4.

c1,c2定義為字元型

複製程式碼
#include <iostream>
using namespace std;
int main()
{
    char c1, c2;
    cout << "請輸入兩個字元";
    c1 = getchar();
    c2 = getchar();
    cout << "putchar 輸出結果";
    putchar(c1);
    putchar('\t');
    putchar(c2);
    putchar('\n');
    cout << "cout輸出結果";
    cout << c1 << '\t' << c2 << endl;
    return 0;
}
複製程式碼

結果:

c1,c2定義為整形

複製程式碼
#include <iostream>
using namespace std;
int main()
{
    int c1, c2;
    cout << "請輸入兩個字元";
    c1 = getchar();
    c2 = getchar();
    cout << "putchar 輸出結果";
    putchar(c1);
    putchar('\t');
    putchar(c2);
    putchar('\n');
    cout << "cout輸出結果";
    cout << c1 << '\t' << c2 << endl;
    return 0;
}
複製程式碼

結果:

定義為整形即可輸出ASCII碼。

8.

0

1

1

0

1

9.

複製程式碼
#include <iostream>
using namespace std;
int main()
{
    int compare(int x, int y, int z);
    int a, b, c, temp, max;
    cout << "輸入三個整數a b c" << endl;
    cin >> a >> b >> c;
    //第一種方法
    /*if ((a > b) && (a > c))
    cout << "最大的數為a = " << a << endl;
    else if((b>a)&&(b>c))
    cout << "最大的數為b = " << b << endl;
    else
    cout << "最大的數為c = " << c << endl;*/
    //第二種方法
    /*if(a<b)
    if(b<c)
    cout << "最大的數為c = " << c << endl;
    else
    cout << "最大的數為b = " << b << endl;
    else
    if(a<c)
    cout << "最大的數為c = " << c << endl;
    else
    cout << "最大的數為a = " << a << endl;*/
    //第三種方法
    /*temp = (a > b) ? a : b;
    max = (temp > c) ? temp : c;
    cout << "最大的數為 " << max << endl;*/
    max = compare(a, b, c);
    cout << "最大的數為 " << max << endl;
    return 0;
}
int compare(int x, int y, int z)
{
    int temp, max;
    temp = (x > y) ? x : y;
    max = (temp > z) ? temp : z;
    return max;
}
複製程式碼

結果:

10.

複製程式碼
#include <iostream>
using namespace std;
int main()
{
    int x, y;
    cout << "輸入 x = ";
    cin >> x;
    if (x < 1)
        y = x;
    else
        if (x >= 1 && x < 10)
            y = 2 * x - 1;
        else
            y = 3 * x - 11;
    cout << "輸出 y = " << y << endl;
    return 0;
複製程式碼

結果:

11.

複製程式碼
#include <iostream>
using namespace std;
int main()
{
    int grade;
    while (1)
    {
        cout << "輸入學生成績grade = ";
        cin >> grade;
        if (grade < 0 || grade > 100)
            cout << "成績輸入錯誤,請重新輸入!" << endl;
        else
            switch ((int)grade / 10)
            {
            case 10: cout << grade << "分成績等級為A" << endl; break;
            case 9: cout << grade << "分成績等級為A" << endl; break;
            case 8: cout << grade << "分成績等級為B" << endl; break;
            case 7: cout << grade << "分成績等級為C" << endl; break;
            case 6: cout << grade << "分成績等級為D" << endl; break;
            default: cout << grade << "分成績等級為E" << endl; break;
            }
    }
    return 0;
}
複製程式碼

結果:

12.

複製程式碼
#include <iostream>
using namespace std;
int main()
{
    long int a;
    cout << "輸入正整數 a = ";
    while (1)
    {
        cin >> a;
        if (a > 99999)
        {
            cout << "輸入錯誤,請重新輸入!" << endl;
            continue;
        }
        else if (a / 10000 > 0)
            cout << "a 為5位數 " << a / 10000 + (a % 10000) / 1000 * 10 + (a % 1000) / 100 * 100 + (a % 100) / 10 * 1000 + (a % 10) * 10000 << endl;
        else if (a / 1000 > 0)
            cout << "a 為4位數 " << a / 1000 + (a % 1000) / 100 * 10 + (a % 100) / 10 * 100 + (a % 10) * 1000 << endl;
        else if (a / 100 > 0)
            cout << "a 為3位數 " << a / 100 + (a % 100) / 10 * 10 + (a % 10) * 100 << endl;
        else if (a / 10 > 0)
            cout << "a 為2位數 " << a / 10 + (a % 10) * 10 << endl;
        else
            cout << "a 為1位數 " << a << endl;

    }
    return 0;
}
複製程式碼

結果:

13.

複製程式碼
#include<iostream>
using namespace std;
int main()
{
    float i;
    int c;
    while (1)
    {
        /*cout << "輸入當月利潤為 i = ";
        cin >> i;
        if (i <= 10)
        cout << "應發獎金為 " << i*0.1 << "萬元" << endl;
        else if (i <= 20 && i >= 10)
        cout << "應發獎金為 " << 10 * 0.1 + (i - 10)*0.075 << "萬元" << endl;
        else if (i <= 40 && i >= 20)
        cout << "應發獎金為 " << 10 * 0.1 + 10 * 0.075 + (i - 20)*0.05 << "萬元" << endl;
        else if (i <= 60 && i >= 40)
        cout << "應發獎金為 " << 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (i - 40)*0.03 << "萬元" << endl;
        else if (i <= 100 && i >= 60)
        cout << "應發獎金為 " << 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + (i - 60)*0.015 << "萬元" << endl;
        else
        cout << "應發獎金為 " << 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + 40 * 0.015 + (i - 100)*0.01 << "萬元" << endl;*/
        cout << "輸入當月利潤為 i = ";
        cin >> i;
        c = i / 10;
        if (c > 10) c = 10;
        switch (c)
        {
        case 0: cout << "應發獎金為 " << i*0.1 << "萬元" << endl; break;
        case 1: cout << "應發獎金為 " << 10 * 0.1 + (i - 10)*0.075 << "萬元" << endl; break;
        case 2:
        case 3: cout << "應發獎金為 " << 10 * 0.1 + 10 * 0.075 + (i - 20)*0.05 << "萬元" << endl; break;
        case 4:
        case 5: cout << "應發獎金為 " << 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (i - 40)*0.03 << "萬元" << endl; break;
        case 6:
        case 7:
        case 8:
        case 9: cout << "應發獎金為 " << 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + (i - 60)*0.015 << "萬元" << endl; break;
        case 10: cout << "應發獎金為 " << 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + 40 * 0.015 + (i - 100)*0.01 << "萬元" << endl; break;
        }

    }
    return 0;
複製程式碼

結果:

14.

複製程式碼
#include<iostream>
using namespace std;
int main()
{
    void sort(int m, int j, int k, int l);
    int a, b, c, d;
    cout << "請輸入四個整數 ";
    cin >> a >> b >> c >> d;
    sort(a, b, c, d);
    return 0;
}
void sort(int m, int j, int k, int l)
{
    int temp;
    if (m > j) { temp = m; m = j; j = temp; }
    if (m > k) { temp = m; m = k; k = temp; }
    if (m > l) { temp = m; m = l; l = temp; }
    if (j > k) { temp = j; j = k; k = temp; }
    if (j > l) { temp = j; j = l; l = temp; }
    if (k > l) { temp = k; k = l; l = temp; }
    cout << "從小到大順序輸出為:" << m << '\t' << j << '\t' << k << '\t' << l << '\t' << endl;
}
複製程式碼

結果:

15.

複製程式碼
#include<iostream>
using namespace std;
int main()
{
    int m, n, temp, p;
    while (1)
    {
        temp = 1;
        cout << "請輸入兩個正整數:";
        cin >> m >> n;
        p = m * n;
        while (temp != 0)
        {
            if (m > n)
            {
                temp = m % n;
                m = n; n = temp;
                if (temp == 0)
                {
                    cout << "m 和 n 的最大公約數為:" << m << endl;
                    cout << "m 和 n 的最小公倍數為:" << p / m << endl;
                }
            }
            else
            {
                temp = n % m;
                n = m; m = temp;
                if (temp == 0)
                {
                    cout << "m 和 n 的最大公約數為:" << n << endl;
                    cout << "m 和 n 的最小公倍數為:" << p / n << endl;
                }
            }
        }
    }
    return 0;
}
複製程式碼

結果:

16.

複製程式碼
#include <iostream>
using namespace std;
int main()

{
    char c;
    int l = 0, s = 0, d = 0, o = 0;
    cout << "請輸入一行字元" << endl;
    while (1)
    {
        while ((c = getchar()) != '\n')
        {
            if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
                l++;
            else if (c == ' ')
                s++;
            else if (c >= '0' && c <= '9')
                d++;
            else