当前位置:C++技术网 > 精选软件 > C++ Primer Plus 6th 6.11 编程练习题 第6题 记录捐款姓名和捐款数额

C++ Primer Plus 6th 6.11 编程练习题 第6题 记录捐款姓名和捐款数额

更新时间:2019-05-11 13:27:22浏览次数:1+次

题目:
6.编写一个程序,记录捐助给“维护合法权利团体”的资金。该程序要求用户输入捐献者数目,然后要求用户输入每一个捐献者的姓名和款项。这些信息被储存在一个动态分配的结构数组中。每个结构有两个成员:用来储存姓名的字符数组(或string对象)和用来存储款项的double成员。读取所有的数据后,程序将显示所有捐款超过10000的捐款者的姓名及其捐款数额。该列表前应包含一个标题,指出下面的捐款者是重要捐款人(Grand  Patrons)。然后,程序将列出其他的捐款者,该列表要以Patrons开头。如果某种类别没有捐款者,则程序将打印单词“none”。该程序只显示这两种类别,而不进行排序。


答案:  书上无答案。

C++技术网辅导详解解答:
    参考代码:

#include <iostream>
using namespace std;
struct info {
    char name[50];
    double money;
};
int main()
{
    cout << "请输入捐献者的数目:";
    int cnt;
    cin >> cnt;
    info * p = new info[cnt];
    for (int i = 0; i < cnt; i++)
    {
        cout << "请输入捐献者的名字:";
        cin >> p[i].name;
        cout << "请输入捐款的金额:";
        cin >> p[i].money;
    }

    cout << "\n\n重要捐款人列表\n名字\t金额\t\n";
    int cnt_big = 0;
    for (int i = 0; i < cnt; i++)
    {
        if (p[i].money > 10000) 
        {
            cout << p[i].name << "\t" << p[i].money << endl;
            cnt_big++;
        }
    }
    if (cnt_big == 0)
    {
        cout << "none\tnone" << endl;
    }

    cout << "\n\n其他捐款人列表\n名字\t金额\t\n";
    int cnt_little = 0;
    for (int i = 0; i < cnt; i++)
    {
        if (p[i].money <= 10000)
        {
            cout << p[i].name << "\t" << p[i].money << endl;
            cnt_little++;
        }
    }
    if (cnt_little == 0)
    {
        cout << "none\tnone" << endl;
    }
    
    delete[] p;
    return 0;
}