📄 学生成绩管理系统.cpp
字号:
#include<iostream.h>
#include<string.h>
#include<fstream.h>
#include<stdlib.h>
#include<iomanip.h>
class student
{
private:
char name[20]; //姓名
double cpro,english,math;//课程
int number; //学号
public:
~student(){}
student(char n[20]="haha",int nu=0,double cc=100,double eng=100,double ma=100)
{
strcpy(name,n);
number=nu;
cpro=cc; english=eng;math=ma;
}
void add();
void update();
void select_name();
void select_num();
void display();
void file_input(ifstream &f);
void del();
void count();
};
char p;char w;
student t[50];
int f_num; //表示文件中记录数
int flag=0;
void student::file_input(ifstream &f)
{
f_num=0;
while(f.good())
{
f>>t[f_num].name;
f>>t[f_num].number;
f>>t[f_num].cpro;
f>>t[f_num].english;
f>>t[f_num].math;
f_num++;
}
}
void student::add()
{
char name[20];
ofstream *fp;
int number;double cpro,english,math;
char c;
do
{
cout<<" 请输入您要增加的学生的姓名:";
cin>>name;
cout<<endl<<" 请输入学号:";
cin>>number;
cout<<endl<<" 请输入C++成绩:";
cin>>cpro;
cout<<endl<<" 请输入英语成绩:";
cin>>english;
cout<<endl<<" 请输入数学成绩:";
cin>>math;
cout<<endl;
fp=new ofstream("document",ios::ate);
*fp<<name<<" "<<number<<" "<<cpro<<" "<<english<<" "<<math<<endl;
cout<<" 数据输入成功,想继续数入吗(y/n):";
cin>>c;
cout<<endl;
if(c!='y'&&c!='n')
{
cout<<" 指令错误!请重新输入!"<<endl<<" ";
cin>>c;
}
}while(c=='y');
}
void student::update()
{
char name[20];int nu, k=0;double cc,eng,ma;flag=0;
char c;
ofstream *fp, *fp1;
ifstream f("document");
file_input(f);
if(f_num==0)
{
cout<<" 管理系统中没有输入数据!"<<endl;
}
else
{
do
{
cout<<" 请输入您要修改的学生的姓名:";
cin>>name;
cout<<endl;
for(int h=0;h<f_num;h++) //h纪录要修改学生的位置
{
if(strcmp(name,t[h].name)==0)
{
flag=1;
fp1=new ofstream("document",ios::out);
*fp1<<"";
cout<<" 请输入新的学号:";
cin>>nu;
cout<<endl<<" 请输入C++成绩:";
cin>>cc;
cout<<endl<<" 请输入英语成绩:";
cin>>eng;
cout<<endl<<" 请输入数学成绩:";
cin>>ma;
cout<<endl;
t[h].cpro=cc;
t[h].english=eng;
t[h].math=ma;
t[h].number=nu;
fp=new ofstream("document",ios::ate);
for(k=0;k<f_num-1;k++)
*fp<<t[k].name<<" "<<t[k].number<<" "<<t[k].cpro<<" "<<t[k].english<<" "<<t[k].math<<endl;
cout<<" 修改成功!"<<endl;
}
}
if(flag==0)
{
cout<<" 您要修改的学生不存在!Check it!"<<endl;
}
cout<<" 想继续修改吗(y/n):";
cin>>c;
cout<<endl;
if(c!='y'&&c!='n')
{
cout<<" 指令错误!请重新输入!"<<endl<<" ";
cin>>c;
}
}while(c=='y');
}
}
void student::select_name()
{
char n[20];char c;
ifstream f("document");
file_input(f);
if(f_num==0)
{
cout<<" 管理系统中没有输入数据!"<<endl;
}
else
{
do{
int flag=0;
cout<<" 请输入你要查询的学生姓名:";
cin>>n;
cout<<endl;
for(int j=0;j<f_num-1;j++)
{
if(strcmp(n,t[j].name)==0)
{
flag=1;
cout<<" 您要查询的学生是:"<<t[j].name<<endl;
cout<<" "<<t[j].name<<"的成绩是: "<<" C++:"<<t[j].cpro<<" 英语:"<<t[j].english<<" 数学:"<<t[j].math<<endl;
}
}
if(flag==0)
cout<<" 您要查询的学生不存在!"<<endl;
cout<<" 您想继续查询吗?(y/n):";
cin>>c;
cout<<endl;
if(c!='y'&&c!='n')
{
cout<<" 指令错误!请重新输入!"<<endl;
cin>>c;
}
}while(c=='y');
}
}
void student::select_num()
{
ifstream f("document");
file_input(f);
int n,j=0;char c;
if(f_num==0){
cout<<" 管理系统中没有输入数据!"<<endl;
}
else
{
do{
int flag=0;
cout<<" 请输入你要查询的学生的学号:";
cin>>n;
cout<<endl;
for(int j=0;j<f_num-1;j++)
{
if(t[j].number==n)
{
flag=1;
cout<<" 您要查询的学生是:"<<t[j].name<<endl;
cout<<" "<<t[j].name<<"的成绩是: "<<" C++:"<<t[j].cpro<<" 英语:"<<t[j].english<<" 数学:"<<t[j].math<<endl;
}
}
if(flag==0)
cout<<" 对不起!您要查询的学生不存在!"<<endl;
cout<<" 您想继续查询吗?(y/n):";
cin>>c;
cout<<endl;
if(c!='y'&&c!='n')
{
cout<<" 指令错误!请重新输入!"<<endl;
cout<<" ";
cin>>c;
}
}while(c=='y');
}
}
void student::display()
{
ifstream f("document");
file_input(f);
if(f_num==0)
{
cout<<" 管理系统中没有输入数据!"<<endl;
}
else
{
cout<<" 本系统所有学生数据如下:"<<endl;
cout<<" 姓名 学号 c++ 英语 数学 "<<endl;
for(int k=0;k<f_num-1;k++)
{
cout<<setw(10)<<t[k].name<<setw(8)<<t[k].number<<setw(6)<<t[k].cpro<<setw(6)<<t[k].english<<setw(6)<<t[k].math<<endl;
}
}
}
void student::del()
{
int num, goal, j;
ofstream *fp, *fp1;
ifstream f("document");
file_input(f);
if(f_num==0) cout<<" 系统中没有任何数据!"<<endl<<" ";
else
{
cout<<" 输入要删除的学生的学号:";
cin>>num;
for(j=0;j<f_num;j++)
{
if(t[j].number==num)
{
goal=j;
break;
}
}
fp1=new ofstream("document",ios::out);
*fp1<<"";
fp=new ofstream("document",ios::ate);
for(j=0;j<f_num-1;j++)
{
if(j!=goal)
*fp<<t[j].name<<" "<<t[j].number<<" "<<t[j].cpro<<" "<<t[j].english<<" "<<t[j].math<<endl;
}
cout<<" 删除成功!"<<endl;
}
}
void student::count()
{
int b,w;
double t_c=0, t_e=0, t_m=0, best_c=0, best_e=0, best_m=0, worst_c=100, worst_e=100, worst_m=100, best_s=0, worst_s=300;
ifstream f("document");
file_input(f);
for(int j=0;j<f_num;j++)
{
t_c+=t[j].cpro;t_e+=t[j].english;t_m+=t[j].math;
if(t[j].cpro>best_c) best_c=t[j].cpro;
if(t[j].english>best_e) best_e=t[j].english;
if(t[j].math>best_m) best_m=t[j].math;
if(t[j].cpro<worst_c) worst_c=t[j].cpro;
if(t[j].english<worst_e) worst_e=t[j].english;
if(t[j].math<worst_m) worst_m=t[j].math;
if(t[j].cpro+t[j].english+t[j].math>best_s) {b=j;best_s=t[j].cpro+t[j].english+t[j].math;}
if(t[j].cpro+t[j].english+t[j].math<worst_s) {w=j;worst_s=t[j].cpro+t[j].english+t[j].math;}
}
cout<<" C++ 平均分:"<<t_c/j<<endl;
cout<<" 英语平均分:"<<t_e/j<<endl;
cout<<" 数学平均分:"<<t_m/j<<endl<<endl;
cout<<" C++ 单科最高分:"<<best_c<<endl;
cout<<" 英语单科最高分:"<<best_e<<endl;
cout<<" 数学单科最高分:"<<best_m<<endl<<endl;
cout<<" 最好的学生:"<<t[b].name<<" 总分:"<<best_s<<endl;
cout<<" 最差的学生:"<<t[w].name<<" 总分:"<<worst_s<<endl;
}
void main()
{
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<<"\n *****************************************"<<endl;
cout<<" 选择1-8数字进行操作"<<endl;
cout<<" ";
student stu;
do //flag判断输入是否有效
{
cin>>p;
if((p>='1'&&p<='8'))
flag=1;
else
cout<<" 指令错误!请重新输入:"<<endl<<" ";
}while(flag==0);
do{
switch(p) //接收功能选项
{
case '1': stu.add(); break; //增加数据
case '2': stu.update(); break; //修改数据
case '3': stu.del(); break;
case '4': stu.select_name(); break; //按姓名查询
case '5': stu.select_num(); break; //按学号查询
case '7': stu.count();break;
case '6': stu.display(); break; //输出
case'8'://退出
{exit(0); }
}
cout<<" 您想继续进行其他操作吗?(y/n):";
int flag=0;
do
{
cin>>w;
cout<<endl;
if(w!='y'&&w!='n')
cout<<" 指令错误!请重新输入!"<<endl;
else
flag=1;
}while(flag==0);
if(w=='y'){
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<<" ";
cin>>p;
}while(w=='y');
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -