C++ Primer Plus 第六版课后习题答案(第六章)

这篇博客提供了C++ Primer Plus第六版第六章的全部课后习题答案,读者可以在main函数修改题号以运行不同程序。特别地,第九题的测试用例已附在资源中。

 C++ Primer Plus 第六版(第六章)的课后习题答案,在main函数中把题号换掉就可以跑不同的程序,第九题的用例在附件中:

#include <iostream>
#include <fstream>
#include <cctype>
#include <vector>
using namespace std;

void p1()
{
    char ch;
    while( ch = cin.get())
    {
        if (ch == '@')
            break;
        else if (isdigit(ch))
            continue;
        else if(islower(ch))
            ch = toupper(ch);
        else if(isupper(ch))
            ch = tolower(ch);
        cout << ch;
    }
}

bool is_double(string x)
{
    double y;
	try{
		y = stod(x);
	}
	catch (const std::exception&){
		return false;
	}
	return true;
}

void p2()
{
    double donation,sum,average;
    double Donation[10];
    int size,above = 0;
    string buf;
    cout << "Please input the donation:"<< endl;
    getline(cin, buf);
    while(is_double(buf) && size<10)
    {
        donation = stod(buf);
        Donation[size++] = donation;
        sum += donation;
        getline(cin,buf);
    }
    average = sum/size;
    cout << "The average is " << average <<endl;
    for(int i=0; i<=size; ++i)
    {
        if(Donation[i]>average)
            above++;
    }
    cout << "There are " << above << " numbers larger than average."<<endl;
}

void p3()
{
    char ch;
    cout << "Please enter one of the following choices:" << endl;
    cout << "c) carnivore                    p) pianist" << endl;
    cout << "t) tree                         g) game" << endl;
    while(true)
    {
        cout << "Please enter a c, p, t, or g: ";
        cin >> ch;
        if(ch != 'c' && ch != 'p' && ch != 't' && ch != 'g')
        {
            continue;
        }
        cout << "A maple is a";
        switch(ch)
        {
            case 'c':
                cout << "carnivore.";
                break;
            case 'p':
                cout << "pianist.";
                break;
            case 't':
                cout << "tree.";
                break;
            case 'g':
                cout << "game.";
                break;
        }
        break;
    }
}


void p4()
{
    const int strSize = 50;
    struct bop
    {
        char fullname[strSize]; //real name
        char title[strSize];    //job title
        char bopname[strSize];  //secret BOP name
        int preference;  //0=fullname, 1=title, 2=bopname
    };
    vector<bop> bop_vector;
    char ch;
    bop_vector.push_back(bop{"Bob Smith", "chairman", "Bob", 0});
    bop_vector.push_back(bop{"Anne Green", "vice chairman", "Ann", 2});
    bop_vector.push_back(bop{"Helen Chen", "Junior Programmer", "Helen", 1});
    bop_vector.push_back(bop{"Wimp Macho", "MIPS", "wimp", 2});
    bop_vector.push_back(bop{"Raki Rhodes", "LOOPY", "raki", 1});
    cout << "Benevolent Order of Programmers Report" << endl;
    cout << "a. display              b.display by title" << endl;
    cout << "c. display by bopname   d. display by preference" << endl;
    cout << "q. quit" << endl;
    cout << "Enter your choice:";
    cin >> ch;
    while(true)
    {
        if(ch == 'q')
        {
            cout << "Bye!";
            break;
        }
        switch(ch)
        {
            case 'a':
                for(int i=0; i<bop_vector.size(); ++i)
                {
                    cout<<bop_vector[i].fullname << endl;
                }
                break;
            case 'b':
                for(int i=0; i<bop_vector.size(); ++i)
                {
                    cout<<bop_vector[i].title << endl;
                }
                break;
            case 'c':
                for(int i=0; i<bop_vector.size(); ++i)
                {
                    cout<<bop_vector[i].bopname << endl;
                }
                break;
            case 'd':
                for(int i=0; i<bop_vector.size(); ++i)
                {
                    int preference = bop_vector[i].preference;
                    switch (preference)
                    {
                        case 0:
                            cout << bop_vector[i].fullname << endl;
                            break;
                        case 1:
                            cout << bop_vector[i].title << endl;
                            break;
                        case 2:
                            cout << bop_vector[i].bopname << endl;
                    }
                }
                break;
        }
        cout << "Next choice:";
        cin >> ch;
    }
}

