C++ primer plus 课后练习题

第六章:分支语句和逻辑运算符

        一、复习题

        1. 减少代码的运行次数,提升其效率的同时,增强了代码的可读性

        2. 将把字符的ascii码加一后以数字的形式输出

        3. ct1 = 9, ct2 = 9

        4. a. weight >= 115 && weight < 125

            b. ch = 'q' || ch = 'Q'

            c. x % 2 == 0 && x != 26

            d. x % 2 == 0 && x % 26 != 0

            e. (donation >= 1000 && donation <= 2000) || guest == 1

            f. (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')

        5. 不同。!!x会先计算(!x)并将其结果转化为布尔值,若x原先不为0,那么(!x)的结果为false(0),再取反后为true(1),一般情况下与原先的x不相等。

        6. x >= 0 ? x : -x ;

        7. switch(ch){

                case 'A' : a_grade++;break;

                case 'B' : b_grade++;break;

                case 'C' : c_grade++;break;

                case 'D' : d_grade++;break;

                default : f_grade++;

        }

        8. 不会产生cin的读取错误,能使程序更稳定

        9. int line = 0 ;

            char ch;

            while(cin.get(ch)) && ch != 'Q'){

                        if(ch == '\n'){

                                line++;

                        }

            }

        二、编程练习

        1.

#include <iostream>
#include <cctype>

using namespace std;

int main() {
    cout << "Enter the words: " << endl;
    char ch;
    while ((ch = cin.get()) != '@') {
        if (islower(ch)) {
            cout << (char) (ch - 32);
        } else if (isupper(ch)) {
            cout << (char) (ch + 32);
        }
    }
    return 0;
}

        2.

#include <iostream>

const int Size = 10;

using namespace std;

int main() {
    double array[Size];
    int i = 0;
    double avg = 0;
    cout << "Enter the num: ";
    while (i < Size && cin >> array[i]) {
        if (++i < Size) {
            cout << "Enter the num: ";
        }
    }
    for (int j = 0; j < i; j++) {
        avg += array[j] / i;
    }
    int count = 0;
    for (int j = 0; j < i; j++) {
        count += array[j] > avg ? 1 : 0;
    }
    cout << "avg = " << avg << ", count = " << count << endl;
    return 0;
}

        3.

#include <iostream>

using namespace std;

void menu() {
    cout << "Please enter one of the following choices: " << endl;
    cout << "c) carnivore\t\tp) pianist" << endl;
    cout << "t) tree\t\t\t\tg) game" << endl;
}

int main() {
    menu();
    char ch;
    bool flag = true;
    while (true) {
        cin >> ch;
        switch (ch) {
            case 'c' :
                cout << "A maple is a carnivore" << endl;
                flag = false;
                break;
            case 'p' :
                cout << "A maple is a pianist" << endl;
                flag = false;
                break;
            case 't' :
                cout << "A maple is a tree" << endl;
                flag = false;
                break;
            case 'g' :
                cout << "A maple is a game" << endl;
                flag = false;
                break;
            default:
                menu();
        }
        if (!flag) {
            break;
        }
    }

    return 0;
}

        4.

#include <iostream>

using namespace std;
const int strsize = 20;
struct bop {
    char fullname[strsize];
    char title[strsize];
    char bopname[strsize];
    int preference;
};

void menu() {
    cout << "Please enter one of the following choices: " << endl;
    cout << "a) display by name\t\tb) display by title" << endl;
    cout << "c) display by bopname\t\td) display by preference" << endl;
}

void showName(const bop (*people)[3]) {
    for (int i = 0; i < 3; i++) {
        cout << (*people)[i].fullname << endl;
    }
}

void showTitle(const bop (*people)[3]) {
    for (int i = 0; i < 3; i++) {
        cout << (*people)[i].title << endl;
    }
}

void showBop(const bop (*people)[3]) {
    for (int i = 0; i < 3; i++) {
        cout << (*people)[i].bopname << endl;
    }
}

void showPreference(const bop (*people)[3]) {
    for (int i = 0; i < 3; i++) {
        switch ((*people)[i].preference) {
            case 0:
                cout << (*people)[i].fullname << endl;
                break;
            case 1:
                cout << (*people)[i].title << endl;
                break;
            case 2:
                cout << (*people)[i].bopname << endl;
                break;
        }
    }
}

