📄 ѧ
字号:
// 学生管理程序.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream.h>
#include <string.h>
#include <process.h>
class Person
{
friend class Class;
private:
char *name;
char *sex;
int age;
public:
Person();
void setPerson(char *n1, char *n2, int a);
virtual ~Person();
virtual void print();
};
Person::Person()
{
name=new char[10];
sex=new char[10];
age=18;
}
void Person::setPerson(char *n1, char *n2, int a)
{
name =new char[strlen(n1)+1];
sex =new char[strlen(n2)+1];
strcpy(name,n1);
strcpy(sex,n2);
age=a;
}
Person::~Person()
{
delete []name;
delete []sex;
}
void Person::print()
{
cout<<"\n姓名:"<<name<<endl;
cout<<"性别:"<<sex<<endl;
cout<<"年龄:"<<age<<endl;
}
class Teacher: public Person
{
private:
char *post;
int classes;
public:
Teacher();
void setTeacher(char *n1, char *n2, char *n3, int a, int b);
virtual ~Teacher();
virtual void print();
};
Teacher::Teacher()
{
post=new char[10];
classes=0;
}
void Teacher::setTeacher(char *n1, char *n2, char *n3, int a,int b)
{
setPerson(n1, n2, a);
post =new char[strlen(n3)+1];
strcpy(post,n3);
classes=b;
}
Teacher::~Teacher()
{
delete []post;
}
void Teacher::print()
{
Person::print();
cout<<"职称:"<<post<<endl;
}
class Student: public Person
{
friend Class;
private:
float math, phy, che;
static int totalStudent;
public:
Student();
setStudent(char *n1, char *n2, int a, float c1, float c2, float c3);
virtual ~Student();
void exchange(Student&,Student&);
static int getTotalStudent();
virtual void print();
};
int Student::totalStudent=0;
Student::Student()
{
math=0;
phy=0;
che=0;
totalStudent++;
}
Student::setStudent(char *n1, char *n2, int a, float c1, float c2, float c3)
{
setPerson(n1, n2, a);
math=c1;
phy=c2;
che=c3;
}
void Student::exchange(Student &a,Student &b)
{
Student temp;
temp.math=b.math;
b.math=a.math;
a.math=temp.math;
temp.phy=b.phy;
b.phy=a.phy;
a.phy=temp.phy;
temp.che=b.che;
b.che=a.che;
a.che=temp.che;
}
void Student::print()
{
Person::print();
cout<<"该生成绩:"<<endl;
cout<<"数学:"<<math;
cout<<"\t物理:"<<phy;
cout<<"\t化学:"<<che<<endl;
}
Student::~Student()
{
totalStudent--;
}
int Student::getTotalStudent()
{
return totalStudent;
}
class Class
{
friend class Control;
public:
Class();
~Class();
void creatStu();
void showMath1();
void showMath2();
void showPhy1();
void showPhy2();
void showChe1();
void showChe2();
private:
int sum;
Student *stu;
int classnum;
static int totalClass;
};
int Class::totalClass=0;
void Class::creatStu()
{
totalClass++;
Student::totalStudent+=sum;
stu= new Student[sum];
for(int i=0;i<sum;i++)
{
char *n1=new char[10], *n2=new char[10];
int a;
float c1, c2, c3;
cout<<"\n请输入第 "<<i+1<<"个学生的姓名:";
cin>>n1;
cout<<"请输入性别:(男/女)";
cin>>n2;
bool flag=true;
while(flag==true)
{
if(strcmp(n2,"男")==0||strcmp(n2,"女")==0)
flag=false;
else
{
cout<<"请重新输入性别:(男/女)";
cin>>n2;
}
}
cout<<"请输入年龄:";
cin>>a;
cout<<"请输入数学成绩:";
cin>>c1;
cout<<"请输入物理成绩:";
cin>>c2;
cout<<"请输入化学成绩:";
cin>>c3;
stu[i].setStudent(n1, n2, a, c1, c2, c3);
}
}
Class::Class()
{
classnum=totalClass+1;
cout<<"\n请输入 "<<classnum<<" 班的人数:";
cin>>sum;
creatStu();
}
Class::~Class()
{
delete []stu;
totalClass--;
}
void Class::showMath1()
{
for(int i=0;i<sum;i++)
{
for(int j=i+1;j<sum;j++)
{
if(stu[i].math<stu[j].math)
stu[i].exchange(stu[i],stu[j]);
}
}
for(int k=0;k<sum;k++)
stu[k].print();
}
void Class::showMath2()
{
for(int i=0;i<sum;i++)
{
for(int j=i+1;j<sum;j++)
{
if(stu[i].math>stu[j].math)
stu[i].exchange(stu[i],stu[j]);
}
}
for(int k=0;k<sum;k++)
stu[k].print();
}
void Class::showPhy1()
{
for(int i=0;i<sum;i++)
{
for(int j=i+1;j<sum;j++)
{
if(stu[i].phy<stu[j].phy)
stu[i].exchange(stu[i],stu[j]);
}
stu[i].print();
}
}
void Class::showPhy2()
{
for(int i=0;i<sum;i++)
{
for(int j=i+1;j<sum;j++)
{
if(stu[i].phy>stu[j].phy)
stu[i].exchange(stu[i],stu[j]);
}
stu[i].print();
}
}
void Class::showChe1()
{
for(int i=0;i<sum;i++)
{
for(int j=i+1;j<sum;j++)
{
if(stu[i].che<stu[j].che)
stu[i].exchange(stu[i],stu[j]);
}
stu[i].print();
}
}
void Class::showChe2()
{
for(int i=0;i<sum;i++)
{
for(int j=i+1;j<sum;j++)
{
if(stu[i].che>stu[j].che)
stu[i].exchange(stu[i],stu[j]);
}
stu[i].print();
}
}
class Control
{
public:
void menu();
~Control();
void showInfo(Class);
private:
int classAll,teacAll;
Teacher *teac;
Student *stu;
Class *clas;
};
void Control::showInfo(Class classtemp)
{
stu=classtemp.stu;
for(int i=0;i<classtemp.sum;i++)
{
stu[i].print();
}
}
Control::~Control()
{
delete []stu;
delete []teac;
delete []clas;
}
void Control::menu()
{
label1:
cout<<"*************学生成绩管理系统*****************"<<endl;
cout<<"1.创建"<<endl;
cout<<"2.查询"<<endl;
cout<<"3.退出"<<endl;
int a;
cout<<"请选择(1-3):";
cin>>a;
label2:
switch(a)
{
case 1:
cout<<"-----------------------"<<endl;
cout<<"1.输入班级信息"<<endl;
cout<<"2.输入老师信息"<<endl;
cout<<"3.放弃(返回上一层)"<<endl;
int b;
cout<<"请选择(1-3):";
cin>>b;
switch(b)
{
case 1:
if(classAll>0)
cout<<"班级已经创建!"<<endl;
else
{
cout<<"总共几个班?";
cin>>classAll;
clas= new Class[classAll];
}
goto label2;
case 2:
if(teacAll>0)
cout<<"教师已经创建!"<<endl;
else
{
cout<<"总共多少个教师?";
cin>>teacAll;
teac= new Teacher[teacAll];
for(int i=0;i<teacAll;i++)
{
char *n1=new char[10], *n2=new char[10], *n3=new char[10];
int a, b;
cout<<"\n请输入第 "<<i+1<<"个老师的姓名:";
cin>>n1;
cout<<"请输入性别:(男/女)";
cin>>n2;
bool flag=true;
while(flag==true)
{
if(strcmp(n2,"男")==0||strcmp(n2,"女")==0)
flag=false;
else
{
cout<<"请重新输入性别:(男/女)";
cin>>n2;
}
}
cout<<"请输入年龄:";
cin>>a;
cout<<"请输入班级:";
cin>>b;
cout<<"请输入教师职称:";
cin>>n3;
teac[i].setTeacher(n1, n2, n3, a, b);
}
}
goto label2;
case 3:
goto label1;
default:
cout<<"请按要求重新输入!"<<endl;
goto label2;
}
case 2:
label3:
cout<<"-----------------------"<<endl;
cout<<"请输入要查询的内容:"<<endl;
cout<<"1.老师信息(包含职称)"<<endl;
cout<<"2.学生信息(包含成绩)"<<endl;
cout<<"3.系统内人员构成"<<endl;
cout<<"4.放弃(返回上一层)"<<endl;
int d;
cout<<"请选择(1-4):";
cin>>d;
switch(d)
{
case 1:
if(teacAll<0)
cout<<"\n老师尚未创建!"<<endl;
else
{
cout<<"老师:"<<endl;
for(int j=0;j<teacAll;j++)
teac[j].print();
cout<<"\n共有"<<teacAll<<"个老师。"<<endl;
}
goto label3;
case 2:
label4:
cout<<"-----------------------"<<endl;
cout<<"1.按各班数学成绩从高到低排序"<<endl;
cout<<"2.按各班数学成绩从低到高排序"<<endl;
cout<<"3.按各班物理成绩从高到低排序"<<endl;
cout<<"4.按各班物理成绩从低到高排序"<<endl;
cout<<"5.按各班化学成绩从高到低排序"<<endl;
cout<<"6.按各班化学成绩从低到高排序"<<endl;
cout<<"7.放弃(返回上一层)"<<endl;
int e;
cout<<"请选择(1-7):";
cin>>e;
switch(e)
{
case 1:
if(classAll<0)
cout<<"\n学生尚未创建!"<<endl;
else
{
for(int i=0;i<classAll;i++)
{
cout<<"\n"<<i+1<<"班的学生:"<<endl;
clas[i].showMath1();;
}
}
goto label4;
case 2:
if(classAll<0)
cout<<"\n学生尚未创建!"<<endl;
else
{
for(int i=0;i<classAll;i++)
{
cout<<"\n"<<i+1<<"班的学生:"<<endl;
clas[i].showMath2();
}
}
goto label4;
case 3:
if(classAll<0)
cout<<"\n学生尚未创建!"<<endl;
else
{
for(int i=0;i<classAll;i++)
{
cout<<"\n"<<i+1<<"班的学生:"<<endl;
clas[i].showPhy1();
}
}
goto label4;
case 4:
if(classAll<0)
cout<<"\n学生尚未创建!"<<endl;
else
{
for(int i=0;i<classAll;i++)
{
cout<<"\n"<<i+1<<"班的学生:"<<endl;
clas[i].showPhy2();
}
}
goto label4;
case 5:
if(classAll<0)
cout<<"\n学生尚未创建!"<<endl;
else
{
for(int i=0;i<classAll;i++)
{
cout<<"\n"<<i+1<<"班的学生:"<<endl;
clas[i].showChe1();
}
}
goto label4;
case 6:
if(classAll<0)
cout<<"\n学生尚未创建!"<<endl;
else
{
for(int i=0;i<classAll;i++)
{
cout<<"\n"<<i+1<<"班的学生:"<<endl;
clas[i].showChe2();
}
}
goto label4;
case 7:
goto label3;
default:
cout<<"请按要求重新输入!"<<endl;
goto label4;
}
case 3:
cout<<"-----------------------"<<endl;
if(classAll<0)
cout<<"\n学生尚未创建!"<<endl;
else
{
cout<<"\n共有"<<Student::getTotalStudent()/2<<"个学生。"<<endl;
cout<<"\n共有"<<classAll<<"个班级。"<<endl;
}
if(teacAll<0)
cout<<"\n老师尚未创建!"<<endl;
else
cout<<"\n共有"<<teacAll<<"个老师。"<<endl;
goto label3;
case 4:
goto label1;
default:
cout<<"请按要求重新输入!"<<endl;
goto label3;
}
case 3:
exit(1);
default:
cout<<"请按要求重新输入!"<<endl;
goto label1;
}
}
void main(int argc, char* argv[])
{
Control con;
con.menu();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -