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

目录

        6.1 

        6.2 

        6.3

        6.4

        6.5

        6.6

        6.7

        6.8

        6.9 


6.1 编写一个程序, 读取键盘输入, 直到遇到@符号为止, 并回显输入(数字除外), 同时将大写字符转换为小写, 将小写字符转换为大写(别忘了cctype函数系列)。
#include <iostream>
#include <cctype>
using namespace std;
int main() {
    char ch;
    while(cin.get(ch) && ch != '@')
    {
        if(isupper(ch) == true)
            ch = tolower(ch);
        else
            ch = toupper(ch);
        cout<<ch;
    }
    return 0;
}
6.2 编写一个程序, 最多将10个donation 值读入到一个doub le数组中(如果您愿意, 也可使用模板类array)。 程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于 平均值。
#include <iostream>
#include <vector>
using namespace std;
int main() {
    vector<double> arr(10,0);
    int index = 0,count = 0;
    double sum = 0;
    cout<<"输入一个double类型的数字:"<<endl;
    while(cin>>arr[index] && index < 10)
    {
        cout<<"继续输入一个double类型的数字:";
        sum += arr[index];
        index++;
    }
    double ave = sum/index;
    for(auto a : arr)
    {
        if(a > ave)
            count++;
    }
    cout<<"数组的平均值为:"<<ave<<" \n其中有"<<count<<"个数字是大于平均值的"<<endl;
    return 0;
}
6.3编写一个菜单驱动程序的雏形。该程序显示一个提供4个选项的菜单 每个选项用一个字母标记。 如果用户使用有效选项之外的字母进行响应,程序将提示用户输入一个有效的字母,直到用户这样做为止。 然后, 该程序使用一条switch语句, 根据用户的选择执行一个简单操作。 该程序的运行情况如下:
#include <iostream>
using namespace std;
void show()
{
    cout<<"Please enter one of the following choices:"<<endl;
    cout<<"c) carnivore    p) pianist"<<endl;
    cout<<"t) tree         g) game"<<endl;
}
int main() {
    show();
    char ch;
    while(cin.get(ch))
    {
        show();
        switch (ch) {
            case 'c':
                cout<<"A maple is a carnivore\n";break;
            case 'p':
                cout<<"A maple is a pianist\n";break;
            case 't':
                cout<<"A maple is a t\n";break;
            case 'g':
                cout<<"A maple is a game\n";break;
            default:
                break;
        }
        cin.clear(); //flush the flag
        cin.sync(); //clear the buffer
        //为什么要这两行呢?因为cin都是先缓存再读取的,而回车符号也是会存进去的
        //cin在遇到空格、回车、以及tab键都会结束读取,此时若输出缓冲区还有数据,
        // 则下一个cin会直接取走输入,而不会请求键盘输入,对于回车,最后的enter也会保存在缓冲区。
    }
    return 0;
}
6.4加入BenevolentOrder of Programmer后, 在BOP大会上, 人们便可以通过加入者的真实姓名、 头衔或秘密BOP姓名来了解他(她)。 请编写一个程序, 可以使用真实姓名、 头衔、 秘密姓名或成员偏好来列出成员。 编写该程序时, 请使用下面的结构:
#include <iostream>
#include <unistd.h>
using namespace std;

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

bop temp[] = {
        {"wyz","Mr wu","xiao wu",0},
        {"zwl","Miss zuo","xiao zuo",1},
        {"lyr","Mr luo","Xiao luo",2},
        {"dzh","Mr hua","hua ge",2}
};

void show(){
    cout<<"Benevolent Order of Programmers Report"<<endl;
    cout<<"a. display by name     b. display by title\n";
    cout<<"c. display by bopname  d. display by preference\n";
    cout<<"q. quit\n";
    cout<<"Enter your choice:";
}
void displayByName(){
    for(auto a : temp)
    {
        cout<<a.fullname<<endl;
    }
};
void displayByTitle(){
    for(auto a : temp)
    {
        cout<<a.title<<endl;
    }
};
void displayBybopname(){
    for(auto a : temp)
    {
        cout<<a.bopname<<endl;
    }
};
void displayBypre(){
    for(auto a : temp)
    {
        switch (a.preference) {
            case 0 :
                cout<<a.fullname<<endl;break;
            case 1:
                cout<<a.title<<endl;break;
            case 2:
                cout<<a.bopname<<endl;break;
            default:break;
        }
    }
};