int main() {
    bop people[3];
    people[0] = {"David", "D", "d", 1};
    people[1] = {"Eason", "E", "n", 0};
    people[2] = {"Angel", "A", "l", 2};
    menu();
    char ch;
    bool flag = true;
    while (true) {
        cin >> ch;
        switch (ch) {
            case 'a' :
                showName(&people);
                flag = false;
                break;
            case 'b' :
                showTitle(&people);
                flag = false;
                break;
            case 'c' :
                showBop(&people);
                flag = false;
                break;
            case 'd' :
                showPreference(&people);
                flag = false;
                break;
            default:
                flag = true;
                break;
        }
        if (flag) {
            break;
        }
    }

    return 0;
}

        5.

#include <iostream>

using namespace std;

int main() {
    cout << "Enter the num plz: " << endl;
    double num;
    double p = 0;
    while (cin >> num) {
        p = 0;
        if (num > 35000) {
            p += (num - 35000) * 0.2 + 20000 * 0.15 + 10000 * 0.1;
        }
        if (num >= 15001 && num <= 35000) {
            p += (num - 15000) * 0.15 + 10000 * 0.1;
        }
        if (num >= 5001 && num <= 15000) {
            p += (num - 5000) * 0.1;
        }
        cout << "p = " << p << endl;
        cout << "Enter the num plz: " << endl;
    }
    return 0;
}

        6.

#include <iostream>

using namespace std;
struct person {
    char name[20];
    double money;
};

int main() {
    cout << "Enter the num of the people: ";
    int num;
    int count = 0;
    cin >> num;
    person *p = new person[num];
    for (int i = 0; i < num; i++) {
        cout << "Enter #" << (i + 1) << " name: ";
        cin >> p[i].name;
        cout << "Enter #" << (i + 1) << " money: ";
        cin >> p[i].money;
    }
    cout << "Grand Patrons: " << endl;
    for (int i = 0; i < num; i++) {
        if (p[i].money > 10000) {
            cout << p[i].name << endl;
            count++;
        }
    }
    if (count == 0) {
        cout << "none" << endl;
    }
    count = 0;
    cout << "Patrons: " << endl;
    for (int i = 0; i < num; i++) {
        if (p[i].money <= 10000) {
            cout << p[i].name << endl;
            count++;
        }
    }
    if (count == 0) {
        cout << "none" << endl;
    }
    delete[] p;
    return 0;
}

        7.

#include <iostream>

using namespace std;

int main() {
    cout << "Enter words (q to quit): " << endl;
    int v_word = 0;
    int c_word = 0;
    int other = 0;
    string ch;
    cin >> ch;
    while (ch != "q") {
        if (isalpha(ch[0])) {
            if (ch[0] == 'a' || ch[0] == 'A' || ch[0] == 'o' || ch[0] == 'O' || ch[0] == 'e' || ch[0] == 'E'
                || ch[0] == 'i' || ch[0] == 'I' || ch[0] == 'u' || ch[0] == 'U') {
                v_word++;
            } else {
                c_word++;
            }
        } else if (isnumber(ch[0])) {
            other++;
        }
        cin >> ch;
    }
    cout << "v_word = " << v_word << endl;
    cout << "c_word = " << c_word << endl;
    cout << "other = " << other << endl;
    return 0;
}

        8.

#include <iostream>
#include <fstream>

using namespace std;

int main() {
    ifstream inFile;
    inFile.open("test");
    int count = 0;
    char ch;
    if (!inFile.is_open()) {
        exit(0);
    }
    while (!inFile.eof()) {
        inFile >> ch;
        count++;
    }
    cout << "count = " << count << endl;
    return 0;
}

        9.

#include <iostream>
#include <fstream>

using namespace std;
struct person {
    char name[20];
    double money;
};

int main() {
    ifstream inFile;
    inFile.open("test");
    int num;
    int count = 0;
    inFile >> num;
    person *p = new person[num];
    for (int i = 0; i < num; i++) {
        inFile >> p[i].name;
        inFile >> p[i].money;
    }
    cout << "Grand Patrons: " << endl;
    for (int i = 0; i < num; i++) {
        if (p[i].money > 10000) {
            cout << p[i].name << endl;
            count++;
        }
    }
    if (count == 0) {
        cout << "none" << endl;
    }
    count = 0;
    cout << "Patrons: " << endl;
    for (int i = 0; i < num; i++) {
        if (p[i].money <= 10000) {
            cout << p[i].name << endl;
            count++;
        }
    }
    if (count == 0) {
        cout << "none" << endl;
    }
    delete[] p;
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值