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

#include <iostream>
#include <cctype>
#include <string>
#include <fstream>
#include <cstdlib>


using namespace std;

void question_1();
void question_2();
void question_3();
void question_4();
void question_5();
void question_6();
void question_7();
void question_8();
void question_9();

int main()
{
    question_9();
    return 0;
}

void question_1()
{
    char ch;
    cout << "Enter '@' to quit: " << endl;
    
    while ((ch = cin.get()) != '@')
    {
        if (isdigit(ch))
            continue;
        else if (islower(ch))
            cout << char(toupper(ch));
        else if (isupper(ch))
            cout << char(tolower(ch));
    }
    return;
}

void question_2()
{
    cout << "Enter double numbers(no more than 10): " << endl;
    double donation[10];
    int i = 0;
    int count = 0;
    double sum = 0;
    while (i < 10)
    {
        if (cin >> donation[i])
        {
            sum += donation[i];
            i++;
        }
        else
            break;
    }
    double avr = sum / i;
    cout << "The average of these numbers is: " << avr << endl;
    for (int j = 0; j < i; j++)
    {
        if (donation[j] > avr)
            count++;
    }
    cout << count << " numbers are bigger than the average." << endl;
    return;
}

void question_3()
{
    char ch;
    cout << "Please enter one of the following choices:" << endl;
    cout << "c) carnivore   p) pianist" << endl;
    cout << "t) tree        g) game" << endl;
    while (cin >> ch)
    {
        switch (ch)
        {
        
            case 'c': cout << "carnivore" << endl;
                      break;
            case 'p': cout << "pianist" << endl;
                      break;
            case 't': cout << "A maple is a tree." << endl;
                      break;
            case 'g': cout << "It's a game." << endl;
                      break;
            default:  cout << "Please enter a c, p, t, or g: ";
                      continue;
        }
        break;
    }
    return;
}

void question_4()
{
    const int strsize = 50;
    const int arrsize = 5;
    struct bop
    {
        char fullname[strsize];
        char title[strsize];
        char bopname[strsize];
        int preference;
    };
    struct bop members[arrsize] = 
    {
                  {"Wimp Macho","Senior","LYD",0},
                  {"Raki Rhodes","Junior Programmer","sunny",1},
                  {"Celia Laiter","Leader","MIPS",2},
                  {"Hoppy Hipman","Analyst Trainee","",1},
                  {"Pat Hand","CEO","LOOPY",2}
    };
    char ch;
    int i;
    cout << "Benevolent Order of Programmers Report" << endl;
    cout << "a. display by name     b. display by title" << endl;
    cout << "c. display by bopname  d. display by preference" << endl;
    cout << "q. quit" << endl;
    cout << "Enter your choice: ";
    while (cin >> ch)
    {
        switch (ch)
        {
            case 'a': for (i = 0; i < arrsize; i++)
                      {
                            cout << members[i].fullname << endl;
                      }
                      cout << "Next choice: "; 
                      continue;
            case 'b': for (i = 0; i < arrsize; i++)
                      {
                            cout << members[i].title << endl;
                      }
                      cout << "Next choice: "; 
                      continue;
            case 'c': for (i = 0; i < arrsize; i++)
                      {
                            cout << members[i].bopname << endl;
                      }
                      cout << "Next choice: "; 
                      continue;
            case 'd': for (i = 0; i < arrsize; i++)
                      {
                           switch (members[i].preference)
                           {
                                   case 0: cout << members[i].fullname << endl;
                                           break;
                                   case 1: cout << members[i].title << endl;
                                           break;
                                   case 2: cout << members[i].bopname << endl;
                                           break;
                                   default: cout << "members " << i << " preference got wrong numbers!" << endl;
                                           break;
                           }
                      }
                      cout << "Next choice: "; 
                      continue;
            case 'q': break;
            default : cout << "Please enter a, b, c, d or q:";
                      continue;
        }
        cout << "Bye!" << endl;
        break;
    }
    return;
}

void question_5()
{
    int money;
    double tax;
    cout << "Please enter your salary(-1 or alpha to quit): " << endl;
    while (cin >> money && money >= 0)
    {
        if (money <= 5000)
            tax = 0.0;
        else if (money >= 5001 && money <= 15000)
            tax = (money - 5000) * 0.1;
        else if (money >= 15001 && money <= 35000)
            tax = 10000 * 0.1 + (money - 15000) * 0.15;
        else 
            tax = 10000 * 0.1 + 20000 * 0.15 + (money - 35000) * 0.2;
        cout << "Your tax is: " << tax << endl;
        cout << "Please enter your salary(-1 or alpha to quit): " << endl;
    }
    return;
}

void question_6()
{
    struct donate
    {
        char name[50];
        double money;
    };
    cout << "Please enter the number of patrons: ";
    int num;
    cin >> num;
    cin.get();
    struct donate * patrons = new donate[num];
    for (int i = 0; i < num; i++)
    {
        cout << "Patron#" << i+1 << ":" << endl;
        cout << "name: ";
        cin.get(patrons[i].name, 50);
        cout << "money: ";
        cin >> patrons[i].money;
        cin.get();
    }
    cout << "Grand Patrons:" << endl;
    bool none = true;
    for (int j = 0; j < num; j++)
    {
        if (patrons[j].money > 10000)
        {
            none = false;
            cout << patrons[j].name << " donates " << patrons[j].money << endl;
        }
    }
    if (none)  cout << "none" << endl;
    cout << "Patrons:" << endl;
    none = true;
    for (int m = 0; m < num; m++)
    {
        if (patrons[m].money <= 10000)
        {
            none = false;
            cout << patrons[m].name << " donates " << patrons[m].money << endl;
        }
    }
    if (none)  cout << "none" << endl;
    delete [] patrons;
    return;
}

void question_7()
{
    cout << "Enter words (q to quit): " << endl;
    string word;
    int vowel, cons, others;
    vowel = cons = others = 0;

    while (cin >> word)
    {
        if (word == "q")
            break;
        else if (isalpha(word[0]))
            switch (word[0])
            {
                case 'a': 
                case 'e':
                case 'i':
                case 'u':
                case 'o': vowel++;
                          break;
                default : cons++;
                          break;
            }
        else
            others++;
    }
    cout << vowel << " words beginning with vowels" << endl;
    cout << cons << " words beginning with consonants" << endl;
    cout << others << " others" << endl;
    return;
}

void question_8()
{
    const char filename[50] = "question8.txt";
    /*
    question8.txt
    Hello,
    I'm a test.
    */
    char ch;
    int count = 0;
    ifstream file;
    file.open(filename);
    if (!file.is_open())
    {
        cout << "Could not open this file!" << endl;
        exit (EXIT_FAILURE);
    }
    else
        cout << "The file is opened." << endl;
    file.get(ch);
    while (file.good())
    {
        count++;
        file.get(ch);
    }
    if (file.eof())
        cout << "End of file." << endl;
    file.close();
    cout << count << " char in total. " << endl;
    return;
}

void question_9()
{
    struct donate
    {
        char name[50];
        double money;
    };
    const char filename[50] = "question9.txt";
    ifstream file;
    file.open(filename);
    int num;
    file >> num;
    file.get();
    struct donate * patrons = new donate[num];
    for (int i = 0; i < num; i++)
    {
        cout << "Patron#" << i+1 << ":" << endl;
        cout << "name: ";
        file.get(patrons[i].name, 50);
        cout << patrons[i].name << endl;
        cout << "money: ";
        file >> patrons[i].money;
        file.get();
        cout << patrons[i].money << endl;
    }
    cout << "Grand Patrons:" << endl;
    bool none = true;
    for (int j = 0; j < num; j++)
    {
        if (patrons[j].money > 10000)
        {
            none = false;
            cout << patrons[j].name << " donates " << patrons[j].money << endl;
        }
    }
    if (none)  cout << "none" << endl;
    cout << "Patrons:" << endl;
    none = true;
    for (int m = 0; m < num; m++)
    {
        if (patrons[m].money <= 10000)
        {
            none = false;
            cout << patrons[m].name << " donates " << patrons[m].money << endl;
        }
    }
    if (none)  cout << "none" << endl;
    delete [] patrons;
    file.close();
    return;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值