1、问题描述
学生信息包括:学号、姓名、性别、年龄、班级等信息。
小学生除了包括学生所有信息外,还包括英语、数学和语文成绩。
中学生除了包括小学生所有信息外,还包括地理、历史成绩、家庭住址等信息。
大学生除了包括学生所有信息外,还包括专业、家庭地址、联系方式等信息。
2、功能要求
(1)添加功能:程序能够添加不同学生的记录,提供选择界面供用户选择所要添加的类别,要求学号要唯一,如果添加了重复学号的记录时,则提示数据添加重复并取消添加。
(2)查询功能:可根据学号、姓名等信息对已添加的学生记录进行查询,如果未找到,给出相应的提示信息,如果找到,则显示相应的记录信息。
(3)显示功能:可显示当前系统中所有学生的记录,每条记录占据一行。
(4)编辑功能:可根据查询结果对相应的记录进行修改,修改时注意学号的唯一性。
(5)删除功能:主要实现对已添加的学生记录进行删除。如果当前系统中没有相应的记录,则提示“记录为空!”并返回操作。
(6)统计功能:能根据多种参数进行统计。能统计学生人数、按性别统计、按年龄统计等。
(7)保存功能:可将当前系统中各类记录存入文件中,存入方式任意。
(8)读取功能:可将保存在文件中的信息读入到当前系统中,供用户进行使用。
源码如下:
#include <iostream>
#include <fstream>
#include <cstring>
#include <windows.h>
#include <time.h>
using namespace std;
void Creat_Student();
void Delete_Student();
void Print_Student();
void Statistics_Student();
void Search_Student();
void Find_Student();
void Change_Student();
void Out_File();
void In_File();
class Student//基类
{
public:
friend void Out_File();
friend void In_File();
int show_sex()
{
if(this->sex=="男") return 1;
else if(this->sex=="女") return 2;
}
float show_age()
{
return this->age;
}
virtual void showshow()
{
cout<<"姓名:"<<name<<endl<<"学号:"<<id<<endl;
cout<<"性别:"<<sex<<endl<<"班级:"<<classes<<endl;
cout<<"年龄:"<<age<<endl;
}
void set_name(string name)
{
this->name=name;
}
void set_id(string id)
{
this->id=id;
}
void set_sex(string sex)
{
this->sex=sex;
}
void set_classes(string classes)
{
this->classes=classes;
}
void set_age(float age)
{
this->age=age;
}
string id;
string name;
protected:
string sex;
string classes;
float age;
};
class s_student: public Student// 小学生
{
public:
friend void Out_File();
friend void In_File();
void showshow()
{
cout<<"姓名:"<<name<<endl<<"学号:"<<id<<endl;
cout<<"性别:"<<sex<<endl<<"班级:"<<classes<<endl;
cout<<"年龄:"<<age<<endl;
cout<<"英语成绩:"<<English<<endl<<"数学成绩:"<<math<<endl<<"语文成绩:"<<Chinese<<endl;
}
void setEnglish(float English)
{
this->English=English;
}
void setmath(float math)
{
this->math=math;
}
void setChinese(float Chinese)
{
this->Chinese=Chinese;
}
protected:
float English;
float math;
float Chinese;
};
class m_student: public s_student//中学生
{
public:
friend void Out_File();
friend void In_File();
void showshow()
{
cout<<"姓名:"<<name<<endl<<"学号:"<<id<<endl;
cout<<"性别:"<<sex<<endl<<"班级:"<<classes<<endl;
cout<<"年龄:"<<age<<endl;
cout<<"地理成绩:"<<geog<<endl<<"历史成绩:"<<history<<endl<<"地址:"<<addr<<endl;
}
void setgeog(float geog)
{
this->geog=geog;
}
void sethistory(float history)
{
this->history=history;
}
void setaddr(string addr)
{
this->addr=addr;
}
protected:
float geog;//地理成绩
float history;
string addr;
};
class l_student : public Student //大学生
{
public:
friend void Out_File();
friend void In_File();
void showshow()
{
cout<<"姓名:"<<name<<endl<<"学号:"<<id<<endl;
cout<<"性别:"<<sex<<endl<<"班级:"<<classes<<endl;
cout<<"年龄:"<<age<<endl;
cout<<"专业:"<<major<<endl<<"地址:"<<addr<<endl<<"电话:"<<tel<<endl;
}
void setmajor(string major)
{
this->major=major;
}
void setaddr(string addr)
{
this->addr=addr;
}
void settel(string tel)
{
this->tel=tel;
}
protected:
string major;
string addr;
string tel;
};
s_student stu1[100];
m_student stu2[100];
l_student stu3[100];
int main()
{
while(1)
{
system("color 5E");
system("date/t");
system("time/t");
cout<<"*===============================================================================================*"<<endl;
cout<<"**&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&欢迎使用学生管理系统&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&**"<<endl;
cout<<"** 1.新增学生信息 **"<<endl;
cout<<"** 2.删除学生信息 **"<<endl;
cout<<"** 3.导入学生信息(已经保存于文件的信息) **"<<endl;
cout<<"** 4.学生姓名检索(按姓名) **"<<endl;
cout<<"** 5.学生信息统计(性别/年龄) **"<<endl;
cout<<"** 6.学生信息查找(按学号) **"<<endl;
cout<<"** 7.学生信息保存 **"<<endl;
cout<<"** 8.显示学生信息 **"<<endl;
cout<<"** 9.修改学生信息 **"<<endl;
cout<<"** 0. 退出 **"<<endl;
cout<<"**&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&**" <<endl;
cout<<"*===============================================================================================*"<<endl;
cout<<"请输入您要选择的服务种类:(‘0’~‘9’)\n";
int num;
cin>>num;
switch(num)
{
case 1 :system("CLS");Creat_Student();system("pause");break;//CLS为清屏函数,pause 为暂停
case 2 :system("CLS");Delete_Student();system("pause");break;
case 3 :system("CLS");In_File();system("pause");break;
case 4 :system("CLS");Search_Student();system("pause");break;
case 5 :system("CLS");Statistics_Student();system("pause");break;
case 6 :system("CLS");Find_Student();system("pause");break;
case 7 :system("CLS");Out_File();system("pause");break;
case 8 :system("CLS");Print_Student();system("pause");break;
case 9 :system("CLS");Change_Student();system("pause");break;
case 0 :system("CLS");cout<<"谢谢使用!";exit(0);system("pause");
default:system("CLS");printf("无效输入!\n\n");system("pause");
}
}
return 0;
}
int mark1=0;
int mark2=0;
int mark3=0;
void Creat_Student()
{
cout<<"请选择要添加的学生类型:1.小学生 2.中学生 3.大学生"<<endl;
float a;
cin>>a;
if(a==1)
{
string name1,id1,sex1,classes1;
float age1,english1,math1,chinese1;
cout<<"请输入要添加的学生数量:"<<endl;
float v;
cin>>v;
for(int i=mark1;i<mark1+v;i++)
{
cout<<"请输入第"<<i+1<<"位学生的姓名:";
cin>>name1;
stu1[i].set_name(name1);
cout<<"请输入第"<<i+1<<"位学生的学号:";
cin>>id1;
bool x=1;
for(int j=0;j<i;j++)
{
if(stu1[j].id==id1)
{
cout<<"学号重复!"<<endl;
x=0;
}
}
if(x==0) break;
stu1[i].set_id(id1);
cout<<"请输入第"<<i+1<<"位学生的性别:";
cin>>sex1;
stu1[i].set_sex(sex1);
cout<<"请输入第"<<i+1<<"位学生的年龄:";
cin>>age1;
stu1[i].set_age(age1);
cout<<"请输入第"<<i+1<<"位学生的班级:";
cin>>classes1;
stu1[i].set_classes(classes1);
cout<<"请输入第"<<i+1<<"位学生的英语成绩:";
cin>>english1;
stu1[i].setEnglish(english1);
cout<<"请输入第"<<i+1<<"位学生的语文成绩:";
cin>>chinese1;
stu1[i].setChinese(chinese1);
cout<<"请输入第"<<i+1<<"位学生的数学成绩:";
cin>>math1;
stu1[i].setmath(math1);
}
mark1+=v;
}
if(a==2)
{
string name1,id1,sex1,classes1,addr1;
float age1,english1,math1,chinese1,geog1,history1;
cout<<"请输入要添加的学生数量:"<<endl;
float v;
cin>>v;
for(int i=mark2;i<v+mark2;i++)
{
cout<<"请输入第"<<i+1<<"位学生的姓名:";
cin>>name1;
stu2[i].set_name(name1);
cout<<"请输入第"<<i+1<<"位学生的学号:";
cin>>id1;
bool x=1;
for(int j=0;j<i;j++)
{
if(stu2[j].id==id1)
{
cout<<"学号重复!"<<endl;
x=0;
}
}
if(x==0) break;
stu2[i].set_id(id1);
cout<<"请输入第"<<i+1<<"位学生的性别:";
cin>>sex1;
stu2[i].set_sex(sex1);
cout<<"请输入第"<<i+1<<"位学生的年龄:";
cin>>age1;
stu2[i].set_age(age1);
cout<<"请输入第"<<i+1<<"位学生的班级:";
cin>>classes1;
stu2[i].set_classes(classes1);
cout<<"请输入第"<<i+1<<"位学生的英语成绩:";
cin>>english1;
stu2[i].setEnglish(english1);
cout<<"请输入第"<<i+1<<"位学生的语文成绩:";
cin>>chinese1;
stu2[i].setChinese(chinese1);
cout<<"请输入第"<<i+1<<"位学生的数学成绩:";
cin>>math1;
stu2[i].setmath(math1);
cout<<"请输入第"<<i+1<<"位学生的地理成绩:";
cin>>geog1;
stu2[i].setgeog(geog1);
cout<<"请输入第"<<i+1<<"位学生的历史成绩:";
cin>>history1;
stu2[i].sethistory(history1);
cout<<"请输入第"<<i+1<<"位学生的地址:";
cin>>addr1;
stu2[i].setaddr(addr1);
}
mark2+=v;
}
if(a==3)
{
string name1,id1,sex1,classes1,major1,addr1,tel1;
float age1;
cout<<"请输入要添加的学生数量:"<<endl;
float v;
cin>>v;
for(int i=mark3;i<v+mark3;i++)
{
cout<<"请输入第"<<i+1<<"位学生的姓名:";
cin>>name1;
stu3[i].set_name(name1);
cout<<"请输入第"<<i+1<<"位学生的学号:";
cin>>id1;
bool x=1;
for(int j=0;j<i;j++)
{
if(stu3[j].id==id1)
{
cout<<"学号重复!"<<endl;
x=0;
}
}
if(x==0) break;
stu3[i].set_id(id1);
cout<<"请输入第"<<i+1<<"位学生的性别:";
cin>>sex1;
stu3[i].set_sex(sex1);
cout<<"请输入第"<<i+1<<"位学生的年龄:";
cin>>age1;
stu3[i].set_age(age1);
cout<<"请输入第"<<i+1<<"位学生的班级:";
cin>>classes1;
stu3[i].set_classes(classes1);
cout<<"请输入第"<<i+1<<"位学生的专业:";
cin>>major1;
stu3[i].setmajor(major1);
cout<<"请输入第"<<i+1<<"位学生的地址:";
cin>>addr1;
stu3[i].setaddr(addr1);
cout<<"请输入第"<<i+1<<"位学生的电话:";
cin>>tel1;
stu3[i].settel(tel1);
}
mark3+=v;
}
}
void Delete_Student()
{
cout<<"请输入要删除的学生类型:1.小学生 2.中学生 3.大学生"<<endl;
int aa;
cin>>aa;
if(aa==1)
{
cout<<"请输入要删除学生的学号:"<<endl;
string num;
cin>>num;
bool x=1;
for(int i=0;i<100;i++)
{
if(stu1[i].id==num)
{
x=0;
stu1[i].id="\0";
cout<<"删除成功!"<<endl;
}
}
if(x==1) cout<<"此学号不存在"<<endl;
}
else if(aa==2)
{
cout<<"请输入要删除学生的学号:"<<endl;
string num;
cin>>num;
bool x=1;
for(int i=0;i<100;i++)
{
if(stu2[i].id==num)
{
stu2[i].id="\0";
x=0;
cout<<"删除成功!"<<endl;
}
// else cout<<"此学号不存在"<<endl;
}
if(x==1) cout<<"此学号不存在"<<endl;
}
else if(aa==3)
{
cout<<"请输入要删除学生的学号:"<<endl;
string num;
cin>>num;
bool x=1;
for(int i=0;i<100;i++)
{
if(stu3[i].id==num)
{
stu3[i].id="\0";
cout<<"删除成功!"<<endl;
x=0;
break;
}
}
if(x==1) cout<<"此学号不存在"<<endl;
}
}
void Print_Student()
{
bool x=1;
cout<<"小学生信息:"<<endl;
for(int i=0;i<100;i++)
{
if(stu1[i].id!="\0") x=0,stu1[i].showshow();
}
if(x==1) cout<<"暂无小学生信息"<<endl;
bool y=1;
cout<<"中学生信息:"<<endl;
for(int i=0;i<100;i++)
{
if(stu2[i].id!="\0") y=0,stu2[i].showshow();
}
if(y==1) cout<<"暂无中学生信息"<<endl;
bool z=1;
cout<<"大学生信息:"<<endl;
for(int i=0;i<100;i++)
{
if(stu3[i].id!="\0") z=0,stu3[i].showshow();
}
if(z==1) cout<<"暂无大学生信息"<<endl;
}
void Statistics_Student()
{
cout<<"请输入统计方式:1.按性别统计 2.按年龄统计"<<endl;
int www;
cin>>www;
if(www==1)
{
int sum1=0;//男num
int sum2=0;//女
for(int i=0;i<100;i++)
{
if(stu1[i].show_sex()==1) sum1++;
if(stu1[i].show_sex()==2) sum2++;
}
cout<<"共有小学生"<<sum1+sum2<<"人,其中男学生有"<<sum1<<"人,女生有"<<sum2<<"人"<<endl;
//
int sum11=0;//男num
int sum22=0;//女
for(int i=0;i<100;i++)
{
if(stu2[i].show_sex()==1) sum11++;
if(stu2[i].show_sex()==2) sum22++;
}
cout<<"共有中学生"<<sum11+sum22<<"人,其中男学生有"<<sum11<<"人,女生有"<<sum22<<"人"<<endl;
//
int sum111=0;//男num
int sum222=0;//女
for(int i=0;i<100;i++)
{
if(stu3[i].show_sex()==1) sum111++;
if(stu3[i].show_sex()==2) sum222++;
}
cout<<"共有大学生"<<sum111+sum222<<"人,其中男学生有"<<sum111<<"人,女生有"<<sum222<<"人"<<endl;
}
else if(www==2)
{
cout<<"请输入要统计的年龄:"<<endl;
float k;
cin>>k;
float ss1,ss2,ss3;
ss1=0;
ss2=0;
ss3=0;
for(int i=0;i<100;i++)
{
if(stu1[i].show_age()==k) ss1++;
if(stu2[i].show_age()==k) ss2++;
if(stu3[i].show_age()==k) ss3++;
}
cout<<"年龄为"<<k<<"岁的学生共有"<<ss1+ss2+ss3<<"人,其中小学生有"<<ss1<<"人,中学生有"<<ss2<<"人,大学生有"<<ss3<<"人"<<endl;
}
}
void Search_Student()
{
cout<<"请输入要查找的学生姓名:"<<endl;
string nn;
cin>>nn;
bool x=1;
for(int i=0;i<100;i++)
{
if(stu1[i].name==nn) x=0,stu1[i].showshow();
if(stu2[i].name==nn) x=0,stu2[i].showshow();
if(stu3[i].name==nn) x=0,stu3[i].showshow();
}
if(x==1) cout<<"没有该学生"<<endl;
}
void Find_Student()
{
cout<<"请输入要查找的学生学号:"<<endl;
string nn;
cin>>nn;
bool x=1;
for(int i=0;i<100;i++)
{
if(stu1[i].id==nn) x=0,stu1[i].showshow();
if(stu2[i].id==nn) x=0,stu2[i].showshow();
if(stu3[i].id==nn) x=0,stu3[i].showshow();
}
if(x==1) cout<<"没有该学生"<<endl;
}
void Change_Student()
{
cout<<"请输入要修改的学生类型:1.小学生 2.中学生 3.大学生"<<endl;
int n;
cin>>n;
cout<<"请输入要修改的学生的学号:"<<endl;
string kk;
cin>>kk;
if(n==1)
{
bool x=1;
for(int i=0;i<100;i++)
{
if(stu1[i].id==kk)
{
x=0;
string name1,id1,sex1,classes1;
float age1,english1,math1,chinese1;
cout<<"请输入更改后的姓名:";
cin>>name1;
stu1[i].set_name(name1);
cout<<"请输入更改后的学号:";
cin>>id1;
bool x=1;
for(int j=0;j<i;j++)
{
if(stu1[j].id==id1)
{
cout<<"学号重复!"<<endl;
x=0;
}
}
if(x==0) break;
stu1[i].set_id(id1);
cout<<"请输入性别:";
cin>>sex1;
stu1[i].set_sex(sex1);
cout<<"请输入更改后的年龄:";
cin>>age1;
stu1[i].set_age(age1);
cout<<"请输入更改后的班级:";
cin>>classes1;
stu1[i].set_classes(classes1);
cout<<"请输入更改后的英语成绩:";
cin>>english1;
stu1[i].setEnglish(english1);
cout<<"请输入更改后的语文成绩:";
cin>>chinese1;
stu1[i].setChinese(chinese1);
cout<<"请输入更改后的数学成绩:";
cin>>math1;
stu1[i].setmath(math1);
break;
}
}
if(x==1) cout<<"该学号不存在!"<<endl;
}
else if(n==2)
{
bool x=1;
for(int i=0;i<100;i++)
{
if(stu2[i].id==kk)
{
x=0;
string name1,id1,sex1,classes1,addr1;
float age1,english1,math1,chinese1,geog1,history1;
cout<<"请输入更改后的姓名:";
cin>>name1;
stu2[i].set_name(name1);
cout<<"请输入更改后的学号:";
cin>>id1;
bool x=1;
for(int j=0;j<i;j++)
{
if(stu2[j].id==id1)
{
cout<<"学号重复!"<<endl;
x=0;
}
}
if(x==0) break;
stu2[i].set_id(id1);
cout<<"请输入性别:";
cin>>sex1;
stu2[i].set_sex(sex1);
cout<<"请输入更改后的年龄:";
cin>>age1;
stu2[i].set_age(age1);
cout<<"请输入更改后的班级:";
cin>>classes1;
stu2[i].set_classes(classes1);
cout<<"请输入更改后的英语成绩:";
cin>>english1;
stu2[i].setEnglish(english1);
cout<<"请输入更改后的语文成绩:";
cin>>chinese1;
stu2[i].setChinese(chinese1);
cout<<"请输入更改后的数学成绩:";
cin>>math1;
stu2[i].setmath(math1);
cout<<"请输入更改后的地理成绩:";
cin>>geog1;
stu2[i].setgeog(geog1);
cout<<"请输入更改后的历史成绩:";
cin>>history1;
stu2[i].sethistory(history1);
cout<<"请输入更改后的地址:";
cin>>addr1;
stu2[i].setaddr(addr1);
break;
}
}
if(x==1) cout<<"该学号不存在!"<<endl;
}
else if(n==3)
{
bool x=1;
for(int i=0;i<100;i++)
{
if(stu3[i].id==kk)
{
x=0;
string name1,id1,sex1,classes1,major1,addr1,tel1;
float age1;
cout<<"请输入更改后的姓名:";
cin>>name1;
stu3[i].set_name(name1);
cout<<"请输入更改后的学号:";
cin>>id1;
bool x=1;
for(int j=0;j<i;j++)
{
if(stu3[j].id==id1)
{
cout<<"学号重复!"<<endl;
x=0;
}
}
if(x==0) break;
stu3[i].set_id(id1);
cout<<"请输入性别:";
cin>>sex1;
stu3[i].set_sex(sex1);
cout<<"请输入更改后的年龄:";
cin>>age1;
stu3[i].set_age(age1);
cout<<"请输入更改后的班级:";
cin>>classes1;
stu3[i].set_classes(classes1);
cout<<"请输入更改后的专业:";
cin>>major1;
stu3[i].setmajor(major1);
cout<<"请输入更改后的地址:";
cin>>addr1;
stu3[i].setaddr(addr1);
cout<<"请输入更改后的电话:";
cin>>tel1;
stu3[i].settel(tel1);
break;
}
}
if(x==1) cout<<"该学号不存在!"<<endl;
}
}
void Out_File()
{
ofstream outt("s_student.dat",ios::out);
if(outt==NULL)
{
cout<<"打开dat文件失败!\n";
}
else
{
for(int i=0;i<100;i++)
{
if(stu1[i].id!="\0")
outt<<stu1[i].id<<" "<<stu1[i].name<<" "<<stu1[i].age<<" "<<stu1[i].classes<<" "<<stu1[i].sex<<" "<<stu1[i].Chinese<<" "<<stu1[i].English<<" "<<stu1[i].math<<" ";
}
}
outt.close();
//
ofstream outt2("m_student.dat",ios::out);
if(outt2==NULL)
{
cout<<"打开dat文件失败!\n";
}
else
{
for(int i=0;i<100;i++)
{
if(stu2[i].id!="\0")
outt2<<stu2[i].id<<" "<<stu2[i].name<<" "<<stu2[i].age<<" "<<stu2[i].classes<<" "<<stu2[i].sex<<" "<<stu2[i].Chinese<<" "<<stu2[i].English<<" "<<stu2[i].math<<" "<<stu2[i].geog<<" "<<stu2[i].history<<" "<<stu2[i].addr<<" ";
}
}
outt2.close();
//
ofstream outt3("l_student.dat",ios::out);
if(outt3==NULL)
{
cout<<"打开dat文件失败!\n";
}
else
{
for(int i=0;i<100;i++)
{
if(stu3[i].id!="\0")
outt3<<stu3[i].id<<" "<<stu3[i].name<<" "<<stu3[i].age<<" "<<stu3[i].classes<<" "<<stu3[i].sex<<" "<<stu3[i].major<<" "<<stu3[i].addr<<" "<<stu3[i].tel<<" ";
}
}
outt3.close();
cout<<"保存成功!"<<endl;
}
void In_File()
{
ifstream in1("s_student_in.txt",ios::in);
if(in1==NULL)
{
cout<<"打开txt文件失败!\n";
}
else
{
int i=0;
while(!in1.eof())
{
in1>>stu1[i].id>> stu1[i].name>> stu1[i].age>> stu1[i].classes>> stu1[i].sex>> stu1[i].Chinese>> stu1[i].English>> stu1[i].math;
i++;
}
}
in1.close();
//
ifstream in2("m_student_in.txt",ios::in);
if(in2==NULL)
{
cout<<"打开txt文件失败!\n";
}
else
{
int i=0;
while(!in2.eof())
{
in2>>stu2[i].id>> stu2[i].name>> stu2[i].age>> stu2[i].classes>> stu2[i].sex>> stu2[i].Chinese>> stu2[i].English>> stu2[i].math>> stu2[i].geog>> stu2[i].history>> stu2[i].addr;
i++;
}
}
in2.close();
//
ifstream in3("l_student_in.txt",ios::in);
if(in3==NULL)
{
cout<<"打开txt文件失败!\n";
}
else
{
int i=0;
while(!in3.eof())
{
in3>>stu3[i].id>> stu3[i].name>> stu3[i].age>> stu3[i].classes>> stu3[i].sex>> stu3[i].major>> stu3[i].addr>> stu3[i].tel;
i++;
}
}
in3.close();
}