C++ Primer Plus 第6版 中文版 第6章编程练习

本文集合了多个C++编程实例,包括字符转换、捐赠平均数计算、菜单选择、结构体数组操作、税收计算等,展示了C++语言的基础用法及常见问题解决技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、

#include <iostream>
#include <cctype>

using namespace std;

int main()
{
    char ch;

    while(cin.get(ch) && ch != '@')
    {
        if(islower(ch))
            //toupper(ch);
            ch -= 32;
        else if(isupper(ch))
            //tolower(ch);
            ch += 32;
        else if(isdigit(ch))
            continue;

        cout << ch;
    }

    return 0;
}

2、

#include <iostream>

using namespace std;

int main()
{
    int  i, count;
    double ch, sum, average;
    double donation[10] = {0};

    i = count = 0;
    ch = sum = average = 0;

    while( (i<10) && (cin >> ch) )
    {
        donation[i] = ch;
        sum += ch;
        i++;
    }

    average = sum / i;

    for(int j=0; j<i; j++)
    {
        cout << donation[j] << " ";
        if(donation[j] > average)
            count++;
    }

    cout << "Average: " << average << " Number: " << count << endl;

    return 0;
}

3、

#include <iostream>

using namespace std;

int main()
{
    cout << "Please enter one of the following choices:" << endl;
    cout << "c) carnivore   p) pianist" << endl;
    cout << "t) tree        g) game" << endl;
    cout << "Please enter a c, p, t, or g: ";

    char ch;

    while(cin.get(ch))
    {
        if((ch=='c') || (ch=='p') || (ch=='t') || (ch=='g'))
            break;
        cout << "Please enter a c, p, t, or g: ";
        cin.get(); //去掉回车换行符
    }

    switch (ch)
    {
    case 'c':
        cout << "A maple is carnivore." << endl;
        break;
    case 'p':
        cout << "A maple is pianist." << endl;
        break;
    case 't':
        cout << "A maple is tree." << endl;
        break;
    case 'g':
        cout << "A maple is game." << endl;
        break;
    default:
        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 display(bop p[])
{
    for(int i=0; i<4; i++)
    {
        int tmp = p[i].preference;
        switch (tmp) {
        case 0:
            cout << p[i].fullname << endl;
            break;
        case 1:
            cout << p[i].title << endl;
            break;
        case 2:
            cout << p[i].bopname << endl;
            break;
        default:
            break;
        }
    }
}

int main()
{
    char ch;

    bop b[4] = {
        {"Wimp Macho", "coder", "wip", 2},
        {"Raki Rhodes", "typist", "rak", 1},
        {"Celia Laiter", "teacher", "cel", 0},
        {"Hoppy Hipman", "student", "hop", 1}
    };

    cout << "Benevolent Order of Programmers Report" << endl;
    cout << "a. display by name     b. display by title" << endl;
    cout << "c. display by bopname  d. display by preference" << endl;
    cout << "q. quit" << endl;
    cout << "Enter your choice: ";

    cin >> ch;
    while(ch != 'q')
    {
        switch (ch) {
        case 'a':
            for(int i=0; i<4; i++)
                cout << b[i].fullname << endl;
            break;
        case 'b':
            for(int i=0; i<4; i++)
                cout << b[i].title << endl;
            break;
        case 'c':
            for(int i=0; i<4; i++)
                cout << b[i].bopname << endl;
            break;
        case 'd':
            display(b);
            break;
        default:
            break;
        }

        cout << "Next choice: ";
        cin >> ch;
    }

    cout << "Bye!" << endl;

    return 0;
}

5、

#include <iostream>

using namespace std;

int main()
{
    double  income, revenge;

    cout << "Please enter your income: ";
    while( (cin >> income) && (income >= 0) )
    {
        if(income <= 5000)
            revenge = 0;
        else if(income <= 15000)
            revenge = 5000*0.00 + (income-5000)*0.10;
        else if(income <= 35000)
            revenge = 5000*0.00 + 10000*0.10 + (income-15000)*0.15;
        else
            revenge = 5000*0.00 + 10000*0.10 + 20000*0.15 + (income-35000)*0.20;

        cout << "revenge = " << revenge << endl;
        cout << "Please enter your income: ";
    }

    return 0;
}

6、

#include <iostream>

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

int main()
{
    int count;
    int flag1 = 0, flag2 = 0;

    cout << "Please enter patrons number:";
    cin >> count;
    patrons *p = new patrons[count];

    for(int i=0; i<count; i++)
    {
        cout << "#" << (i+1) << " patrons: " << endl;
        cout << "name: ";
        cin.get();
        cin.getline(p[i].name, 20);
        cout << "money: ";
        cin >> p[i].money;
    }

    cout << "\nGrand Patrons\n";
    for(int i=0; i<count; i++)
    {
        if(p[i].money >= 10000)
        {
            cout << p[i].name << endl;
            flag1 = 1;
        }
    }
    if(flag1 == 0)
        cout << "none" << endl;

    cout << "\nPatrons\n";
    for(int i=0; i<count; i++)
    {
        if(p[i].money < 10000)
        {
            cout << p[i].name << endl;
            flag2 = 1;
        }
    }
    if(flag2 == 0)
        cout << "none" << endl;

    delete [] p;

    return 0;
}

7、

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

using namespace std;

int main()
{
    int vowels, consonants, others;
    char ch, word[20];

    vowels = consonants = others = 0;

    while(cin >> word)
    {
        ch = word[0];
        if((strlen(word)==1) && (ch=='q'))
            break;
        if(isalpha(ch))
        {
            if( (ch=='a') || (ch=='e') || (ch=='i') || (ch=='o') || (ch=='u'))
                vowels++;
            else
                consonants++;
        }
        else
        {
            others++;
        }
    }

    cout << vowels << " words beginning with vowels" << endl
         << consonants << " words beginning with consonants" << endl
         << others << " others" << endl;

    return 0;
}

8、

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{
    ifstream inFile;
    char ch;
    int count = 0;

    inFile.open("test.txt");
    if(!inFile.is_open())
    {
        cout << "Could not open the file " << endl;
        exit(EXIT_FAILURE);
    }

    while(inFile >> ch)
    {
        count++;
    }

    cout << "Characters: " << count << endl;

    inFile.close();

    return 0;
}

9、

#include <iostream>
#include <fstream>
#include <cstdlib>

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

int main()
{
    int count;
    int flag1 = 0, flag2 = 0;
    ifstream inFile;

    inFile.open("test.txt");
    if(!inFile.is_open())
    {
        cout << "Could not open the file " << endl;
        exit(EXIT_FAILURE);
    }

    cout << "Please enter patrons number:";
    inFile >> count;
    patrons *p = new patrons[count];

    for(int i=0; i<count; i++)
    {
        cout << "#" << (i+1) << " patrons: " << endl;
        cout << "name: ";
        inFile.get();
        inFile.getline(p[i].name, 20);
        cout << "money: ";
        inFile >> p[i].money;
    }

    cout << "\nGrand Patrons\n";
    for(int i=0; i<count; i++)
    {
        if(p[i].money >= 10000)
        {
            cout << p[i].name << endl;
            flag1 = 1;
        }
    }
    if(flag1 == 0)
        cout << "none" << endl;

    cout << "\nPatrons\n";
    for(int i=0; i<count; i++)
    {
        if(p[i].money < 10000)
        {
            cout << p[i].name << endl;
            flag2 = 1;
        }
    }
    if(flag2 == 0)
        cout << "none" << endl;

    delete [] p;

    return 0;
}




 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值