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

2024-11-23 04:19:08
推荐回答(4个)
回答1:

#include
#include
#include
#include
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 m_list;
};

void main()
{
Mgr mgr;
bool bRun = true;
while (bRun)
{
system("cls");
cout<<"1.添加"< cout<<"2.保存"< cout<<"3.读出"< cout<<"4.退出"< 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<<"该学生平均分为:"< system("pause");
break;
case '2':
system("cls");
if (mgr.SaveData())
cout<<"保存成功!"< else
cout<<"保存失败!"< system("pause");
break;
case '3':
system("cls");
if (!mgr.LoadAndPrintData())
{
cout<<"读出失败!"< }
system("pause");
break;
case '4':
bRun = false;
break;
}
}
}

Mgr::~Mgr()
{
list::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::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平均分"< STUDENT student;
while(fread(&student, sizeof(STUDENT), 1, pf))
{
cout< }

fclose(pf);
return true;
}

回答2:

qidai

回答3:

#include
#include
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<<"第"< sd[i].input();
sd[i].average();
}
FILE *fp;
fp = fopen("STUD.DAT","w");
cout<<"学号 "<<"姓名 "<<"平均分"< for(int i=0; i<5; i++)
{
sd[i].print(fp);
sd[i].show();
}
fclose(fp);

return 0;
}

回答4:

希望你能明白 :)
#include
#include
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;
}