c++ primer plus第六版第六章编程练习

  1. 编写程序读取键盘输入,回显除数字外字符,同时大写转小写,小写转大写,遇’@'就停止。
//练习6.1   读取键盘输入,回显输出(除数字),另外大写字母和小写字母互转,遇“@”则退出程序。
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
    char ch;
    while ((ch = cin.get()) != '@')     //遇'@'则停
    {
        if (isdigit(ch))                //判断是否为数字,是则跳过
        { 
            continue;
        }
        else if (islower(ch))           //判断是否为小写
        {
            cout << (char)toupper(ch);
        }
        else if (isupper(ch))           //判断是否为大写
        {
            cout << (char)towlower(ch);
        }
        else                            //输出字母数字以外字符
            cout << ch;
    }
    cout << "Done." << endl;
    system("pause");
    return 0;
}

在这里插入图片描述

  1. 最多输入10个donation,数值存储于数组(可用array实现),遇到非数字输入(我都不知道有那么调皮的人)停止,最后计算平均值和存储数值中超过平均值的数的数目。
    放码:
//练习6.2   最多输入10个捐献值到double数组中,遇到非数字结束输入
//并报告数字的平均值以及数组中有多少个数字大于平均值
#include <iostream>
using namespace std;
const int Max = 10;
int main()
{
    double donation[Max];
    double sum = 0.0;
    double average = 0.0;
    int n = 0;
    int lager_ave_n = 0;
    cout << "How many times do you want to enter:";
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cout << "Please enter the donation #" << i + 1 << ':';
        cin >> donation[i];
        sum += donation[i];
    }
    average = sum / n;
    cout << "The average value of the donation is " << average << '.' << endl;
    for (int j = 0; j < n; j++)
    {
        if (donation[j] > average)
            lager_ave_n++;
    }
    cout << "There are " << lager_ave_n << " donations exceed the average." << endl;
    system("pause");
    return 0;
}

在这里插入图片描述
我这边测试结果只进行了最大值输入进行测试,没有测试之外的,有问题请告知。
3. 编写驱动程序雏形,显示可选菜单,并对应输入相应。

//练习6.3   编写驱动程序雏形,提供四个选项的菜单
//每个选项有一个字母标记,用户输入有效选项以内的输入得到相应,否则提示输入
#include <iostream>
#include <cstdlib>
using namespace std;
const int Max = 10;
void showmenu();
void quit();
int main()
{
    showmenu();
    char choice;
    while (cin >> choice && choice != 'q')
    {
        switch (choice)
        {
        case 'a':
            cout << "Let's start the journey." << endl;
            break;
        case 'c':
            cout << "Really?Don't you wanna try?" << endl;
            break;
        case 'p':
            cout << "Panist is cool,right?" << endl;
            break;
        case 't':
            cout << "As the tree in the new world." << endl;
            break;
        case 'g':
            cout << "Here we go!" << endl;
            break;
        default:
            cout << "It's not invalid.Please try again:" << endl;
            break;
        }
        cout << "You can try another:" << endl;
    }
    quit();
    system("pause");
    return 0;
}
void showmenu()         //菜单展示
{
    cout << "Please enter one of the following choices:" << endl;
    cout << "a)start  c)cry   p)panist" << endl;
    cout << "t)tree   g)game  q)quit" << endl;
}
void quit()
{
    cout << "Bye." << endl;
    long long wait = 0;
    while (wait < 10000000000)          //设置一个退出时延
        wait++;
    exit(EXIT_SUCCESS);
}

在这里插入图片描述
在程序退出我设置了一个退出时延,其实就是让系统计算一个数累加而已,可自调。
4.

//练习6.4   用户可以通过加入者姓名、头衔和BOP来了解到个人
//结构体存储成员信息,结构体数组存储对应成员
//用户选择展示方式
//等熟悉后面标准库的使用,可以改成输入个人姓名(或BOP),系统输出个人信息,这样更符合期望
#include <iostream>
using namespace std;
const int strsize = 30;
const int Max = 5;
void showmenu();
void showname();
void showtitle();
void hopname();
void show_preference();
void quit();
struct bop
{
    char name[strsize];
    char bopname[strsize];
    char title[strsize];
    int preference;
};
bop person[Max] = {
    {"Wimp Macho", "Mach", "Junior Programmer", 1},
    {"Raki Rhodes", "Rhodes", "Intermediate Programmer", 2},
    {"Celia Laiter", "Laiter", "Senior Programmer", 3},
    {"Hoppy Hipman", "Hipman", "Green Hand", 0},
    {"Jack.H", "Jim", "Boss", 4},
};
int main()
{
    showmenu();
    char choice;
    while (cin >> choice && choice != 'q')
    {
        switch (choice)
        {
        case 'a':
            showname();
            break;
        case 'b':
            showtitle();
            break;
        case 'c':
            hopname();
            break;
        case 'd':
            show_preference();
            break;
        default:
            cout << "It's not invalid.Please try again:" << endl;
            break;
        }
        cout << "You can try another:" << endl;
    }
    quit();
    system("pause");
    return 0;
}
void showmenu() //菜单展示
{
    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  q)quit" << endl;
}
void showname()
{
    for (int i = 0; i < Max; i++)
    {
        cout << person[i].name << endl;
    }
}
void showtitle()
{
    for (int j = 0; j < Max; j++)
    {
        cout << '\t' << person[j].name << person[j].title << endl;
    }
    cout << endl;
}
void hopname()
{
    for (int k = 0; k < Max; k++)
    {
        cout << person[k].bopname << endl;
    }
}
void show_preference()
{
    int small = 0;
    for (int m = 0; m < Max; m++)
    {
        small = person[m].preference;
        switch (small)
        {
        case 0:
            cout <<person[3].name<<' '<<person[3].title << endl;
            break;
        case 1:
            cout <<person[0].name<<' '<< person[0].title << endl;
            break;
        case 2:
            cout <<person[1].name<<' '<< person[1].title << endl;
            break;
        case 3:
            cout <<person[2].name<<' '<< person[2].title << endl;
            break;
        default:
            cout <<person[4].name<<' '<< person[4].title << endl;
            break;
        }
    }
}
void quit()
{
    cout << "Bye." << endl;
    long long wait = 0;
    while (wait < 4000000000) //设置一个退出时延
        wait++;
    exit(EXIT_SUCCESS);
}

在这里插入图片描述

本来是想做得智能点的,结果越走越偏,就成这么臃肿了,有改进方法请指教指教。

  1. 编写一个计算纳税查询程序,其实可以加一个效果是遇特殊字符退出之类的,不过我懒就没改。
//练习6.5   编写计算纳税的程序
//货币:tvarp
//纳税规则:5000(不纳税);5001-15000(0.1);15001-35000(0.15);大于35000(0.2)
//简单计算38000tvarp收入就是:sum=5000x0.0+10000x0.10+20000x0.15+3000x0.2
#include <iostream>
using namespace std;
int main()
{
    double income = 0.0;
    double sum = 0.0;
    int n;
    cout << "Please enter your income:";
    cin >> income;
    n = income / 5000;
    switch (n)
    {
    case 0:
        cout << "You don't have to pay for tax.Sorry and happy for you." << endl;
        break;
    case 1:
    case 2:
    case 3:
        sum = (income - 5000) * 0.10;
        break;
    case 4:
    case 5:
    case 6:
    case 7:
        sum = (income - 15000) * 0.15 + 1000.0;
        break;
    default:
        sum = (income - 35000) * 0.2 + 3000.0 + 1000.0;
    }
    cout << "You should pay tax " << sum << " tvarps,thank you." << endl;
    system("pause");
    return 0;
}
  1. 捐款信息处理
//练习6.6   编写关于捐献人和捐款的信息处理程序
//建立结构体donate包含捐款人和捐款额度,建立动态结构体数组donator存储
//捐款超过10000的为重要捐款人(Grand Patrons),读取数据后,对重要捐款人信息进行列表显示
//输出列表以Patrons开头,无名捐款人用none显示
#include <iostream>
#include <string>
using namespace std;
const int Max = 10; //动态结构体数组大小
struct donation
{
    string name;
    double money;
};
int main()
{
    donation *donator = new donation[Max];
    donation *grand_patrons = new donation[Max];
    int m, n = 0;
    int k = 0;
    cout << "Please enter the numbers of the donators:";
    cin >> m;
    cin.get();
    for (int i = 0; i < m; i++)
    {
        cout << "Please input the name(Anonymous donator can replace with none):";
        getline(cin, donator[i].name);
        cout << "Please input the donation:";
        cin >> donator[i].money;
        cin.get();
    }
    for (int j = 0; j < m; j++)
    {
        if (donator[j].money >= 10000)
        {
            grand_patrons[k] = donator[j];
            k++;
        }
    }
    if (k)
    {
        cout << "Patrons" << endl;
        for (int z = 0; z < k + 1; z++)
        {
            cout << grand_patrons[z].name << '\t' << grand_patrons[z].money << endl;
        }
    }
    else
        cout << "There is no one donated over 10000." << endl;
    delete[] donator;
    delete[] grand_patrons;
    system("pause");
    return 0;
}

在这里插入图片描述

其实后面还有不恰当输出,但我到瓶颈了,就先放一放。
解决:把z<k+1改成z<k即可,哈哈哈
7. 编写识别输入字符信息的程序(代码是有问题的,没错,有问题,得不到理想输出,但我不知道怎么解决)

//练习6.7   单词输入存储
//直到只输入q就停止输入,指出包含多少个元音打头的单词和有多少个辅音打头单词,另外还有不在此类的数目
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
    int vowel = 0;
    int consonant = 0;
    int other = 0;
    string s;
    cout << "Enter words (q to quit):" << endl;
    while ((cin >> s))
    {
        if (s[0] = 'q' && s.size() == 1)
        {
            break;
        }
        else if (isalpha(s[0]))
        {
            if (s[0] == 'a' || s[0] == 'e' || s[0] == 'i' || s[0] == 'o' || s[0] == 'u')
                vowel++;
            else
                consonant++;
        }
        else
            other++;
    }
    cout << vowel << " words beginning with vowels," << endl;
    cout << consonant << " words beginning with consonant," << endl;
    cout << other << " others." << endl;
    system("pause");
    return 0;
}
  1. 读取文件,识别包含多少个单词
//练习6.8   编写打开文本文件程序,读取字符并输出包含字符数
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <cstdlib>
const int Max = 1000;
using namespace std;
int main()
{
    string note;
    ifstream inFile;
    cout << "Please enter the name fo data file:";
    getline(cin, note);
    inFile.open(note);
    while (!inFile.is_open())
    {
        cout << "Could not open the file " << note << endl;
        cout << "Programming terminating." << endl;
        exit(EXIT_FAILURE);
    }
    string str;
    int sum = 0;
    int count = 0;
    getline(inFile, str, ' ');
    while (inFile)
    {
        sum++;
        count++;
        inFile >> str;
    }
    if (inFile.eof())
        cout << "End of file reached." << endl;
    else if (inFile.fail())
        cout << "Input terminated by data mismatch." << endl;
    else
        cout << "Input terminated for unknown reason." << endl;
    if (count == 0)
        cout << "No data processed." << endl;
    else
        cout << "There are " << sum << " charcters." << endl;
    inFile.close();
    system("pause");
    return 0;
}

在这里插入图片描述

9.改写练习6,改为从文件读取捐献者信息,文本如下:
在这里插入图片描述

//练习6.9   在练习6的基础上改动,改成从文件读取信息
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int Max = 10; //动态结构体数组大小
struct donation
{
    string name;
    double money;
};
int main()
{
    donation *donator = new donation[Max];
    donation *grand_patrons = new donation[Max];
    int m, n = 0;
    int k = 0;
    string filename;
    cout << "Enter the name of the file:";
    getline(cin, filename);
    fstream inFile;
    inFile.open(filename);
    if (!inFile.is_open())
    {
        cout << "Could not open the file." << endl;
        cout << "Program terminating." << endl;
        exit(EXIT_FAILURE);
    }
    inFile >> m;
    inFile.get();
    for (int i = 0; i < m; i++)
    {
        //cout << "Please input the name(Anonymous donator can replace with none):";
        getline(inFile, donator[i].name); //由于读取都是行读取的,所以这一类函数可行
        //cout << "Please input the donation:";
        inFile >> donator[i].money;
        inFile.get();
    }
    for (int j = 0; j < m; j++)
    {
        if (donator[j].money >= 10000)
        {
            grand_patrons[k] = donator[j];
            k++;
        }
    }
    if (k)
    {
        cout << "Patrons" << endl;
        for (int z = 0; z < k + 1; z++)
        {
            cout << grand_patrons[z].name << '\t' << grand_patrons[z].money << endl;
        }
    }
    else
        cout << "There is no one donated over 10000." << endl;
    delete[] donator;
    delete[] grand_patrons;
    inFile.close();
    system("pause");
    return 0;
}

要改动的主要是输入格式,就是定义文件输入流对象,然后可像使用cin对象那样使用,在文件关联后需要关闭,我用的是vscode,这里查找文件路径最好明示,放在你项目所在文档最方便。最后,问题还是那个问题,会有奇怪的输出在patrons那里,我觉得问题应该是两个动态数组互相赋值带来的影响。
在这里插入图片描述
解决:和上面一样k+1改成k即可

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值