• c++primer 第l六章编程练习答案


    6.11.1

    #include<iostream>
    #include<cctype>
    
    int main() {
        using namespace std;
        char ch;
        cin.get(ch);
        while (ch != '@') {
            if (isdigit(ch))
                cout << ch;
            else if (isupper(ch)) {
                ch = tolower(ch);
                cout << ch;
            }
            else if (islower(ch)) {
                ch = toupper(ch);
                cout << ch;
            }
            cin.get(ch);
        }
    }

    6.11.2

    #include<iostream>
    #include<cctype>
    
    int main() {
        using namespace std;
        double donations[10];
        double donation;
        double average, sum = 0;
        int i, num=0;
        cout << "input donation,at most 10
    ";
        for (i = 0; (cin >> donation) && (i < 10); i++) {
            donations[i] = donation;
            sum += donations[i];
        }
        average = sum / i;
        for (i = 0; i < 10; i++) {
            if (donations[i] > average)
                num++;
        }
        cout << "average is " << average << " and " << num << " numbers bigger than avergae.";
    }

    6.11.3

    #include<iostream>
    
    int main() {
        using namespace std;
        char ch;
        cout << "Please enter one of the following choices:
    "
            "c) carnivore             p) pianist
    "
            "t) tree                  g) game
    ";
        cout << "Please enter a c,p,t,or g:";
        cin >> ch;
        //cout << "A maple is a ";
        while (ch!='f') {
            switch (ch)
            {
            case'c':cout << "A maple is a carnivore.";
                break;
            case'p':cout << "A maple is a pianist.";
                break;
            case't':cout << "A maple is a tree.";
                break;
            case'g':cout << "A maple is a game.";
                break;
            default:cout << "Please enter a c,p,t,or g:";
            }
            cin >> ch;
        }
    }

    6.11.4

    #include<iostream>
    
    using namespace std;
    
    const int strsize = 20;
    const int memberSize = 3;
    
    struct bop {
        char fullname[strsize];
        char title[strsize];
        char bopname[strsize];
        int preference;
    };
    void name(bop *member);
    void title(bop *member);
    void bopname(bop *menber);
    void preference(bop *member);
    
    int main() {
        char choose;
        bop member[3] = {
            {"zhang","manager","boss",1},
            {"li","programer","pg",0},
            {"wang","teacher","tc",2}
        };
        cout << "Benevolent Order of Programmers Report
    "
            "a.display by name          b.display by title
    "
            "c.display by bopname       d.display by preference
    "
            "q.quit
    "
            "Enter your choice: ";
        cin >> choose;
    
        while (choose!='q')
        {
            switch (choose)
            {
            case 'a':name(member);
                break;
            case 'b':title(member);
                break;
            case 'c':bopname(member);
                break;
            case 'd':preference(member);
                break;
            default:cout << "Not good choose
    ";
                break;
            }
            cout << "Next choose:";
            cin >> choose;
        }
    }
    
    void name(bop * member)
    {
        for (int i = 0; i < memberSize; i++) {
            cout << member[i].fullname << endl;
        }
    }
    
    void title(bop * member)
    {
        for (int i = 0; i < memberSize; i++) {
            cout << member[i].title << endl;
        }
    }
    
    void bopname(bop * member)
    {
        for (int i = 0; i < memberSize; i++) {
            cout << member[i].bopname << endl;
        }
    }
    
    void preference(bop * member)
    {
        for (int i = 0; i < memberSize; i++) {
            switch (member[i].preference)
            {
            case 0:cout << member[i].fullname << endl;
                break;
            case 1:cout << member[i].title << endl;
                break;
            case 2:cout << member[i].bopname << endl;
                break;
            default:
                cout << "存在非法的偏好";
                break;
            }
        }
    }

    6.11.5

    #include<iostream>
    
    int main() {
        using namespace std;
        int money;
        double tax = 0;
        cout << "Please input your income: ";
        cin >> money;
        cout << fixed;
        cout.precision(2);
        cout.setf(ios::showpoint);
        while (cin.good()&&(money>=0))
        {
            if (money<=5000)
            {
                tax = 0.0;
                break;
            }
            else if ((money > 5000) && (money <= 15000)) {
                tax = (money - 5000)*0.1;
                break;
            }
            else if ((money > 15000) && (money <= 35000)) {
                tax = (money - 15000)*0.15 + 10000 * 0.1;
                break;
            }
            else {
                tax = (money - 35000)*0.2 + 20000 * 0.15 + 10000 * 0.1;
                break;
            }
        }
        cout << "Your personal income tax is " << tax << " tvarps";
    }

    6.11.6

    #include<iostream>
    #include<string>
    
    using namespace std;
    
    struct donations
    {
        string name;
        double money;
    };
    
    int main() {
        int donorSize, grand_patron_size = 0, patrons_size = 0;
        cout << "How many people donate?
    "
            "donorSize: ";
        cin >> donorSize;
        donations *donor = new donations[donorSize];
        donations *grand_patrons = new donations[donorSize];
        donations *patrons = new donations[donorSize];
        cout << "Please enter donor's contribution amount:" << endl;
        cout << fixed;
        cout.precision(2);
        cout.setf(ios_base::showpoint);
        for (int i = 0; i < donorSize; i++)
        {
            cout << "name:";
            cin >> donor[i].name;
            //cin.get();
            //cout << "	";
            cout << "contribution amout:";
            cin >> donor[i].money;
        }
        for (int i = 0; i < donorSize; i++)
        {
            if (donor[i].money > 10000) {
                grand_patrons[grand_patron_size] = donor[i];
                ++grand_patron_size;
            }
            else {
                patrons[patrons_size] = donor[i];
                ++patrons_size;
            }
        }
        cout << "=============== Grand Partrons ================" << endl;
        if (grand_patron_size != 0) {
            for (int i = 0; i < grand_patron_size; i++)
            {
                cout << "name: " << grand_patrons[i].name << "	money:" << grand_patrons[i].money << endl;
            }
        }
        else
        {
            cout << "None";
        }
        cout << "================= Partrons ====================" << endl;
        if (patrons_size != 0) {
            for (int i = 0; i < patrons_size; i++)
            {
                cout << "name: " << patrons[i].name << "	money:" << patrons[i].money << endl;
            }
        }
        else
        {
            cout << "None";
        }
        delete[] donor;
        delete[] grand_patrons;
        delete[] patrons;
    }

    6.11.7

    #include<iostream>
    #include<string>
    #include<cctype>
    
    int main() {
        using namespace std;
        int vowel = 0, consonant = 0, other = 0;
        string word;
        cout << "Enter word (q to quit): 
    ";
        cin >> word;
        while (cin.good() && word != "q")
        {
            if (isalpha(word[0])) {
                switch (word[0])
                {
                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.";
    }

    6.11.8

    #include<iostream>
    #include<fstream>
    #include<cstdlib>
    #include<string>
    
    int main() {
    using namespace std;
    ifstream infile;
    string str;
    int sum = 0;
    infile.open("D:\visual studio 2015\Projects\homework\homework\Debug\TextFile1.txt");
    if (infile.is_open()) {
    while (infile.good())
    {
    str += infile.get();
    ++sum;
    }
    cout << "The file contains " << sum << " characters.";
    }
    else
    {
    cout << "Failed to read file.";
    exit(EXIT_FAILURE);
    }
    infile.close();
    }

    6.11.9

    #include<iostream>
    #include<string>
    #include<fstream>
    #include<cstdlib>
    
    using namespace std;
    
    struct donations
    {
    string name;
    double money;
    };
    
    int main() {
        int donorSize, grand_patron_size = 0, patrons_size = 0;
        string donor_information;
        ifstream infile;
        infile.open("D:\visual studio 2015\Projects\homework\homework\TextFile2.txt");
        if (infile.is_open()) {
            infile >> donorSize;
            infile.get();
            cout << donorSize << endl;
            donations *donor = new donations[donorSize];
            donations *grand_patrons = new donations[donorSize];
            donations *patrons = new donations[donorSize];
    
            cout << fixed;
            cout.precision(2);
            cout.setf(ios_base::showpoint);
    
            for (int i = 0; i < donorSize; i++)
            {
    
                getline(infile, donor[i].name);
                infile >> donor[i].money;
                infile.get();
            }
            for (int i = 0; i < donorSize; i++)
            {
                cout << donor[i].name << endl;
                cout << donor[i].money << endl;
            }
            for (int i = 0; i < donorSize; i++)
            {
                if (donor[i].money > 10000) {
                    grand_patrons[grand_patron_size] = donor[i];
                    ++grand_patron_size;
                }
                else {
                    patrons[patrons_size] = donor[i];
                    ++patrons_size;
                }
            }
            cout << "=============== Grand Partrons ================" << endl;
            if (grand_patron_size != 0) {
                for (int i = 0; i < grand_patron_size; i++)
                {
                    cout << "name: " << grand_patrons[i].name << "	money:" << grand_patrons[i].money << endl;
                }
            }
            else
            {
                cout << "None";
            }
            cout << "================= Partrons ====================" << endl;
            if (patrons_size != 0) {
                for (int i = 0; i < patrons_size; i++)
                {
                    cout << "name: " << patrons[i].name << "	money:" << patrons[i].money << endl;
                }
            }
            else
            {
                cout << "None";
            }
            delete[] donor;
            delete[] grand_patrons;
            delete[] patrons;
        }
        else
        {
            cout << "Failed to open file.";
            exit(EXIT_FAILURE);
        }
        infile.close();
    }
  • 相关阅读:
    商业模式--资源整合
    “不小心出卖了领导”
    计算机禁用桌面并且禁用键盘Win+*快捷键组合之后如何打开文件资源管理器
    spring-boot 参数长度、文件上传大小限制问题
    Python 学习基础
    Python字典详解
    Python元组
    Python列表详解
    Python变量类型
    Python字符串详解
  • 原文地址:https://www.cnblogs.com/zhang293/p/7552254.html
Copyright © 2020-2023  润新知