首页 > 试题广场 >

编写一个程序,记录捐助给“维护合法权利团体”的资金。该程序

[问答题]
编写一个程序,记录捐助给“维护合法权利团体”的资金。该程序 要求用户输入捐献者数目,然后要求用户输入每一个捐献者的姓名和款项。这些信息被储存在一个动态分配的结构数组中。每个结构有两个成员:用来储存姓名的字符数组(或string对象)和用来存储款项的double成员。读取所有的数据后,程序将显示所有捐款超过10000的捐款者的姓名及其捐款数额。该列表前应包含一个标题,指出下面的捐款者是重要捐款人(Grand Patrons)。然后,程序将列出其他的捐款者,该列表要以Patrons开头。如果某种类别没有捐款者,则程序将打印单词“none”。该程序只显示这两种类别,而不进行排序。并从上述文件中读取所需的信息。该文件的第一项应为捐款人数,余下的内容应为成对的行。在每一对中,第一行为捐款人姓名,第二行为捐款数额,即该文件类似于下面:
4
Sam Stone
2000
Freida Flass
100500
Tammy Tubbs
5000
Rich Raptor
55000


#include<iostream>
#include<cstring>
using namespace std;
struct mem
{
    string name;
	double money;
};
int main()
{
	
	int n,count;
	cin >> n;
	mem *person=new mem[n];//set a uncentainty array

	for (int i = 0; i < n; i++)
	{
        if (i == 0)
        {
            cout << "Please enter name of first person." << endl;
            cin >> person[i].name;
            cout << "Please enter contribution of first person." << endl;
            cin >> person[i].money;
        }
        else if(i==1)
        {
            cout << "Please enter name of second person." << endl;
            cin >> person[i].name;
            cout << "Please enter contribution of second person." << endl;
            cin >> person[i].money;
        }
        else if (i == 2)
        {
            cout << "Please enter name of third person." << endl;
            cin >> person[i].name;
            cout << "Please enter contribution of third person." << endl;
            cin >> person[i].money;
        }
        else
        {
            cout << "Please enter name of  " << i+1 << " th person." << endl;
            cin >> person[i].name;
            cout << "Please enter money of  " << i+1 << " th person." << endl;
            cin >> person[i].money;
        }
        cout << endl;
	}
    int num1 = 0,num2=0;
    cout << "\nGrand Patrons\n" << endl;
    for (int j = 0; j < n; j++)
    {
        if (person[j].money > 10000)
        {
            cout << person[j].name << "\t" << person[j].money << endl;
            num1++;
        }
    }
    if (num1 == 0)
        cout << "None." << endl;

    cout << "\npatrons\n" << endl;
    for (int k = 0; k < n; k++)
    {
        if (person[k].money <= 10000)
        {
            cout << person[k].name << "\t" << person[k].money << endl;
            num2++;
        }
    }
    if (num2 == 0)
        cout << "None." << endl;
	return 0;
}

发表于 2022-03-07 10:56:14 回复(0)
#include<iostream>
using namespace std;

struct pat
{
	string name;
	double contribution;
};

int main()
{
	int i;
	cout << "请输入捐助人数: ";
	cin >> i;
	pat* patrons = new pat[i];
	for (int j = 0; j < i; j++)
	{
		cout << "第 " << j + 1 << " 个捐助者姓名: ";
		cin >> patrons[j].name;
		cout << "第 " << j + 1 << " 个捐助者金额: ";
		cin >> patrons[j].contribution;
		cout << endl;
		
	}
	int count1 = 0;
	int count2 = 0;
	cout << "Grand patrons : " << endl;
	for (int j = 0; j < i; j++)
	{
		if (patrons[j].contribution > 10000)
		{
			count1++;
			cout << patrons[j].name << "    "
				<< patrons[j].contribution << endl;
		}
	}
	if (count1 == 0)
	{
		cout << "none\n";
	}
	cout << "\npatrons : " << endl;
	for (int j = 0; j < i; j++)
	{
		if (patrons[j].contribution <= 10000)
		{
			count2++;
			cout << patrons[j].name << "    "
				<< patrons[j].contribution << endl;
		}
	}
	if (count2 == 0)
	{
		cout << "none\n";
	}
	
	system("pause");
	return 0;
}

发表于 2020-08-17 20:43:55 回复(0)