C++ Primer Plus 第6章 课后编程练习 代码

1#include <cctype>
#include <iostream>
int main()
{
    using namespace std;
    cout << "Please input the letters(@ to quit):\n";
    char ch;
    cin.get(ch);
    while (ch != '@')
    {
        if (isupper(ch))
        {
            ch = tolower(ch);
        }
        else
        {
            ch = toupper(ch);
        }
        // cout.put(ch);//输出方案1
        cout << ch; // 输出方案2

        cin.get(ch);
    }

    cout << "Bye!" << endl;

    return 0;
}
2#include <array>
#include <cstdlib>
#include <iostream>
const int SIZE = 10;
int main()
{
    using namespace std;
    cout << "You can input the value of donation(Up to 10 numbers):\n";
    array<double, SIZE> Dona;
    int i = 0;
    double sum = 0;

    while (cin >> Dona[i]) //cin>>double返回的是读取数据是否成功,成功则进入循环
    {
        sum += Dona[i];
        ++i;
        cout << "Input the next value(non-number to quit):";
    }
    double Average = sum / i;
    cout << "Average: " << Average << endl;

    int count = 0;
    for (int j = 0; j < i; j++)
    {
        if (Dona[j] > Average)
        {
            ++count;
            cout << Dona[j] << "\t";
        }
    }
    cout << endl;

    cout << count << " value(s) are bigger than average.\n";

    return 0;
}
3#include <cctype>
#include <iostream>
using namespace std;
void showmenu();

int main()
{
    cout << "Please enter one of the following choice: \n";
    showmenu();
    char ch;
    cin >> ch;
    ch = tolower(ch);
    while (ch != 'c' && ch != 'p' && ch != 't' && ch != 'g' && ch != 'f')
    {
        cout << "Please enter a c,p,t, or g:";
        cin >> ch;
    }

    switch (ch)
    {
    case 'c':
        cout << "Your choice is carnivore.\n";
        break;
    case 'p':
        cout << "Your choice is pianist.\n";
        break;
    case 't':
        cout << "Your choice is tree.\n";
        break;
    case 'g':
        cout << "Your choice is game.\n";
        break;
    default:
        break;
    }
    // cout << "You can input your next choices:\n";
    return 0;
}
void showmenu()
{
    cout << "c)   carnivore      p)   pianist\n"
         << "t)   tree           g)   game\n"
         << "f)   quit\n";
}
4#include <iostream>
using namespace std;
const int strsize = 40;
struct bop
{
    char fullname[strsize];
    char title[strsize];
    char bopname[strsize];
    int preference;
};
void showmenu();

int main()
{
    //info of bop
    bop arr_member[5] =
        {
            {"Wimp Macho", "Professor", "Kate", 0},
            {"Raki Rhodes", "Academician", "Junior Programmer", 2},
            {"Celia Laiter", "Professor", "MIPS", 2},
            {"Hoppy Hipman", "Analyst Trainee", "LUCKY boy", 1},
            {"Pat Hand", "Student", "LOOPY", 2}};
    cout << "Benevolent Order of Programmers Report\n";
    showmenu();
    cout << "Enter your chioces: ";
    //creat a val of choice
    char choice;
    cin >> choice;
    // corresponding to eace choice
    while (cin.good() && choice != 'q')
    {
        switch (choice)
        {
        case 'a':
            for (int i = 0; i < 5; i++)
            {
                cout << arr_member[i].fullname << endl;
            }
            break;
        case 'b':
            for (int i = 0; i < 5; i++)
            {
                cout << arr_member[i].title << endl;
            }
            break;
        case 'c':
            for (int i = 0; i < 5; i++)
            {
                cout << arr_member[i].bopname << endl;
            }
            break;
        case 'd':
            for (int i = 0; i < 5; i++)
            {
                switch (arr_member[i].preference)
                {
                case 0:
                    cout << arr_member[i].fullname << endl;
                    break;
                case 1:
                    cout << arr_member[i].title << endl;
                    break;
                case 2:
                    cout << arr_member[i].bopname << endl;
                    break;
                }
            }

            break;
        default:
            break;
        }
        cout << "Next choice: ";
        cin >> choice;
    }
    cout << "Bye!\n";

    return 0;
}

void showmenu()
{
    cout << "a.display by name      b.display by title\n"
         << "c.display by bopname   d.display by preference\n"
         << "q.quit\n";
}
5#include <iostream>
int main()
{
    using namespace std;
    double tax;
    double income;
    cout << "Enter your income (unit in tvarps): ";

    cin >> income;
    while (cin.good() && income >= 0)
    {
        if (income <= 5000)
        {
            tax = 0.0;
        }
        else if (income > 5000 && income <= 15000)
        {
            tax = (income - 5000) * 0.1;
        }
        else if (income > 15000 && income <= 35000)
        {
            tax = (income - 15000) * 0.15 + 10000 * 0.1;
        }
        else if (income > 35000)
        {
            tax = (income - 35000) * 0.2 + 20000 * 0.15 + 10000 * 0.1;
        }
        cout << "The tax = " << tax << " tvarps.\n";

        cout << "You can enter next income(unit in tvarps): ";
        cin >> income;
    }
    cout << "Byebye!\n";

    return 0;
}
6#include <iostream>
// #include <string>
#include <vector>
using namespace std;
struct Member
{
    char name[50];
    double money;
};

