整理下C++primer plus 第六版第六章习题。

#include <iostream>
#include<cctype>
#include<array>
#include<string>
#include<fstream>
using namespace std; //头文件为了偷懒,把所有题目有用到过的都写在一块了
第一题

int main() 

{
    char word;
    while (cin.get(word) && word != '@')
    {
        if (islower(word))
            cout << static_cast<char>(toupper(word)) << endl;
        else if (isupper(word))
            cout << static_cast<char>(tolower(word)) << endl;
        else
            continue;
    }
    int m;
    cin >> m;
    return 0;

}


第二题

int main() 
{
    double count = 0.0, average, sum = 0.0;
    array<double, 10> love;
    for (int i = 0; i < 10; i++)
    {
        count = 0;
        while (!(cin >> love[i]))
        {
            cin.clear();
            while (cin.get() != '\n')
                continue;
            cout << "pleasr enter a number:";
        }
        sum += love[i];
        average = sum / (i + 1);
        for (int j = 0; j <= i; j++)
            if (love[j] > average)
                count++;
        cout << "总数为:" << sum << endl;
        cout << "平均数为:" << average << endl;
        cout << "共有" << count << "个数字大于平均数" << endl;
    }
    int m;
    cin >> m;
    return 0;

}


第三题

int main()
{
    double donation[10], sum = 0;
    int i, count = 0;
    for (i = 0; i < 10; ++i) {
        if (cin >> donation[i])
            sum += donation[i];
        else break;
    }
    sum /= i;
    for (int j = 0; j < i; ++j) {
        if (donation[j] > sum)
            ++count;
    }
    cout << sum << endl << count << endl;
    system("pause");
    return 0;

}


第四题

void show();
int main()
{
    show();
    char ch;
    cin >> ch;
    while (ch!='q')
    {
        switch (ch)
        {
        case 'c':cout << "carnivore\n";
            break;
        case 'p':cout << "pianist\n";
            break;
        case 't':cout << "A maple is a tree\n";
            break;
        case 'g':cout << "game\n";
            break;
        default:cout << "please enter a c,p,t or g:\n";
        }
        show();
        cin>>ch;                                           
    }
    system("pause");
    return 0;
}
void show()
{
    cout << "please enter one of the following choices:\n";
    cout << "c)carnivore\t p)pianist\nt)tree\t g)game"<< endl;

}


第五题   //第五题保存了两个源文件,这个是自己写的,下面有个是用来参考的。

void show();  
void count_name();
void count_title();
void count_bopname();
void count_preference();
struct bop //创建BOP结构
{
    char fullname[20];
    char title[20];
    char bopname[20];
    int preference;
};
bop bopstruct[5] =
{
    { "Wimp Macho","Wimp Macho","James", 1 },
    { "Raki Rhodes","Junior Programmer","Stephen",2 },
    { "Celia Laiter","MIPS","YaoMing",2 },
    { "Hoppy Hipman","Analyst Trainee","Athoney",1 },
    { "Pat Hand","LOOPY","Iverson",0 }
};
int main()
{
    show();
    char ch;
    cin>>ch;
    while (ch != 'q')
    {
        switch (ch)
        {
        case'a':count_name(); break;
        case'b':count_title(); break;
        case'c':count_bopname(); break;
        case'd':count_preference(); break;
        default:cout << "It's not a right choice\n";
        }
            cout << "pleast input the next choice:_\b";
            cin>>ch;
    }
    system("pause");
    return 0;
 }
void show()
{
    cout << "Benevolent Order of Programmers Report\n";
    cout << "a.display by name\t b.display by title\nc.display by bopname\t d.display by perference\nq.quit\n";
}
void count_name()
{
    for (int i = 0; i < 5; i++)
        cout <<bopstruct[i].fullname << endl;
}
void count_title()
{
    for (int i = 0; i < 5; i++)
        cout << bopstruct[i].title << endl;
}
void count_bopname()
{
    for (int i = 0; i < 5; i++)
        cout << bopstruct[i].bopname << endl;
}
void count_preference()
{
    for (int i = 0; i < 5; i++)
    {
        switch (bopstruct[i].preference)
        {
        case 0:cout << bopstruct[i].fullname << endl; break;
        case 1:cout << bopstruct[i].title << endl; break;
        case 2:cout << bopstruct[i].bopname << endl; break;
        default:cout << "error";
        }
    }

}


