C++ Primer Plus(第6版)课后编程习题答案(自敲)——第六章

C++ Primer Plus_第6版_第六章


ps:本章最后两道编程练习题属于文件操作,故放置于第十七章进行整体汇总

6.11.1

编写程序,读取键盘输入,知道遇到@为止,并回显,同时将大小写字母互相转换

#include <iostream>
#include <cctype>

using namespace std;

/*
编写程序,读取键盘输入,直到遇到@为止,并回显输入(数字除外)
同时将大小写字母互相转换
*/

int main()
{
    char ch;
    cin.get(ch);
    while (ch != '@') {
        if (islower(ch)) {
            cout << char(toupper(ch));
        }
        else if (isupper(ch)) {
            cout << char(tolower(ch));
        }
        ++ch;
        cin.get(ch);
    }
    return 0;
}

6.11.2

编写程序,最多将10个值读入一个double数组中(也可以使用array)程序遇到非数字输入时,结束输入,输出这些数字的平均值以及数组中有多少个数字大于平均值

#include <iostream>

using namespace std;

/*
编写程序,最多将10个值读入一个double数组中(也可以使用array)
程序遇到非数字输入时,结束输入
输出这些数字的平均值以及数组中有多少个数字大于平均值
*/

int main()
{
    double num[10];
    int i = 0;
    double sum = 0.0;    //总和
    int count_num = 0;  //总共的数字个数
    double ave = 0.0;    //平均值
    int count_upper = 0;    //大于平均值的个数
    while (cin >> num[i]) {
        sum += num[i];
        count_num++;
        i++;
        if (count_num > 10)
            break;
    }
    ave = sum / count_num;
    cout << "平均值:" << ave << endl;
    i = 0;
    while (i < count_num) {
        if (num[i] > ave)
            count_upper++;
        i++;
    }
    cout << "比平均值大的有" << count_upper << "个" << endl;
    return 0;
}

6.11.3

编写程序,显示一个提供4个选项的菜单——每个选项用一个字母标记,如果用户使用有效选项之外的字母,程序将提示用户输入一个有效的字母,直到用户这样做为止

#include <iostream>

using namespace std;

/*
编写程序,显示一个提供4个选项的菜单——每个选项用一个字母标记
如果用户使用有效选项之外的字母,
程序将提示用户输入一个有效的字母,直到用户这样做为止
*/

int main()
{
    char ch;
    cout << "请选择一个游戏:" << endl;
    cout << "a.打地鼠\tb.贪吃蛇" << endl;
    cout << "c.推箱子\td.抓小鸡" << endl;
    cout << "请输入a,b,c,d:";
    cin >> ch;
    while (true) {

        if ((ch!='a')&&(ch!='b')&&(ch!='c')&&(ch!='d')) {
            cout << "请重新输入:";
            cin >> ch;
        }
        else
            break;
    }
    switch (ch) {
    case 'a':
        cout << "打地鼠" << endl;
        break;
    case 'b':
        cout << "贪吃蛇" << endl;
        break;
    case 'c':
        cout << "推箱子" << endl;
        break;
    case 'd':
        cout << "抓小鸡" << endl;
        break;
    default:
        break;
    }
    return 0;
}

6.11.4

BOP大会上,人们可以通过加入者的姓名,头衔或者秘密BOP来了解他(她),编写程序,可以使用真实姓名、头衔、秘密姓名火成员偏好列出成员,使用下面的结构
struct bop {
char fullname[strsize]; //真实姓名
char title[strsize]; //头衔
char bopname[strsize]; //bop姓名
int preference; //0=fullname, 1=title, 2=bopname
};
创建一个由上述结构组成的小型数组,并将其初始化为适当的值

#include <iostream>

using namespace std;

/*
BOP大会上,人们可以通过加入者的姓名,头衔或者秘密BOP来了解他(她)
编写程序,可以使用真实姓名、头衔、秘密姓名火成员偏好列出成员
使用下面的结构
struct bop {
    char fullname[strsize];  //真实姓名
    char title[strsize];    //头衔
    char bopname[strsize];  //bop姓名
    int preference; //0=fullname, 1=title, 2=bopname
};
创建一个由上述结构组成的小型数组,并将其初始化为适当的值
*/

const int strsize = 20;

struct bop {
    char fullname[strsize];  //真实姓名
    char title[strsize];    //头衔
    char bopname[strsize];  //bop姓名
    int preference; //0=fullname, 1=title, 2=bopname
};

