用来储存姓名和字符数组(或string对象)和用来存储款项的double成员。读取所有的数据后,程序将显示所有捐款超过10000的捐献者的姓名及其捐款数额。该列表前应包含一个标题,指出下面的捐款者是重要捐款人(Grand Patrons)。然后,程序将列出其他的捐款者,该列表要以Patrons开头。如果某种类型没有捐献者,则程序将打印单词“none”。该程序只显示这两种类别,而不进行排序。
#include <iostream>
using namespace std;
struct donation {
string name;
double money;
};
int main() {
cout << "enter the number of the donors: ";
int num, i;
int grand = 0;
cin >> num;
donation* pl = new donation[num];
for (i = 0; i < num; i++) {
cout << "name:";
cin >> pl[i].name;
cout << "money:";
cin >> pl[i].money;
if (pl[i].money > 10000)
grand++;
}
if (grand > 0 && grand < num)
{
cout << "Grand Patrons:\n";
for (i = 0; i < num; i++)
{
if (pl[i].money > 10000)
{
cout << pl[i].name << "\t" << pl[i].money << endl;
}
}
cout << "Patrons:\n";
for (i = 0; i < num; i++)
{
if (pl[i].money <= 10000)
{
cout << pl[i].name << "\t" << pl[i].money << endl;
}
}
}
if (grand == 0)
{
cout << "Grand Patrons:\n" << "none";
cout << "Patrons:\n";
for (i = 0; i < num; i++)
{
if (pl[i].money <= 10000)
{
cout << pl[i].name << "\t" << pl[i].money << endl;
}
}
}
if (grand == num)
{
cout << "Grand Patrons:\n";
for (i = 0; i < num; i++)
{
if (pl[i].money > 10000)
{
cout << pl[i].name << "\t" << pl[i].money << endl;
}
}
cout << "Patrons:\n" << "none";
}
delete[]pl;
return 0;
}