类的定义:
class DonativeInfo{
friend ifstream& operator >>(ifstream& in, DonativeInfo& di){
getline(in, di.m_name);
string tStr;
getline(in, tStr);
di.m_num = atoi(tStr.c_str());
return in;
}
public:
int
GetNum()const
{return m_num;}
string GetName()const {return m_name;}
private:
string
m_name;
int
m_num;
};
所需头文件:
#include <iostream>
#include <fstream>
#include <string>
命名空间:
std
主程序:
int main(){
ifstream fin("in.txt");
int totalNum;
string tStr;
getline(fin, tStr);
totalNum = atoi(tStr.c_str());
DonativeInfo *pDI = new DonativeInfo[totalNum];
for(int idx=0; idx<totalNum; ++idx)
fin >> pDI[idx];
cout << endl << "捐款数大于等于10000的捐款人如下:" << endl << endl;
cout << "姓名" << "\t\t\t" << "数目" << endl;
for(idx=0; idx<totalNum; ++idx){
if(pDI[idx].GetNum() >= 10000)
cout << pDI[idx].GetName() << "\t\t" << pDI[idx].GetNum() << endl;
}
cout << endl;
delete [] pDI;
return 0;
}