int main()
{
    bop people[5] = {
        {"Bob", "player", "B", 0},
        {"Jack", "boss", "J", 2},
        {"Steve", "worker", "S", 2},
        {"White", "engineer", "W", 1},
        {"Alex", "finder", "A", 0},
    };
    cout << "菜单:" << endl;
    cout << "a.fullname\tb.title" << endl;
    cout << "c.bopname\td.preference" << endl;
    cout << "q.quit" << endl;
    cout << "请输入:";
    char choice;
    while (true) {
        cin >> choice;
        if ((choice == 'a')||(choice == 'b')||(choice == 'c')||(choice == 'd')||(choice == 'q')) {
            switch (choice) {
            case 'a': {
                for (int i = 0; i < 5; i++) {
                    cout << people[i].fullname << endl;
                }
                break;
            }
            case 'b': {
                for (int i = 0; i < 5; i++) {
                    cout << people[i].title << endl;
                }
                break;
            }
            case 'c': {
                for (int i = 0; i < 5; i++) {
                    cout << people[i].bopname << endl;
                }
                break;
            }
            case 'd': {
                for (int i = 0; i < 5; 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;
                    }
                }   //for
                break;
            }   //case 'd'
            }   //switch
            if (choice == 'q') {
                cout << "再见!" << endl;
                break;
            }
        }   //if
        else {
            cout << "输入有误,请重新输入:";
        }
    }
    return 0;
}

6.11.5

编写程序,显示应交的税,下面是分段应交的税款百分比
0~5000:0%
5001~15000:10%
15001~35000:15%
35000~:20%

#include <iostream>

using namespace std;

/*
应交的税
0~5000:0%
5001~15000:10%
15001~35000:15%
35000~:20%
*/

int main()
{
    double money;
    double shui;
    while (cin >> money) {
        if (money <= 5000) {
            shui = 0;
        }
        else if (money > 5000 && money <= 15000) {
            shui = (money - 5000) * 0.1;
        }
        else if (money > 15001 and money <=35000) {
            shui = (15000 - 5000) * 0.1 + (money - 15000) * 0.15;
        }
        else {
            shui = (15000 - 5000) * 0.1 + (35000 - 15000) * 0.15 + (money - 35000) * 0.2;
        }
        cout << "应交的税" << shui << endl;
    }
    return 0;
}

6.11.6

编写程序,用户输入捐献者数目,然后输入每个捐献者的姓名和款项,这些信息被存储在一个动态数组中,每个结构有两个成员,姓名和款项,读取所有数据以后,程序将显示所有的捐款超过10000的捐款者姓名以及捐款数额
有两个类别:重要捐款人和普通捐款人在这里插入代码片,如果没有某种类别,则显示none

#include <iostream>

using namespace std;

/*
编写程序,用户输入捐献者数目,然后输入每个捐献者的姓名和款项
这些信息被存储在一个动态数组中
每个结构有两个成员,姓名和款项
读取所有数据以后,程序将显示所有的捐款超过10000的捐款者姓名以及捐款数额
有两个类别:重要捐款人和普通捐款人
如果没有某种类别,则显示none
*/

struct people {
    string name;
    double money;
};

int main()
{
    int num;
    cout << "请输入总共有多少人捐款:";
    cin >> num;
    people *p = new people[num];
    for (int i = 0; i < num; i++) {
        cout << "请输入第" << i+1 << "个人的姓名:";
        cin >> p[i].name;
        cout << "请输入第" << i+1 << "个人的捐款款项:";
        cin >> p[i].money;
    }
    cout << "重要捐款人:" << endl;
    for (int i = 0; i < num; i++) {
        if (p[i].money >= 10000) {
            cout << p[i].name << "捐款数目:" << p[i].money << endl;
        }
    }
    cout << "普通捐款人:" << endl;
    for (int i = 0; i < num; i++) {
        if (p[i].money < 10000) {
            cout << p[i].name << "捐款数目:" << p[i].money << endl;
        }
    }
    return 0;
}

6.11.7

编写程序,每次读取一个单词,直到用户只输入q,该程序指出有多少个单词以元音打头,有多少个单词以辅音打头,有哪些单词不属于这两类

#include <iostream>
#include <cctype>
#include <cstring>

using namespace std;

/*
编写程序,每次读取一个单词,直到用户只输入q
该程序指出有多少个单词以元音打头,有多少个单词以辅音打头,有哪些单词不属于这两类
*/

int main()
{
    char word[20];
    int num_y = 0;  //元音
    int num_f = 0;  //辅音
    int other = 0;
    cin >> word;
    while (strcmp(word, "q")) {
        if (isalpha(word[0])) {
            switch (word[0]) {
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
            case 'A':
            case 'E':
            case 'I':
            case 'O':
            case 'U':
                num_y++;
                break;
            default:
                num_f++;
                break;
            }
        }
        else {
            other++;
        }
        cin >> word;
    }
    cout << "元音打头的有" << num_y << "个" << endl;
    cout << "辅音打头的有" << num_f << "个" << endl;
    cout << "其他打头的有" << other << "个" << endl;
    return 0;
}

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逸人止

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值