C++PrimerPlus中文第六版第6章编程练习答案

1、

#include<iostream>
using namespace std;

int main()
{
    char ch;
    cout << "Enter text:\n";
    while (cin.get(ch) && ch != '@')
    {
        if (isalpha(ch))
            ch = tolower(ch);
        if (isdigit(ch))
            continue;
        cout << ch;
    }

}

2、

#include<iostream>
#include<array>
using namespace std;

const int Max = 10;

int main()
{
    array<double, Max> donation;
    int i = 0, count = 0;
    double sum = 0;
    cout << "Enter a series of double(number up to 10 and terminate with non-number):\n";
    while ((cin >> donation[i]) and i < Max)
    {
        count++;
        sum += donation[i];
        i++;
    }

    cout << count << " number has been read.\n";
    double ave;
    cout << "The average is " << (ave = sum / count) << endl;
    int largers = 0;
    i = 0;
    while (i++ < count)
    {
        if (donation[i] > ave)
            largers++;
    }
    cout << largers <<" donations are larger than average.\n";
}

3、

#include<iostream>
#include<array>
using namespace std;

const int Max = 10;

int main()
{
    cout << "Please enter one of the following choice:\n";
    cout << "a)alpha\t\tc)carnivore\np)pianist\t\tt)tree\ng)game\n";
    char ch;
    while (cin.get(ch) && !('a' == ch || 'c' == ch || 'p' == ch || 't' == ch || 'g' == ch))
    {
        cout << "Please enter a, c, p, t or g: ";
    }
    cout << "A sample is a ";
    switch (ch)
    {
    case 'a': cout << "alpha\n"; break;
    case 'c': cout << "carnivore\n"; break;
    case 'p': cout << "pianist\n"; break;
    case 't': cout << "tree\n"; break;
    case 'g': cout << "game\n"; break;
    default: "error"; break;
    }

    
}

4、

#include<iostream>
using namespace std;

const int strsize = 20;

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
};

void printInfo(bop*, int, bool, bool, bool, bool);
void printInfoCond(bop*, int);

int main()
{
    bop members[5] =
    {
        {"Wimp Macho", "manager", "WM", 0},
        {"Raki Rhodes", "programmer001", "RR", 0},
        {"Celia Laiter", "programmer002", "CL", 0},
        {"Hoppy Hipman", "programmer003", "HH", 0},
        {"Pat Hand", "programmer004", "PH", 0}
    };

    cout << "Benevolent Order of Programmers Report\n";
    cout << "a. display by name\t\tb. display by title\nc. display by bopname\t\td.diaplay by preference\nq.quit\n";
    cout << "Enter your choice: ";
    char choice;
    while (!(cin >> choice) || (choice != 'a' && choice != 'b' && choice != 'c' && choice != 'd' && choice != 'q'))
    {
        cout << "Enter a valid choice with a, b, c, d or q: ";
    }

    bool isa, isb, isc, isq;
    isa = isb = isc = isq = false;
    switch (choice)
    {
    case 'a': isa = true; break;
    case 'b': isb = true; break;
    case 'c': isc = true; break;
    case 'q': isq = true; break;
    default: break;
    }
    
    printInfo(members, 5, isq, isa, isb, isc);

}

void printInfo(bop* members, int n, bool isq, bool isa, bool isb, bool isc)
{
    if (isq)
    {
        cout << "Quit!\n";
        return;
    }

    bop* member = members;
    if (isa)
    {
        for (int i = 0; i < n; i++)
            cout << member++->fullname << endl;
    }
    else if (isb)
    {
        for (int i = 0; i < n; i++)
            cout << member++->title << endl;
    }
    else if (isc)
    {
        for (int i = 0; i < n; i++)
            cout << member++->bopname << endl;
    }
    else
    {
        for (int i = 0; i < n; i++)
        {
            printInfoCond(member, member->preference);
            member++;
        }
    }

}

void printInfoCond(bop* member, int pref)
{
    switch (pref)
    {
    case 0: cout << member->fullname << endl; break;
    case 1: cout << member->title << endl; break;
    case 2: cout << member->bopname << endl; break;
    default: cout << "Preference " << pref << " doesn't match any information!\n"; break;
    }
}

5、

#include<iostream>
using namespace std;

double incomeTax(double);

int main()
{
    double earning;
    cout << "Enter the earning int tvarps: ";
    while (cin >> earning && earning >= 0)
    {
        cout << "Tax is " << incomeTax(earning) << " for earning " << earning << " tvarps\n";
        cout << "Enter the earning int tvarps: ";
    }
}

double incomeTax(double income)
{
    double tax;
    if (income <= 5000)
        tax = 0;
    else if (income <= 15000)
        tax = (income - 5000) * 0.1;
    else if (income <= 35000)
        tax = (income - 15000) * 0.15 + 10000 * 0.1;
    else
        tax = (income - 35000) * 0.2 + 20000 * 0.15 + 10000 * 0.1;

    return tax;
}

6、

#include<iostream>
#include<string>
using namespace std;

const int StrSize = 20;

struct contri
{
    char name[StrSize];
    double fund;
};
int main()
{
    int n;
    cout << "Please enter the number of contributers: ";
    (cin >> n).get();
    contri* contributors = new contri[n];
    for (int i = 0; i < n; i++)
    {
        cout << "\nContributor #" << i + 1 << endl;
        cout << "Name: ";
        cin.getline(contributors[i].name, StrSize);
        cout << "fund: ";
        (cin >> contributors[i].fund).get();
    }

    bool grandPatrons = false;
    cout << "Grand Patrons:\n";
    for (int i = 0; i < n; i++)
    {
        if (contributors[i].fund >= 10000)
        {
            grandPatrons = true;
            cout << "Name: " << contributors[i].name << ", fund: $" << contributors[i].fund << endl;
        }
    }
    if (!grandPatrons)
        cout << "none\n";


    //Other Patrons
    bool otherPatrons = false;
    cout << "Other Patrons:\n";
    for (int i = 0; i < n; i++)
    {
        if (contributors[i].fund < 10000)
        {
            otherPatrons = true;
            cout << "Name: " << contributors[i].name << ", fund: $" << contributors[i].fund << endl;
        }
    }
    if (not otherPatrons)
        cout << "none\n";

}

7、

#include<iostream>
using namespace std;

const int WordMaxLen = 20;

int main()
{
    char word[WordMaxLen];
    cout << "Enter words (q to quit):\n";
    int vowel = 0, consonant = 0, other = 0;
    while (cin >> word && strcmp(word, "q"))
    {
        if (isalpha(word[0]))
        {
            switch (word[0] = tolower(word[0]))
            {
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u': vowel++; break;
            default: consonant++; break;
            }
        }
        else
            other++;
    }
    cout << vowel << " words beginning with vowels\n";
    cout << consonant << " words beginning with consonant\n";
    cout << other << " others\n";

}

8、

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

int main()
{
    ifstream fin;
    string filename = "chapter6question8.txt";
    fin.open(filename);
    if (!fin.is_open())
    {
        cout << filename << " doesn't exits! Programming exiting unexpectedly.\n";
    }
    char ch;
    int count = 0;
    while ((fin.get(ch)).good())
    {
        cout << ch;
        count++;
    }
    cout << count << " characters readed.\n";
}

9、

#include<iostream>
#include<string>
#include<fstream>
using namespace std;

const int StrSize = 40;

struct contri
{
    char name[StrSize];
    double fund;
};
int main()
{
    ifstream fin;
    char filename[StrSize] = "chapter6question9.txt";
    fin.open(filename);
    if (!fin.is_open())
    {
        cout << "File " << filename << " doesn't exists, program exiting unexpectedly!\n";
        exit(EXIT_FAILURE);
    }

    int n;
    (fin >> n).get();
    contri* contributors = new contri[n];
    cout << "Reading files:";
    for (int i = 0; i < n; i++)
    {
        cout << "\nContributor #" << i + 1 << endl;
        fin.getline(contributors[i].name, StrSize);
        cout << "Name: " << contributors[i].name << endl;
        (fin >> contributors[i].fund).get();
        cout << "fund: " << contributors[i].fund << endl;
    }
    cout << "Reading over.\n";
    // Grand Patrons
    bool grandPatrons = false;
    cout << "\nGrand Patrons:\n";
    for (int i = 0; i < n; i++)
    {
        if (contributors[i].fund >= 10000)
        {
            grandPatrons = true;
            cout << "Name: " << contributors[i].name << ", fund: $" << contributors[i].fund << endl;
        }
    }
    if (!grandPatrons)
        cout << "none\n";


    // Other Patrons
    bool otherPatrons = false;
    cout << "\nOther Patrons:\n";
    for (int i = 0; i < n; i++)
    {
        if (contributors[i].fund < 10000)
        {
            otherPatrons = true;
            cout << "Name: " << contributors[i].name << ", fund: $" << contributors[i].fund << endl;
        }
    }
    if (not otherPatrons)
        cout << "none\n";

}

chapter6question9.txt

4
Sam Stone
2000
Freida Flass
100500
Tammy Tubs
5000
Rich Raptor
55000

注意最后一行有个回车。

posted @ 2022-11-13 23:39  Triphan  阅读(76)  评论(0)    收藏  举报