void p5()
{
    int incoming;
    cout << "Please input your incoming: ";
    while( cin >> incoming && incoming>0 )
    {
        cout << "Your taxes is ";
        if(incoming <= 5000)
            cout << 0;
        else if(incoming <= 15000 && incoming >5000)
            cout << (incoming-5000)*0.1;
        else if(incoming <= 35000 && incoming >15000)
            cout << 15000*0.1+(incoming-15000)*0.15;
        else
            cout << 15000*0.1+20000*0.15+(incoming-35000)*0.2;
        cout << "." << endl;
        cout << "Please input your incoming: ";
    }   
}

void p6()
{
    struct Donors
    {
        string name;
        double money;
    };
    vector<Donors> donors_vector;
    while(true)
    {
        string name;
        double money;
        char next;
        cout << "Please input the patron's name: ";
        getline(cin,name);
        cout << "Please input the money of " << name << " : ";
        (cin >> money).get();
        donors_vector.push_back(Donors{name,money});
        cout << "Next patron? (Y or N)" << endl;
        (cin >> next).get();
        if(next != 'Y' && next != 'y')
            break;
    }
    int grand,normal = 0;
    cout << "Grand Patrons:" << endl;
    for(Donors x : donors_vector)
    {
        if(x.money > 10000)
        {
            cout << x.name << " : " << x.money << endl;
            grand++;
        }
    }
    if(grand == 0)
        cout << "none" << endl;

    cout << "Patrons:" << endl;
    for(Donors x : donors_vector)
    {
        if(x.money <= 10000)
        {
            cout << x.name << " : " << x.money << endl;
            normal++;
        }
    }
    if(normal == 0)
        cout << "none" << endl;  
}

void p7()
{
    char ch;
    int vowel,consonants,others = 0;
    cout << "Enter words (q to quit): " << endl;
    while(cin.get(ch))
    {
        if(isalpha(ch))
        {
            if(ch == 'q')
                break;
            else if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
            {
                vowel++;
                while(cin.get() != ' ');
            }    
            else if(isalpha(ch))
            {
                consonants++;
                while(cin.get() != ' ');
            }
        }
        else
        {
            others++;
            while(cin.get() != ' ');
        }
    }
    cout << vowel << " words beginning with vowels" << endl;
    cout << consonants << " words beginning with consonants" << endl;
    cout << others << " others" << endl;
}

void p8()
{
    ifstream inFile;
    string filename;
    cout << "Enter name of data file: ";
    getline(cin,filename);
    inFile.open(filename);
    if(!inFile.is_open())
        exit(EXIT_FAILURE);
    char ch;
    int numbers = 0;
    inFile >> ch;
    while(inFile.good())
    {
        numbers++;
        inFile >> ch;
    }
    if(inFile.eof())
        cout << numbers << " chars are in this file." << endl;
}

void p9()
{
    string filename;
    int number;
    ifstream inFile;
    struct Donors
    {
        string name;
        double money;
    };
    vector<Donors> donors_vector;
    cout << "Enter name of patrons file: ";
    cin >> filename;
    inFile.open(filename);
    if(!inFile.is_open())
        exit(EXIT_FAILURE);
    (inFile >> number).get();
    while(inFile.good())
    {
        string name;
        double money;
        getline(inFile,name);
        (inFile >> money).get();
        donors_vector.push_back(Donors{name,money});
    }
    if(inFile.eof())
    {
        int grand,normal = 0;
        cout << "Grand Patrons:" << endl;
        for(Donors x : donors_vector)
        {
            if(x.money > 10000)
            {
                cout << x.name << " : " << x.money << endl;
                grand++;
            }
        }
        if(grand == 0)
            cout << "none" << endl;

        cout << "Patrons:" << endl;
        for(Donors x : donors_vector)
        {
            if(x.money <= 10000)
            {
                cout << x.name << " : " << x.money << endl;
                normal++;
            }
        }
        if(normal == 0)
            cout << "none" << endl; 
    }
}

int main()
{
    p1();//在这换题号
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值