利用c++类和对象建立学生成绩管理系统
利用c++类和对象建立学生成绩管理
利用重载函数实现添加、浏览、查询、修改、删除、存储、读取等功能。
形式一:
using namespace std;
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);
}
形式二:
class student //定义学生类
{
private:
char name[20];
char sex[6];
int number;
int score[7];
int total;
int average;
int last;
public:
student(); //学生数据初始化以及保存学生数据
void input(); //添加学生信息
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::input() //添加学生信息
{
char t = ‘y’;
while (t == ‘y’)
{
cin >> stu[n];
n++;
cout << “继续录入?(输入y/n)” << endl;
cin >> t;
if (t != ‘y’)
{
if (t == ‘n’)
{
break;
}
else
{
cout << “输入错误,请重新输入:”;
cin >> t;
}
}
}
}
void student::output() //输出学生信息
{
if (n != 0)
{
cout << “***************************************************************” << endl;
cout << ” 学号 姓名 性别 语文 英语 数学 物理 化学 生物 平时成绩 总评” << endl;
int t;
for (t = 0; t
上一篇: 越想越怕,好吓人的赶脚!
下一篇: 恐怖故事里的遭遇