#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
using namespace std;
struct member
{
string name;
double donation;
};
int main()
{
int num;
int count1 = 0; // grand donator
int count2 = 0; // nomal donator
string filename;
ifstream file;
cout << "Enter the filename: ";
getline(cin, filename);
file.open(filename);
if (!file.is_open())
{
cout << "This file: " << filename << " can't open!" << endl;
cout << "Program terminating.\n";
exit(EXIT_FAILURE);
}
// 获取第一行数据
file >> num;
cout << "\nThere are " << num << " donators." << endl;
file.get(); // 读取缓冲区的换行符
// 将数据存储到结构体数组(指针)
member * pd = new member [num];
for (int i = 0; i < num; i++)
{
getline(file, pd[i].name);
file >> pd[i].donation;
file.get(); // 读取缓冲区的换行符
}
// seprate discussions
cout << "\nThe grand donator: " << endl;
for (int i = 0; i < num; i++)
{
if (pd[i].donation >= 10000)
{
cout << pd[i].name << ": " << pd[i].donation << endl;
++count1;
}
}
if (count1 == 0)
cout << "none!" << endl;
cout << "\nThe normal donator: " << endl;
for (int i = 0; i < num; i++)
{
if (pd[i].donation < 10000)
{
cout << pd[i].name << ": " << pd[i].donation << endl;
++count2;
}
}
if (count2 == 0)
cout << "none!" << endl;
delete [] pd;
return 0;
}
6.11.9 完成编程练习6,但从文件中读取所需的信息。该文件的第一项应为捐款人数,余下的内容应为成对的行。在每一对中,第一行为捐款人姓名,第二行为捐款数额。
这篇文章描述了一个C++程序,它从一个文本文件中读取捐赠者信息,根据捐赠金额将其分为大额捐赠者(>10000)和小额捐赠者(<10000),并输出相应的名单。
摘要由CSDN通过智能技术生成