求高手帮忙,关于一个C++程序

要求从键盘输入5个学生的数据(包括学号,姓名,年龄,三门功课的分数),然后求出没个人的平均分数,把学号,姓名和平均分数输入到磁盘文件STUD.DAT中,最后从该文件中读出... 要求从键盘输入5个学生的数据(包括学号,姓名,年龄,三门功课的分数),然后求出没个人的平均分数,把学号,姓名和平均分数输入到磁盘文件STUD.DAT中,最后从该文件中读出这些数据,并在屏幕上显示出来。

中间我只知道要下面一小段程序
class three_d{
private:
int a,b,c;
}
展开
 我来答
榊桗桗
2007-11-13 · TA获得超过227个赞
知道小有建树答主
回答量:367
采纳率:0%
帮助的人:287万
展开全部
#include <conio.h>
#include <list>
#include <assert.h>
#include <iostream.h>
using namespace std;

#define FILEPATH "./STUD.DAT"
#define NAME_MAXLEN 9

class Mgr
{
typedef struct tagStudent
{
int id;
char name[NAME_MAXLEN];
int score1;
int score2;
int score3;
}STUDENT, *PSTUDENT;
public:
~Mgr();
int Add(int id, char* pName, int score1, int score2, int score3);
bool SaveData();
bool LoadAndPrintData();
protected:
private:
list<PSTUDENT> m_list;
};

void main()
{
Mgr mgr;
bool bRun = true;
while (bRun)
{
system("cls");
cout<<"1.添加"<<endl;
cout<<"2.保存"<<endl;
cout<<"3.读出"<<endl;
cout<<"4.退出"<<endl;
char ch = getch();

switch(ch)
{
case '1':
int id;
char name[NAME_MAXLEN];
int score1, score2, score3;

system("cls");
cout<<"学号:";
cin>>id;
cin.clear();
cout<<"姓名:";
cin>>name;
cin.clear();
cout<<"成绩1:";
cin>>score1;
cin.clear();
cout<<"成绩2:";
cin>>score2;
cin.clear();
cout<<"成绩3:";
cin>>score3;
cin.clear();
cout<<"该学生平均分为:"<<mgr.Add(id, name, score1, score2, score3)<<endl;
system("pause");
break;
case '2':
system("cls");
if (mgr.SaveData())
cout<<"保存成功!"<<endl;
else
cout<<"保存失败!"<<endl;
system("pause");
break;
case '3':
system("cls");
if (!mgr.LoadAndPrintData())
{
cout<<"读出失败!"<<endl;
}
system("pause");
break;
case '4':
bRun = false;
break;
}
}
}

Mgr::~Mgr()
{
list<PSTUDENT>::iterator it;
for (it = m_list.begin(); it != m_list.end(); ++it)
{
delete *it;
}
}

int Mgr::Add(int id, char* pName, int score1, int score2, int score3)
{
STUDENT* pStudent = new STUDENT;
pStudent->id = id;
pStudent->score1 = score1;
pStudent->score2 = score2;
pStudent->score3 = score3;
memcpy(pStudent->name, pName, NAME_MAXLEN);
pStudent->name[NAME_MAXLEN - 1] = 0;

m_list.push_back(pStudent);

return (score1 + score2 + score3) / 3;
}

bool Mgr::SaveData()
{
if (m_list.size() == 0)
return false;

FILE *pf = fopen(FILEPATH, "w+");
if (pf == NULL) return false;

list<PSTUDENT>::iterator it;
for (it = m_list.begin(); it != m_list.end(); ++it)
{
assert(*it);
fwrite(*it, sizeof(STUDENT), 1, pf);
}
fclose(pf);
return true;
}

bool Mgr::LoadAndPrintData()
{
FILE *pf = fopen(FILEPATH, "r+");
if (pf == NULL || feof(pf)) return false;

cout<<"学号\t姓名\t成绩1\t成绩2\t成绩3\t平均分"<<endl;
STUDENT student;
while(fread(&student, sizeof(STUDENT), 1, pf))
{
cout<<student.id<<"\t"<<student.name<<"\t"<<student.score1<<"\t"<<student.score2<<"\t"<<student.score3<<"\t"<<(student.score1 + student.score2 + student.score3) / 3<<endl;
}

fclose(pf);
return true;
}
百度网友5353ef490
2007-11-13 · 超过18用户采纳过TA的回答
知道答主
回答量:69
采纳率:0%
帮助的人:58.3万
展开全部
qidai
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友d3e418b2c
2007-11-13 · TA获得超过2195个赞
知道大有可为答主
回答量:1361
采纳率:0%
帮助的人:1699万
展开全部
#include <iostream>
#include <stdio.h>
using namespace std;

class three_d
{
private:
int a, b, c;
protected:
float avg;
void input()
{
cout<<"输入3门课成绩:";
cin>>a;
cin>>b;
cin>>c;
}
public:
void average()
{
avg = (a+b+c)/3.0;
}
};
class student : public three_d
{
private:
int xuehao;
char xingming[20];
int nianling;
public:
void input()
{
cout<<"学号:";
cin>>xuehao;
cout<<"姓名:";
cin>>xingming;
cout<<"年龄:";
cin>>nianling;
three_d::input();
}
void show()
{
printf("%-4d%6.6s%8.2f\n",xuehao, xingming, avg);
}
void print(FILE *fp)
{
fprintf(fp, "%-4d%6.6s%8.2f\n",xuehao, xingming, avg);
}
};

int main(int argc, char * argv[])
{
student sd[5];
for(int i=0; i<5; i++)
{
cout<<"第"<<i+1<<"个学生信息:"<<endl;
sd[i].input();
sd[i].average();
}
FILE *fp;
fp = fopen("STUD.DAT","w");
cout<<"学号 "<<"姓名 "<<"平均分"<<endl;
for(int i=0; i<5; i++)
{
sd[i].print(fp);
sd[i].show();
}
fclose(fp);

return 0;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
jinglebaby0807
2007-11-13 · TA获得超过193个赞
知道小有建树答主
回答量:251
采纳率:0%
帮助的人:181万
展开全部
希望你能明白 :)
#include <iostream>
#include <string>
using namespace std;

struct student
{
string ID;
string name;
int age;
int course_a;
int course_b;
int course_c;
float aver;
};

void input (student &s)
{
cin >> s.ID;
cin >> s.name;
cin >> s.age;
cin >> s.course_a >> s.course_b >> s.course_c;
}

void average (student &s)
{
s.aver = (s.course_a + s.course_b + s.course_c) / 3.00;
}

int main()
{
student buf[5];
int i;
student s;

for (i = 0; i < 5; i++)
input (buf[i]);

for (i = 0; i < 5; i++)
average (buf[i]);

FILE *fp = fopen ("STUD.DAT", "w+");

for (i = 0; i < 5; i++)
{
fwrite (&buf[i].ID, sizeof (buf[i].ID), 1, fp);
fwrite (&buf[i].name, sizeof (buf[i].name), 1, fp);
fwrite (&buf[i].aver, sizeof (buf[i].aver), 1, fp);
}

fseek (fp, 0, SEEK_SET);
for (i = 0; i < 5; i++)
{
fread (&s.ID, sizeof (s.ID), 1, fp);
fread (&s.name, sizeof (s.name), 1, fp);
fread (&s.aver, sizeof (s.aver), 1, fp);
cout << s.ID << " " << s.name << " " << s.aver << endl;
}

return 0;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式