int main() {
    show();
    char ch;
    while(cin.get(ch) && ch != 'q')
    {
        switch (ch) {
            case 'a': displayByName();
                break;
            case 'b': displayByTitle();
                break;
            case 'c':displayBybopname();
                break;
            case 'd':displayBypre();
                break;
            default:break;
        }
        sleep(1);
        show();
        cin.sync();
    }
    return 0;
}
6.5在Neut ronia王国, 货币单位是t varp, 收入所得税的计算方式如下:

5000 tvarps: 不收税 ;5001,,...._,15000 tvarps: 10%;15001,,...._,35000 tvarps: 15%;5000 tvarps 以上: 20%。

#include <iostream>
using namespace std;



int main() {
    double salary;
    double taxation = 0;
    while(cin>>salary && salary >= 0)
    {
        if(salary <= 5000)
            taxation = 0;
        else if(salary > 5000 && salary <= 15000)
            taxation = (salary - 5000)*0.1;
        else if(salary > 15000 && salary <= 35000)
            taxation = (salary -15000)*0.15 + 1000;
        else
            taxation = 4000 +  (salary -35000)*0.20;
        cout<<taxation<<endl;
    }
    return 0;
}
6.6编写一个程序, 记录捐助给 “维护合法权利团体” 的资金。 该程序要求用户输入捐献者数目, 然后要求用户输入每一个捐献者的姓名和款项。 这些信息被储存在一个动态分配的结构数组中。 每个结构有两个成员: 用来储存姓名的字符数组(或 string 对象)和用来存储款项的 double 成员。 读取所有的数据后,程序将显示所有捐款超过 10000 的捐款者的姓名及其捐款数额。 该列表前应包含一个标题, 指出下面的捐 款者是重要捐款人 (Grand Patrons)。 然后, 程序将列出其他的捐款者, 该列表要以 Parons 开头。 如果某种类别没有捐款者, 则程序将打印单词 “ none ”。该程序只显示这两种类别, 而不进行排序。
#include <iostream>
#include <vector>
using namespace std;
struct donation{
    string name;
    double money;
};
bool flag = false;
void showgrand(const vector<donation> arr){
    cout<<"Grand Patrons\n";
    for(auto a : arr)
    {
        if(a.money > 10000){
            cout<<a.name<<" "<<a.money<<endl;
            flag = true;
        }
    }
    if(flag == false)
        cout<<"None"<<endl;
    flag = false;
}
void shownotgrand(const vector<donation> arr){
    cout<<"Patrons\n";
    for(auto a : arr)
    {
        if(a.money < 10000){
            cout<<a.name<<" "<<a.money<<endl;
            flag = true;
        }
    }
    if(flag == false)
        cout<<"None"<<endl;
    flag = false;
}
int main() {
    int number;
    cout<<"请输入捐款的总人数:";
    cin>>number;
    cin.sync();
    //这里一定要清空缓存,不然缓存里的回车符号会对下面string的输入产生很大的影响;
    vector<donation> arr(number);

    for(int i = 0;i < number;i++)
    {
        cout<<"输入捐款人的姓名:";
        getline(cin,arr[i].name);
        cout<<"输入捐款的金额:";
        cin>>arr[i].money;
        cin.sync();
    }
    showgrand(arr);
    shownotgrand(arr);

    return 0;
}
6.7编写一个程序, 它每次读取一个单词, 直到用户只输入 q。然后, 该程序指出有多少个单词以元音打头, 有多少个单词以辅音打头, 还有多少个单词不属千这两类。 为此, 方法之一是, 使用 isalpha( )来区 分以字母和其他字符打头的单词, 然后对千通过了 isalpha( )测试的单词, 使用 if 或 switch 语句来确定哪些以元音打头。该程序的运行情况如下:
#include <iostream>
#include <cctype>
using namespace std;
int main() {
    string temp;
    int other = 0,vowel = 0,count = 0;
    //这里为什么是cin呢?因为cin是识别不出来空格的,
    //所以它能够很好的降内一个字符以空格的形式分开
    while(cin>>temp && temp != "q")
    {
        if(!isalpha(temp[0]))
            other++;
        else{
            switch (temp[0]) {
                case 'a':
                case 'e':
                case 'i':
                case 'o':
                case 'u':
                    vowel++;break;
                default:
                    count++;break;
            }
        }
    }
    cout<<vowel<<endl;
    cout<<count<<endl;
    cout<<other<<endl;
    return 0;
}
6.8编写一个程序, 它打开一个文件文件, 逐个字符地读取该文件, 直到到达文件末尾, 然后指出该文件中包含多少个字符。

6.9 完成编程练习6, 但从文件中读取所需的信息。 该文件的第一项应为捐款人数, 余下的内容应为成 对的行。 在每一对中, 第一行为捐款人姓名, 第二行为捐款数额。 即该文件类似千下面:
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值