C++ Primer Plus第六版 编程练习第六章

1.

#include <iostream>
#include <cctype>

int main()
{
    using namespace std;
    cout << "输入字符 (@停止): ";
    char ch;
    cin.get(ch);
    while (ch != '@')
    {
        if (isupper(ch))
            ch = tolower(ch);
        else if (islower(ch))
            ch = toupper(ch);
        if (!isdigit(ch))
            cout << ch;
        cin.get(ch);
    }
    cout << endl;
    return 0;
}

2.

#include <iostream>
#include <array>

const int Size = 10;

int main()
{
    using namespace std;
    array<double, Size> donations;
    cout << "输入数字(非数字时停止): ";
    double donation;
    int i, n;
    for (i = 0; (i < Size) && (cin >> donation); ++i) {
        donations[i] = donation;
        n = i + 1;
    }
    double sum;
    for (i = 0, sum = 0.0; i < n; ++i)
        sum += donations[i];
    double avg;
    avg = sum / (float)n;
    cout.precision(2);
    cout << "平均值为: " << avg << endl;
    cout << "比平均值大的数有:" ;
    for (i = 0; i < n; ++i)
    {
        if (donations[i] > avg)
            cout << donations[i] << " ";
    }
    cout << endl;
    return 0;
}

3.

#include <iostream>
#include <array>

const int Size = 10;

int main()
{
    using namespace std;
    char choice;
    cout << "Please enter one of the following choices:\n";
    cout << "c) carnivore           p) pianist\n";
    cout << "t) tree                g) game\n";
    while (cin >> choice)
    {
        switch (choice)
        {
        case'c':cout << "A tiger is a carnivore";
            exit(EXIT_FAILURE);
        case'p':cout << "Howard is a pianist";
            exit(EXIT_FAILURE);
        case't':cout << "A maple is a tree";
            exit(EXIT_FAILURE);
        case'g':cout << "Pubg is a game";
            exit(EXIT_FAILURE);
        default:cout << "Please  enter a c, p, t, or g: ";
        }
    }
    return 0;
}

4.

#include <iostream>

const int strsize = 20;
const int Num = 5;

struct bop {
    char fullname[strsize];
    char title[strsize];
    char bopname[strsize];
    int preference;
};

int main(void)
{
    using namespace std;
    bop bops[Num] = {
        {"Wimp Macho","C Programmer","WMPS",0},
        {"Raki Rhodes","Junior Programmer","RRW",1},
        {"Celia Laiter","Java Programmer","MIPS",2},
        {"Hoppy Hipman","Analyst Trainee","HHR",1},
        {"Pat Hand","Python Programmer","LOOPY",2}
    };
    cout << "Benevolent Order of Programmers Report\n";
    cout << "a. display by name       b.display by title\n"
            "c. display by bopname    d.display by perference\n"
        "q. quit\n";
    cout << "Enter your choice: ";
    char choice;
    cin >> choice;
    while (choice != 'q')
    {
        switch (choice)
        {
        case 'a':
            for (int i = 0; i < Num; i++)
                cout << bops[i].fullname << endl;
            break;
        case 'b':
            for (int i = 0; i < Num; i++)
                cout << bops[i].title << endl;
            break;
        case 'c':
            for (int i = 0; i < Num; i++)
                cout << bops[i].bopname << endl;
            break;
        case'd':
            for (int i = 0; i < Num; i++)
            {
                switch (bops[i].preference)
                {
                case 0: cout << bops[i].fullname << endl;
                    break;
                case 1: cout << bops[i].title << endl;
                    break;
                case 2: cout << bops[i].bopname << endl;
                    break;
                }
            }
            break; 
        }
        cout << "Next choice: ";
        cin >> choice;
    }
    cout << "Bye!";

    return 0;
}

5.

#include<iostream>

using namespace std;
const double tax1 = 5000;
const double interest1 = 0.1;
const double tax2 = 15000;
const double interest2 = 0.15;
const double tax3 = 35000;
const double interest3 = 0.2;
double ntrtax(double);

int main()
{
    double tax;
    double money;
    do {
        cout << "输入你的收入: ";
        cin >> tax;
        money = ntrtax(tax);
        if (tax > 0)
            cout << "应交税金为: " << money << " tvarps" << endl;
    } while (tax > 0);
    return 0;
}
 
