1. 程式人生 > >c++ primer plus 第六章 課後題答案

c++ primer plus 第六章 課後題答案

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

int main()
{
    char in_put;

    do
    {
        cout << "Please enter the letters (enter @ exit):";
        cin >> in_put;

        if (islower(in_put))
            cout << "The uppercase of the letter 
" << in_put << " is " << char (toupper(in_put)) << ".\n"; else if (isupper(in_put)) cout << "The lowercase of the letter " << in_put << " is " << char (tolower(in_put)) << ".\n"; else if (in_put != '@') cout
<< "Please enter the letters!\n"; } while (in_put != '@'); cout << "Exit!\n"; system("pause"); }

#include <iostream>
using namespace std;
const int MAX = 10;

int main()
{
    double in_put[MAX];
    double avg = 0;
    int sum;

    cout << "Please enter no more than 
" << MAX << " digits.\n"; for (int i = 0; i < MAX; ++i) { cout << "The " << i + 1 << "th digit is: "; if (cin >> in_put[i]) avg += in_put[i]; else { cout << "Not digit! Exit!\n"; sum = i; break; } } avg = avg / sum; int num_a=0, num_b=0; for (int i = 0; i < sum; ++i) { if (in_put[i] > avg) num_a += 1; else num_b += 1; } cout << "The average of these numbers is: " << avg << ".\n" << "There are " << num_a << " numbers larger than the average value.\nAnd " << num_b << " numbers not larger than the average value.\n"; system("pause"); }

#include <iostream>
using namespace std;
int show(char);

int show(char ch)
{
    int flag = 0;
    cout << "A maple is a ";
    switch(ch)
    {
    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:
        cout << "Please enter c,p,t,g :";
        flag = 1;
    }
    return flag;
}

int main()
{
    char ch;
    int flag;

    cout << "Please enter one of the following choices:\n"
        << "c) carnivore\t" << "p) pianist\n"
        << "t) tree\t\t" << "g) game\n";

    do
    {
        cin >> ch;
        flag=show(ch);
    } while (flag == 1);
    

    system("pause");
}

 

#include <iostream>
using namespace std;
const int strsize = 20;
const int MAX = 5;


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

int show(char ch, bop mmm[MAX])
{
    int flag = 0;
    switch (ch)
    {
    case 'a':
        for (int i = 0; i < MAX; ++i)
        {
            cout << mmm[i].fullname<<endl;
        }
        break;
    case 'b':
        for (int i = 0; i < MAX; ++i)
        {
            cout << mmm[i].title << endl;
        }
        break;
    case 'c':
        for (int i = 0; i < MAX; ++i)
        {
            cout << mmm[i].bopname << endl;
        }
        break;
    case 'd':
        for (int i = 0; i < MAX; ++i)
        {
            switch(mmm[i].preference)
            {
                case 0:cout << mmm[i].fullname << endl;
                    break;
                case 1:cout << mmm[i].title << endl;
                    break;
                case 2:cout << mmm[i].bopname << endl;
                    break;
            }
        }
        break;
    case 'q':
        flag = 1;
        break;
    default:
        cout << "Please enter a,b,c,d,q!\n";
    }
    return flag;
}



int main()
{
    bop all_peo[MAX] =
    {
        { "QQQ","NiCai","qqq",0 },
        { "WWW","BuCai","www",1 },
        { "EEE","CaiBuCai","eee",2 },
        { "RRR","JiuBuCai","rrr",0 },
        { "TTT","LaDao","ttt",1 }
    };
    
    char ch;
    int flag;
    
    cout << "Benevolent Order of Programmers Report\n"
        << "a. display by name\t" << "b. display by title\n"
        << "c. display by bopname\t" << "d. display by preference\n"
        << "q. quit\n";

    cout << "Enter your choice:";
    cin >> ch;

    flag = show(ch, all_peo);

    while (flag == 0)
    {
        cout << "Next choice: ";
        cin >> ch;
        flag=show(ch, all_peo);
    }
    cout << "Bye!\n";

    system("pause");
}

#include <iostream>
using namespace std;
const float Tax_1 = 0.1;
const float Tax_2 = 0.15;
const float Tax_3 = 0.2;
const int Tax_come1 = 5000;
const int Tax_come2 = 15000;
const int Tax_come3 = 35000;

double Tax(double);

int main()
{
    double income;
    
    cout << "Please enter income(enter a negative or non-numeric exit): ";
    while (cin >> income)
    {
        if (income < 0)
            break;
        double tax;
        tax=Tax(income);
        cout << "Income tax: " << tax << "\nNext enter: ";
    }
    cout << "Bye!\n";

    system("pause");
}

double Tax(double income)
{
    double tax;
    double num1 = income - Tax_come3;
    double num2 = income - Tax_come2;
    double num3 = income - Tax_come1;
    if (num1 > 0)
        tax =(Tax_come2 - Tax_come1)*Tax_1 + (Tax_come3 - Tax_come2)*Tax_2 + num1 * Tax_3;
    else
    {
        if (num2 > 0)
            tax =(Tax_come2 - Tax_come1)*Tax_1 + num2 *Tax_2;
        else
        {
            if (num3 > 0)
                tax = num3 * Tax_1;
            else
                tax = 0;
        }
    }

    return tax;
}

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

struct potron
{
    string name;
    double money;
};

int main()
{
    int num;
    cout << "Please enter the number of donors: ";
    cin >> num;

    potron *potrons = new potron[num];
    for (int i = 0; i < num; ++i)
    {
        cout << "Please enter the " << i + 1 << "th donor name:";
        cin.ignore();
        getline(cin,potrons[i].name);
        cout << "Please enter the number of " << i + 1 << "th donor contributions:";
        cin >> potrons[i].money;
    }

    int flag1 = 0, flag2 = 0;
    cout << "Grand Potrons\n";
    for (int i = 0; i < num; ++i)
    {
        if (potrons[i].money > 10000)
        {
            cout << potrons[i].name << " : " << potrons[i].money << endl;
            flag1 = 1;
        }
    }
    if (flag1 == 0)
        cout << "None!\n";

    cout << "Potrons\n";
    for (int i = 0; i < num; ++i)
    {
        if (!(potrons[i].money > 10000))
        {
            cout << potrons[i].name << " : " << potrons[i].money << endl;
            flag2 = 1;
        }
    }
    if (flag2 == 0)
        cout << "None!\n";

    system("pause");
}

 

#include <iostream>
#include <cctype>
using namespace std;
const int MAX = 100;

int main()
{
    char words[MAX][25];
    char ch;
    int num_y = 0, num_f = 0, num_o = 0;

    cout << "Enter words (q to quit):\n";
    for (int i = 0; i < MAX; ++i)
    {
        cin >> words[i];
        ch = words[i][0];
        if (ch == 'q')
            break;
        if (isalpha(ch))
        {
            switch (ch)
            {
            case 'a':;
            case 'e':;
            case 'i':;
            case 'o':;
            case 'u':
                num_y += 1;
                break;
            default:
                num_f += 1;
                break;
            }
        }
        else
            num_o += 1;
    }

    cout << num_y << " beginning with vowels\n";
    cout << num_f << " beginning with consonants\n";
    cout << num_o << " others\n";
    system("pause");
}

 

#include <iostream>
#include <fstream>    
#include <cstdlib>
using namespace std;
const int SIZE = 100;

int main()
{
    char filename[SIZE];
    ifstream infile;
    cout << "Enter name of data file: ";
    cin.getline(filename, SIZE);
    infile.open(filename);
    if (!infile.is_open())
    {
        cout << "Could not open the file " << filename << endl;
        cout << "Program terminating.\n";
        exit(EXIT_FAILURE);
    }

    int count=0;
    char ch;

    infile >> ch;
    while (infile.good()) 
    {
        ++count;          
//        ch= infile.get();//讀取空白字元
        infile >> ch;//不讀取空白字元
    }

    cout << "The number of characters in the file is: " << count << endl;

    system("pause");
}

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
const int SIZE = 100;

struct potron
{
    string name;
    double money;
};

int main()
{
    char filename[SIZE];
    ifstream infile;
    cout << "Enter name of data file: ";
    cin.getline(filename, SIZE);
    infile.open(filename);
    if (!infile.is_open())
    {
        cout << "Could not open the file " << filename << endl;
        cout << "Program terminating.\n";
        exit(EXIT_FAILURE);
    }

    int num;
    infile >> num;

    potron *potrons = new potron[num];
    for (int i = 0; i < num; ++i)
    {
        infile.ignore();
        getline(infile, potrons[i].name);
        infile >> potrons[i].money;
    }

    int flag1 = 0, flag2 = 0;
    cout << "Grand Potrons\n";
    for (int i = 0; i < num; ++i)
    {
        if (potrons[i].money > 10000)
        {
            cout << potrons[i].name << " : " << potrons[i].money << endl;
            flag1 = 1;
        }
    }
    if (flag1 == 0)
        cout << "None!\n";

    cout << "Potrons\n";
    for (int i = 0; i < num; ++i)
    {
        if (!(potrons[i].money > 10000))
        {
            cout << potrons[i].name << " : " << potrons[i].money << endl;
            flag2 = 1;
        }
    }
    if (flag2 == 0)
        cout << "None!\n";

    system("pause");
}