利用c++类和对象建立学生成绩管理系统
利用重载函数实现添加、浏览、查询、修改、删除、存储、读取等功能。
class student
{
private:
char name[20];
char sex[6];
int number;
int score[7];
int total;
int average;
int last;
public:
student();
void add();
void output();
void find();
void modify();
void deleted();
void save();
void load();
student & operator = (student & T);
friend istream & operator >> (istream & scin, student &s1);
friend ostream & operator << (ostream & scout, student &s2);
};
student stu[200];
int n = 0;
void student::add()
{
char flag = 'Y';
while (toupper(flag) == 'Y')
{
cin >> stu[n];
n++;
cout << "请问要继续录入吗?(请输入y或n)";
cin >> flag;
if (toupper(flag) != 'Y')
{
if (toupper(flag) == 'N')
{
break;
}
else
{
cout << "输入错误,请重新输入:";
cin >> flag;
}
}
}
}
void student::output()
{
if (n != 0)
{
cout << "****************************************************************************\n";
cout << " 学 号 姓 名 性 别 语 文 英 语 数 学 物 理 化 学 生 物 平时成绩 总 评 \n";
cout << "****************************************************************************\n";
for (int t = 0; t < n; t++)
{
cout << stu[t];
}
}
getchar();
}
void student::find()
{
int f3 = 0;
char n1[20];
student temp;
cout << '\n';
cout << "请输入您要查找的学生的姓名:";
cin >> n1;
for (int j = 0; j < n; j++)
{
if (strcmp(stu[j].name, n1) == 0)
{
temp = stu[j];
f3 = 1;
}
}
if (f3 == 0)
cout << "对不起,没有你要查找的学生。" << endl;
else
{
cout << "****************************************************************************\n";
cout << " 学 号 姓 名 性 别 语 文 英 语 数 学 物 理 化 学 生 物 平时成绩 总 评 \n";
cout << "****************************************************************************\n";
cout << setw(5) << temp.number << setw(8) << temp.name
<< setw(6) << temp.sex << setw(6) << temp.score[0]
<< setw(6) << temp.score[1] << setw(6) << temp.score[2]
<< setw(6) << temp.score[3] << setw(6) << temp.score[4]
<< setw(6) << temp.score[5] << setw(6) << temp.score[6]
<< setw(7) << temp.last << endl;
}
getchar();
}
void student::modify()
{
int f4 = 0;
int t;
char n2[50];
cout << '\n';
cout << "请输入您要修改的学生姓名";
cin >> n2;
for (int j = 0; j < n; j++)
{
if (strcmp(stu[j].name, n2) == 0)
{
t = j;
f4 = 1;
}
}
if (f4 = 0)
cout << "对不起,没有您要修改的学生" << endl;
else{
cout << "请输入修改后的学生学号:";
cin >> stu[t].number;
cout << "请输入修改后的学生姓名:";
cin >> stu[t].name;
cout << "请输入修改后的学生性别:";
cin >> stu[t].sex;
cout << "请输入修改后的语文成绩:";
cin >> stu[t].score[0];
cout << "请输入修改后的英语成绩:";
cin >> stu[t].score[1];
cout << "请输入修改后的数学成绩:";
cin >> stu[t].score[2];
cout << "请输入修改后的物理成绩:";
cin >> stu[t].score[3];
cout << "请输入修改后的化学成绩:";
cin >> stu[t].score[4];
cout << "请输入修改后的生物成绩:";
cin >> stu[t].score[5];
cout << "请输入修改后的平时成绩:";
cin >> stu[t].score[6];
stu[t].total = stu[t].score[0] + stu[t].score[1]
+ stu[t].score[2] + stu[t].score[3]
+ stu[t].score[4] + stu[t].score[5];
stu[t].average = stu[t].total / 6;
stu[t].last = (int)(stu[t].score[6] * 0.3 + stu[t].average*0.7);
cout << "修改成功!" << endl;
}
getchar();
}
void student::deleted()
{
char n3[20];
bool f5 = 0;
char c;
do{
cout << '\n';
cout << "请输入您要删除的学生姓名:";
cin >> n3;
for (int i = 0; i < n; i++)
{
if (strcmp(n3, stu[i].name) == 0)
{
f5 = 1;
n--;
do{
stu[i] = stu[i + 1];
i++;
} while (i <= n);
}
}
if (f5 == 0)
cout << "您要删除的学生不存在";
cout << "是否要继续删除(输入y或者n)" << endl;
cin >> c;
if (toupper(c) != 'Y'&&toupper(c) != 'N')
{
cout << "输入错误!您只能输入Y或N" << endl;
cin >> c;
}
} while (toupper(c) == 'Y');
getchar();
}
void student::save()
{
char filename1[20];
cout << "请输入文件名:";
cin >> filename1;
ofstream fout(filename1, ios::app);
if (!fout)
cout << "文件不能打开!" << endl;
else
{
for (int i = 0; i < n; i++)
fout << ' ' << stu[i].number << ' ' << stu[i].name
<< ' ' << stu[i].sex << ' ' << stu[i].score[0]
<< ' ' << stu[i].score[1] << ' ' << stu[i].score[2]
<< ' ' << stu[i].score[3] << ' ' << stu[i].score[4]
<< ' ' << stu[i].score[5] << ' ' << stu[i].score[6]
<< ' ' << stu[i].total << ' ' << stu[i].average
<< ' ' << stu[i].last;
cout << "保存成功" << endl;
}
fout.close();
getchar();
}
void student::load()
{
char filename2[20];
cout << "请输入您要读取的文件名:";
cin >> filename2;
ifstream fin(filename2, ios::in);
if (!fin)
cout << "文件打不开!" << endl;
else
{
for (int i = 0;; i++, n = i - 1)
{
if (fin.eof())
break;
fin >> stu[i].number >> stu[i].name
>> stu[i].sex >> stu[i].score[0] >> stu[i].score[1]
>> stu[i].score[2] >> stu[i].score[3] >> stu[i].score[4]
>> stu[i].score[5] >> stu[i].score[6]
>> stu[i].total >> stu[i].average >> stu[i].last;
}
cout << "文件已读取成功!" << endl;
}
fin.close();
getchar();
}
student & student::operator=(student & T)//重载=运算定义
{
strcpy(name, T.name);
strcpy(sex, T.sex);
number = T.number;
for (int i = 0; i < 7; i++)
score[i] = T.score[i];
total = T.total;
average = T.average;
last = T.last;
return (*this);
}
ostream & operator<<(ostream & scout, student &s2)//重载流插入
{
cout << setw(5) << s2.number << setw(8) << s2.name << setw(6) << s2.sex
<< setw(6) << s2.score[0] << setw(6) << s2.score[1]
<< setw(6) << s2.score[2]<< setw(6) << s2.score[3]
<< setw(6) << s2.score[4] << setw(6) << s2.score[5]
<< setw(7) << s2.score[6] << setw(8) << s2.last << '\n';
return scout;
}
istream & operator>>(istream & scin, student &s1)
{
cout << '\n';
cout << "学号:";
scin >> s1.number;
cout << "姓名:";
scin >> s1.name;
cout << "性别:";
scin >> s1.sex;
cout << "语文:";
scin >> s1.score[0];
cout << "英语:";
scin >> s1.score[1];
cout << "数学:";
scin >> s1.score[2];
cout << "物理:";
scin >> s1.score[3];
cout << "化学:";
scin >> s1.score[4];
cout << "生物:";
scin >> s1.score[5];
cout << "平时成绩:";
scin >> s1.score[6];
s1.total = s1.score[0] + s1.score[1]
+ s1.score[2] + s1.score[3]
+ s1.score[4] + s1.score[5];
s1.average = s1.total / 6;
s1.last = (int)(s1.score[6] * 0.3 + s1.average*0.7);
return scin;
}
student::student()//构造函数
{
strcpy(name, " ");
strcpy(sex, " ");
number = 0;
for (int i = 0; i < 7; i++)
score[i] = 0;
total = 0;
average = 0;
last = 0;
}
void menu()
{
cout << "\n\n\n\t\t\t【学生成绩管理系统主菜单】" << endl;
cout << "\n\n\t\t1.学生成绩录入";
cout << "\t\t\t2.浏览学生成绩" << endl;
cout << endl;
cout << "\t\t3.查询学生成绩";
cout << "\t\t\t4.修改学生成绩" << endl;
cout << endl;
cout << "\t\t5.删除学生成绩";
cout << "\t\t\t6.数据存入文件" << endl;
cout << endl;
cout << "\t\t7.文件读出数据";
cout << "\t\t\t0.退出系统" << endl;
cout << "\n\n请选择功能(0-7)并按回车键确认:";
}
void main()
{
cout << "\t\t\t 欢迎访问学生成绩管理系统" << endl;
int choose;
student s;
do
{
menu();
cin >> choose;
switch (choose)
{
case 1:s.add();
break;
case 2:s.output();
break;
case 3:s.find();
break;
case 4:s.modify();
break;
case 5:s.deleted();
break;
case 6:s.save();
break;
case 7:s.load();
break;
}
} while (choose != 0);
}