64,642
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
class CStudent
{
public:
string m_name;
double m_score[3];
double m_aver;
CStudent();
bool Setdata();
void Output();
void Aver();
};
CStudent::CStudent()
{
m_aver = 0;
//memset(m_name, 0, 10);
}
void CStudent::Aver()
{
m_aver = (m_score[0] + m_score[1] + m_score[2]) / 3;
}
bool CStudent::Setdata()
{
return bool(cin >> m_name >> m_score[0] >> m_score[1] >> m_score[2]);
//Aver();
//return 0;
}
void CStudent::Output()
{
cout << m_name << m_score[0] << m_score[1] << m_score[2] << m_aver << endl;
}
class CStuFile
{
CStudent st[100];
CStudent stu;
ifstream infile;
ofstream outfile;
public:
int AddTo();
int List();
void Sort();
};
int CStuFile::AddTo()
{
outfile.open("students.dat", ios::out | ios::binary);
if (!outfile)
{
cout << "open error!" << endl;
return 0;
}
while (stu.Setdata())//cin >> stu.m_name >> stu.m_score[0] >> stu.m_score[1] >> stu.m_score[2]
{
outfile.write((char *)& stu, sizeof(stu));
}
outfile.close();
return 1;
}
int CStuFile::List()
{
int p;
int i = 0;
//CStudent s[10];
infile.open("students.dat", ios::in | ios::binary);
if (!infile)
{
cout << "open error!" << endl;
return 0;
}
while (infile.read((char *) &st[i], sizeof(st[i])))
{
//st[i] = stu;
cout << st[i].m_name << st[i].m_aver << endl;
++i;
}
int j = i;
infile.close();
outfile.open("students scroe.dat",ios::out|ios::binary);
if(!outfile)
{
cout << "open error!" <<endl;
infile.close();
return 0;
}
for(i = 0; i< j; ++i)
{
outfile.write( (char * ) & st[i], sizeof(st[i]) );
}
outfile.close();
return 1;
}
int main()
{
CStuFile sf;
sf.AddTo();
sf.List();
return 0;
}