double ntrtax(double n)
{
    double index;
    if (n <= tax1)
        index = n * 0;
    else if (n > tax1&& n <= tax2)
        index = (n - tax1) * interest1;
    else if (n > tax2&& n <= tax3)
        index = (n - tax2) * interest2 + (tax2 - tax1) * interest1;
    else
        index = (n - tax3) * interest3 + (tax3 - tax2) * interest2 + (tax2 - tax1) * interest1;
    return index;
}

6.

#include <iostream>
#include <string>

int main()
{
    using namespace std;
    struct Patron {
        string name;
        double money;
    };

    cout << "请输入捐献者的数目: ";
    int np;
    int index=0;
    cin >> np;
    Patron* pt = new Patron[np];
    for (int i = 0; i < np; ++i)
    {
        cout << "输入第"<<i+1<<"位捐款者姓名: ";
        cin >> (pt + i)->name;
        cout << "输入"<<(pt + i)->name<<"的捐款金额: ";
        cin >> (pt + i)->money;
    }
    cout << "捐款10000元以上:\n";
    for (int i = 0; i < np; ++i)
    {
        if ((pt + i)->money > 10000)
        {
            cout << (pt + i)->name << endl;
            index++;
        }
    }
    if (index == 0)
        cout << "没有";
    cout << "剩余捐款者:\n";
    for (int i = 0; i < np; ++i)
    {
        if ((pt + i)->money < 10000)
        cout << (pt + i)->name << endl;
    }
    delete[] pt;
    return 0;
}

7.

#include <iostream>
#include <string>
#include <cctype>

int main()
{
    using namespace std;

    cout << "Enter words (q to quit):\n";

    string word;
    char first_ch;
    int other = 0, vowel = 0, consonant = 0;

    cin >> word;
    while (word != "q")
    {
        // 判断是否是字母开头
        first_ch = word[0];
        if (isalpha(first_ch))
        {
            // 判断元音或辅音
            switch (first_ch)
            {
            case 'a':
                vowel++;
                break;
            case 'A':
                vowel++;
                break;
            case 'e':
                vowel++;
                break;
            case 'E':
                vowel++;
                break;
            case 'i':
                vowel++;
                break;
            case 'I':
                vowel++;
                break;
            case 'o':
                vowel++;
                break;
            case 'O':
                vowel++;
                break;
            case 'u':
                vowel++;
                break;
            case 'U':
                vowel++;
                break;
            default:
                consonant++;
            }
        }
        else
            other++;
        cin >> word;
    }
    cout << vowel << " words beginning with vowels" << endl;
    cout << consonant << " words beginning with consonants" << endl;
    cout << other << " others" << endl;
    return 0;
}

8.

#include <fstream>
#include <iostream>

int main()
{
    using namespace std;
    ifstream infile;
    infile.open("test.txt");
    if (!infile.is_open())
    {
        cout << "文件打开失败 "  << endl;
        exit(EXIT_FAILURE);
    }
    char ch;
    int count = 0;
    while (infile >> ch)
        count++;
    infile.close();
    cout << "文件共有 " << count << " 字符." << endl;
    return 0;
}

9.

#include <iostream>
#include <fstream>
#include <cstdlib>

const int Size = 80;

int main()
{
    using namespace std;

    struct Patron {
        char name[Size];
        double money;
    };

    ifstream infile;
    infile.open("patron.txt");
    if (!infile.is_open())
    {
        cout << "无法打开文件." << endl;
        cout << "程序终止" << endl;
        exit(EXIT_FAILURE);
    }
    int ntot;
    int index = 0;
    infile >> ntot;
    cout << ntot << endl;
    infile.get();
    Patron* pt = new Patron[ntot];
    for (int i = 0; i < ntot; ++i)
    {
        infile.getline((pt + i)->name, Size);
        infile >> (pt + i)->money;
        infile.get();
    }
    for (int i = 0; i < ntot; ++i)
    {
        cout << "输入第" << i + 1 << "位捐款者姓名: ";
        cin >> (pt + i)->name;
        cout << "输入" << (pt + i)->name << "的捐款金额: ";
        cin >> (pt + i)->money;
    }
    cout << "捐款10000元以上:\n";
    for (int i = 0; i < ntot; ++i)
    {
        if ((pt + i)->money > 10000)
        {
            cout << (pt + i)->name << endl;
            index++;
        }
    }
    if (index == 0)
        cout << "没有";
    cout << "剩余捐款者:\n";
    for (int i = 0; i < ntot; ++i)
    {
        if ((pt + i)->money < 10000)
            cout << (pt + i)->name << endl;
    }
    infile.close();
    delete[] pt;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值