第五题   //用来参考的

const int strsize = 20;
const int NUM = 5;
struct bop {
    char fullname[strsize]; // real name  
    char title[strsize];    // job title  
    char bopname[strsize];  // secret BOP name  
    int preference;         // 0 = fullname , 1 = title , 2 = bopname  
};
void display_by_name();
void display_by_title();
void display_by_bopname();
void display_by_preference();
bop people[5] = {
    {
        "Wimp Macho",
        "BOSS",
        "WM",
        0
    },
    {
        "Raki Rhodes",
        "Manager",
        "Junior Programmer",
        2
    },
    {
        "Celia Laiter",
        "MIPS",
        "CL",
        1
    },
    {
        "Hoppy Hipman",
        "Analyst Trainee",
        "AT",
        1
    },
    {
        "Pat Hand",
        "Student",
        "LOOPY",
        2
    }
};
char ch;
int main()
{
    cout << "a. display by name         b. display by title" << endl << "c. display by bopname      d. display by preference" << endl << "q. quit" << endl << "Enter your choice: ";
    while (cin >> ch && ch != 'q')
    {
        switch (ch)
        {
        case 'a': display_by_name(); break;
        case 'b': display_by_title(); break;
        case 'c': display_by_bopname(); break;
        case 'd': display_by_preference(); break;
        }
        cout << "Next choice: ";
    }
    cout << "Bye!" << endl;
    return 0;
}

void display_by_name() {
    for (int i = 0; i < NUM; ++i)
        cout << people[i].fullname << endl;
}

void display_by_title() {
    for (int i = 0; i < NUM; ++i)
        cout << people[i].title << endl;
}

void display_by_bopname() {
    for (int i = 0; i < NUM; ++i)
        cout << people[i].bopname << endl;
}

void display_by_preference() {
    for (int i = 0; i < NUM; ++i) {
        if (people[i].preference == 0)
            cout << people[i].fullname << endl;
        else if (people[i].preference == 1)
            cout << people[i].title << endl;
        else
            cout << people[i].bopname << endl;
    }

}

第六题

int main()
{
    cout << "please input your income:";
    double money,sum=0;
    cin >> money;
    while (cin && money > 0)
    {
        if (money > 35000)
            sum = 10000 * 0.1 + 20000 * 0.15 + (money - 35000)*0.20;
        else if (money > 15000 && money <= 35000)
            sum = 10000 * 0.1 + (money - 15000)*0.15;
        else if (money > 5000 && money <= 15000)
            sum = (money - 5000)*0.1;
        else
            sum = 0;
        cout << "your tax is:" << sum << endl;
        cout << "input your next income:";
        cin >> money;
    }
    system("pause");
    return 0;

}


第七题

int main()
{
    struct devotor {
        string name;
        double num;
     };
    cout << "input the number of patrons:";
    int pat, s=0,m=0;
    cin >> pat;
    devotor *sum = new devotor[pat];
    for (int i = 0; i < pat; i++)
    {
        cin >> sum[i].name;
        cin >> sum[i].num;
    }
    cout << "Grand patrons:\n";
    for (int j{ 0 }; j < pat; j++)
    {
        if (sum[j].num > 10000)
        {
            ++s;
            cout << sum[j].name << ":" << sum[j].num << endl;
        }
    }
    if (s == 0)
        cout << "none" << endl;
    cout << "Patrons:\n";
    for (int j = 0; j < pat; j++)
    {
        if (sum[j].num >= 0 && sum[j].num < 10000)
        {
            m++;
            cout << sum[j].name << endl;
        }
    }
    if (m == 0)
        cout << "none"<< endl;
    system("pause");
    return 0;

}


第八题

int main()
{
    cout << "please input a sentence,and input p to quit" << endl;
    string word;
    int vowel = 0;
    int consonants = 0;
    int other = 0;
    cin >> word;
    while (word!= "q"&&word!= "Q")//Char用‘’,string用""
    {
        if (isalpha(word[0]))
        {
            switch (word[0])
            {
            case'a':
            case'e':
            case'i':
            case'o':
            case'u':vowel++; break;
            default:consonants++;
            }
        }
        else
            other++;
        cin >> word;
    }
    cout << vowel << "words beginning with vowels" << endl;
    cout << consonants << "words beginning with consonants" << endl;
    cout << other << "  others" << endl;
    system("pause");
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值