int main()
{
    cout << "Please enter the number of donors:\n";
    int num;
    cin >> num;
    cin.get();
    vector<Member> Mem_lib(num);
    cout << "Please enter the first donor's name:\n";
    int i = 0;
    cin.getline(Mem_lib[i].name, 50);

    // cin >> Mem_lib[i].name;

    while (i < num && cin.good() && Mem_lib[i].name[0] != '\n') //empty line to quit
    {
        //cin.get();
        cout << "Enter the money:\n";
        cin >> Mem_lib[i].money;
        cin.get(); //在此处插入cin.get()的原因是为了消耗掉末尾的换行符'\n',因为cin读取数据和scanf类似是以空白符作为结尾的,并且会把
        //换行符留在输入队列中,进而影响cin.getline()读取字符串;但是并不影响cin读取数据,因为cin在读取数据时,会忽略遇到有效字符前的空白字符

        i++;
        if (i < num)
        {
            cout << "Please enter the next donor 's name: \n";
            cin.getline(Mem_lib[i].name, 50); //读取数据,遇到换行符就停止读取,所以上一次读取数据后如果把换行符留在输入队列中,将会影响本次读取
        }
    }

    cout << "Grand Patrons\n";
    int count = 0;
    for (int j = 0; j < num; j++)
    {
        if (Mem_lib[j].money >= 10000)
        {
            cout << Mem_lib[j].name << "\t" << Mem_lib[j].money << endl;
            ++count;
        }
    }
    if (count == 0)
    {
        cout << "None!" << endl;
    }

    cout << "Patrons\n";
    int count2 = 0;
    for (int j = 0; j < num; j++)
    {
        if (Mem_lib[j].money < 10000)
        {
            cout << Mem_lib[j].name << "\t" << Mem_lib[j].money << endl;
            ++count2;
        }
    }
    if (count2 == 0)
    {
        cout << "None!" << endl;
    }

    return 0;
}
7#include <cctype>
#include <iostream>
#include <string>
using namespace std;
int main()
{

    cout << "Enter words(q to quit):\n";
    string word;
    cin >> word;

    int v_count = 0;
    int c_count = 0;
    int others = 0;
    //(1 == word.size()) &&
    while ((word.size() != 1) || word[0] != 'q')
    {
        if (isalpha(word[0]))
        {
            word[0] = tolower(word[0]);

            switch (word[0])
            {
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
                ++v_count;
                break;

            default:
                ++c_count;
                break;
            }
        }
        else
        {
            ++others;
        }
        //cin.get(); //每次读取下一个单词前,读走一个字符,用于排除'\n'
        cin >> word;
    }

    cout << v_count << " words beginning with vowels\n";
    cout << c_count << " words beginning with consonants\n";
    cout << others << " others\n";

    return 0;
}
8#include <fstream>
#include <iostream>

int main()
{
    using namespace std;
    ifstream fin;
    fin.open("char.txt");
    if (!fin.is_open())
    {
        cout << "Wrong!Open file failed.\n";
    }
    char ch;
    fin.get(ch);
    int count = 0;
    while (fin.good())
    {
        ++count;
        fin.get(ch);
    }

    if (fin.eof())
    {
        cout << "End of file reached!\n";
    }
    else if (fin.fail())
    {
        cout << "Type is mismatch\n";
    }
    else
    {
        cout << "Wrong! But I don't konw the reason\n";
    }

    cout << "The text has " << count << " characters.\n";
    return 0;
}
9#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
char filename[50];
struct Member
{
    char name[50];
    double money;
};

int main()
{
    ifstream fin;
    cout << "Please input the name of data file: \n";
    cin >> filename;
    fin.open(filename);
    int num;
    fin >> num;
    fin.get();
    vector<Member> Mem_lib(num);

    int i = 0;
    fin.getline(Mem_lib[i].name, 50);

    while (i < num && cin.good() && Mem_lib[i].name[0] != '\n') //empty line to quit
    {

        fin >> Mem_lib[i].money;
        fin.get();
        //在此处插入cin.get()的原因是为了消耗掉末尾的换行符'\n',因为cin读取数据和scanf类似是以空白符作为结尾的,并且会把
        //换行符留在输入队列中,进而影响cin.getline()读取字符串;但是并不影响cin读取数据,因为cin在读取数据时,会忽略遇到有效字符前的空白字符

        i++;
        if (i < num)
        { //读取数据,遇到换行符就停止读取,所以上一次读取数据后如果把换行符留在输入队列中,将会影响本次读取
            fin.getline(Mem_lib[i].name, 50);
        }
    }

    cout << "\nGrand Patrons\n";
    int count = 0;
    for (int j = 0; j < num; j++)
    {
        if (Mem_lib[j].money >= 10000)
        {
            cout << Mem_lib[j].name << "\t" << Mem_lib[j].money << endl;
            ++count;
        }
    }
    if (count == 0)
    {
        cout << "None!" << endl;
    }

    cout << "\nPatrons\n";
    int count2 = 0;
    for (int j = 0; j < num; j++)
    {
        if (Mem_lib[j].money < 10000)
        {
            cout << Mem_lib[j].name << "\t" << Mem_lib[j].money << endl;
            ++count2;
        }
    }
    if (count2 == 0)
    {
        cout << "None!" << endl;
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

咖啡与乌龙

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

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

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

打赏作者

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

抵扣说明:

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

余额充值