📄 学生学籍cpp(2).txt
字号:
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
struct score
{
float math;
float English;
float computer;
};
struct student
{
char num[12];
char name[20];
char sex[2];
int age;
char adr[40];
struct score sco; //结构体嵌套。
};
struct student stu[30];
int i,j;
char a[4],b[4],c[4],d[8],e[4],f[4],g[4],h[6];
//第一部分:输入学生基本信息。
void InputInfo()
{
ofstream outfile("学生基本信息.txt",ios::out);//定义输出文件流对象,以输出方式打开磁盘文件
if(!outfile)
{
cerr<<"open error!"<<endl;
return;
}
outfile<<"学号"<<setw(10)<<"姓名"<<setw(10)<<"性别"<<setw(10)<<"年龄"<<setw(14)<<"家庭地址"<<endl;
cout<<"请输入学生的人数";
cin>>j;
for(i=0;i<j;i++)
{
cout<<"学号";cin>>stu[i].num;
cout<<"姓名";cin>>stu[i].name;
cout<<"性别";cin>>stu[i].sex;
cout<<"年龄";cin>>stu[i].age;
cout<<"地址";cin>>stu[i].adr;
cout<<setiosflags(ios::right);
outfile<<setw(3)<<stu[i].num<<setw(10)<<stu[i].name<<setw(10)<<stu[i].sex<<setw(10)<<stu[i].age<<setw(14)<<stu[i].adr<<endl;
}
outfile.close();
}
//第二部分:输入学习成绩函数。
void score1() //第一学期成绩函数
{
fstream infile("学生基本信息.txt",ios::in); //定义输入文件流,以输入方式打开磁盘文件。
if(!infile)
{
cerr<<"open error!"<<endl;
return ;
}
infile>>a>>b>>c>>d>>e;
//infile>>stu[i].num>>stu[i].name>>stu[i].sex>>stu[i].age>>stu[i].adr;
ofstream outfile("学生成绩信息1.txt",ios::out); //定义输出文件流,以输出方式打开磁盘文件。
if(!outfile)
{
cerr<<"open error!"<<endl;
return ;
}
outfile<<"学号"<<setw(10)<<"姓名"<<setw(10)<<"性别"<<setw(10)<<"高数"<<setw(10)<<"英语"<<setw(12)<<"计算机"<<endl;
cout<<"请按学号输入学生成绩:"<<endl;
for(i=0;i<j;i++)
{
cout<<"请输入第一学期学生成绩。"<<endl;
cout<<"请输入学号为"<<stu[i].num<<"的成绩"<<endl;
cout<<"数学:";cin>>stu[i].sco.math;
cout<<"英语:";cin>>stu[i].sco.English;
cout<<"计算机:";cin>>stu[i].sco.computer;
infile>>stu[i].num>>stu[i].name>>stu[i].sex>>stu[i].age>>stu[i].adr;
//infile>>stu[i].num>>stu[i].name>>stu[i].sex;
outfile<<setiosflags(ios::right);
outfile<<setw(3)<<stu[i].num<<setw(10)<<stu[i].name<<setw(10)<<stu[i].sex<<setw(10)<<stu[i].sco.math<<setw(10)<<stu[i].sco.English<<setw(12)<<stu[i].sco.computer<<endl;
}
infile.close();
outfile.close();
}
void score2() //第二学期成绩
{fstream infile("学生基本信息.txt",ios::in); //定义输入文件流,以输入方式打开磁盘文件。
if(!infile)
{
cerr<<"open error!"<<endl;
return ;
}infile>>a>>b>>c>>d>>e;
ofstream outfile("学生成绩信息2.txt",ios::out); //定义输出文件流,以输出方式打开磁盘文件。
if(!outfile)
{
cerr<<"open error!"<<endl;
return ;
}
outfile<<"学号"<<setw(10)<<"姓名"<<setw(10)<<"性别"<<setw(10)<<"高数"<<setw(10)<<"英语"<<setw(12)<<"计算机"<<endl;
cout<<"请输入第二学期学生成绩。"<<endl;
cout<<"请按学号输入学生成绩:"<<endl;
for(i=0;i<j;i++)
{
cout<<"请输入学号为"<<stu[i].num<<"的成绩"<<endl;
cout<<"数学:";cin>>stu[i].sco.math;
cout<<"英语:";cin>>stu[i].sco.English;
cout<<"计算机:";cin>>stu[i].sco.computer;
infile>>stu[i].num>>stu[i].name>>stu[i].sex>>stu[i].age>>stu[i].adr;
outfile<<setiosflags(ios::right);
outfile<<setw(3)<<stu[i].num<<setw(10)<<stu[i].name<<setw(10)<<stu[i].sex;
outfile<<setw(10)<<stu[i].sco.math<<setw(10)<<stu[i].sco.English<<setw(12)<<stu[i].sco.computer<<endl;
}
infile.close();
outfile.close();
}
//第三部分:查询功能函数。
void search_1()
{ char m[12];
int n;
cout<<"请输入学号:";
cin>>m;
for(i=0;i<j;i++)
{
if(strcmp(m,stu[i].num)==0)
{
do{
cout<<"1.第一学期成绩:"<<"2.第二学期成绩"<<"3.返回"<<endl;
cin>>n;
switch (n)
{
case 1: //查询第一学期成绩
{
fstream infile("学生成绩信息1.txt",ios::in);
if(!infile)
{
cerr<<"open error!"<<endl;
return ;
}infile>>a>>b>>c>>f>>g>>h;
infile.seekg(i*sizeof(stu[i]),ios::beg);
infile>>stu[i].num>>stu[i].name>>stu[i].sex>>stu[i].sco.math>>stu[i].sco.English>>stu[i].sco.computer;
infile.close();
cout<<"学号"<<setw(10)<<"姓名"<<setw(10)<<"性别"<<setw(10)<<"高数"<<setw(10)<<"英语"<<setw(12)<<"计算机"<<endl;
cout<<setiosflags(ios::right);
cout<<setw(3)<<stu[i].num<<setw(10)<<stu[i].name<<setw(10)<<stu[i].sex;
cout<<setw(10)<<stu[i].sco.math<<setw(10)<<stu[i].sco.English<<setw(12)<<stu[i].sco.computer<<endl;
};break;
case 2: //查询第二学期成绩
{
ifstream infil("学生成绩信息2.txt",ios::in);
if(!infil)
{
cerr<<"open error!"<<endl;
return ;
}infil>>a>>b>>c>>f>>g>>h;
infil.seekg(i*sizeof(stu[i]),ios::beg);
infil>>stu[i].num>>stu[i].name>>stu[i].sex>>stu[i].sco.math>>stu[i].sco.English>>stu[i].sco.computer;
infil.close();
cout<<"学号"<<setw(10)<<"姓名"<<setw(10)<<"性别"<<setw(10)<<"高数"<<setw(10)<<"英语"<<setw(12)<<"计算机"<<endl;
cout<<setiosflags(ios::right);
cout<<setw(3)<<stu[i].num<<setw(10)<<stu[i].name<<setw(10)<<stu[i].sex;
cout<<setw(10)<<stu[i].sco.math<<setw(10)<<stu[i].sco.English<<setw(12)<<stu[i].sco.computer<<endl;
};break;
case 3:cout<<"请再次按3键回到主菜单"<<endl; break;
default : cout<<"输入错误。"<<endl;
}
} while(n!=3);
} //if语句结束
else cout<<endl;
} //for 循环结束
}
void search_2()
{
int m;
char a[10];
cout<<"请输入姓名:";
cin>>a;
for(i=0;i<j;i++)
{
if(strcmp(a,stu[i].name)==0)
{
do{
cout<<"1.第一学期成绩:"<<"2.第二学期成绩"<<"3.返回"<<endl;
cin>>m;
switch (m)
{
case 1: //查询第一学期成绩
{ fstream infile("学生成绩信息1.txt",ios::in);
if(!infile)
{
cerr<<"open error!"<<endl;
return ;
}infile>>a>>b>>c>>f>>g>>h;
infile.seekg(i*sizeof(stu[i]),ios::beg);
infile>>stu[i].num>>stu[i].name>>stu[i].sex>>stu[i].sco.math>>stu[i].sco.English>>stu[i].sco.computer;
cout<<"学号"<<setw(10)<<"姓名"<<setw(10)<<"性别"<<setw(10)<<"高数"<<setw(10)<<"英语"<<setw(12)<<"计算机"<<endl;
cout<<setiosflags(ios::right);
cout<<setw(3)<<stu[i].num<<setw(10)<<stu[i].name<<setw(10)<<stu[i].sex<<setw(10)<<stu[i].sco.math<<setw(10)<<stu[i].sco.English<<setw(12)<<stu[i].sco.computer<<endl;
infile.close();
};break;
case 2: //查询第二学期成绩
{
ifstream infil("学生成绩信息2.txt",ios::in);
if(!infil)
{
cerr<<"open error!"<<endl;
return ;
}infil>>a>>b>>c>>f>>g>>h;
infil>>stu[i].num>>stu[i].name>>stu[i].sex>>stu[i].sco.math>>stu[i].sco.English>>stu[i].sco.computer;
cout<<"学号"<<setw(10)<<"姓名"<<setw(10)<<"性别"<<setw(10)<<"高数"<<setw(10)<<"英语"<<setw(12)<<"计算机"<<endl;
cout<<setiosflags(ios::right);
cout<<setw(3)<<stu[i].num<<setw(10)<<stu[i].name<<setw(10)<<stu[i].sex<<setw(10)<<stu[i].sco.math<<setw(10)<<stu[i].sco.English<<setw(12)<<stu[i].sco.computer<<endl;
infil.close();
};break;
case 3:cout<<"请再次按3键回到主菜单"<<endl; break;
default : cout<<"输入错误。"<<endl;
}
} while(m!=3);
} //if语句结束
else cout<<endl;
} //for 循环结束
}
void search()
{
int m;
cout<<"请选择查询方法:"<<"1:按学号查询."<<"2:按姓名查询."<<"3.退出"<<endl;
for( m=1;m<=2;m++)
{
cin>>m;
switch(m)
{
case 1: search_1();break;
case 2: search_2();break;
case 3: break;
default:cout<<"错误,请重新选择:";
}
}
}
//第四部分:数据统计。
void count()
{
int k=0;
int q=0;
cout<<"第一学期不及格课程学生信息"<<endl; //第一学期成绩信息
fstream infile("学生成绩信息1.txt",ios::in);
if(!infile)
{
cerr<<"open error!"<<endl;
return ;
}infile>>a>>b>>d>>f>>g>>h;
for(i=0;i<j;i++)
{ infile>>stu[i].num>>stu[i].name>>stu[i].sex>>stu[i].sco.math>>stu[i].sco.English>>stu[i].sco.computer;
if(stu[i].sco.math||stu[i].sco.English||stu[i].sco.computer<60)
k++;
}
cout<<"不及格人数为:"<<k<<endl;
ofstream outfile("fail_1.txt",ios::out);//定义输出文件流对象,以输出方式打开磁盘文件
if(!outfile)
{
cerr<<"open error!"<<endl;
return;
}
cout<<"不及格信息:"<<endl;
for(i=0;i<j;i++)
{
if(stu[i].sco.math<60) {cout<<"姓名:"<<stu[i].name<<"数学:"<<stu[i].sco.math<<endl; outfile<<"姓名:"<<stu[i].name<<"数学:"<<stu[i].sco.math<<endl;}
if(stu[i].sco.English<60) { cout<<"姓名:"<<stu[i].name<<"英语:"<<stu[i].sco.English<<endl; outfile<<"姓名:"<<stu[i].name<<"英语:"<<stu[i].sco.English<<endl;}
if(stu[i].sco.computer<60) {cout<<"姓名:"<<stu[i].name<<"计算机:"<<stu[i].sco.computer<<endl; outfile<<"姓名:"<<stu[i].name<<"计算机:"<<stu[i].sco.computer<<endl;}
else cout<<endl;
}
cout<<"第二学期不及格课程学生信息。"<<endl; //第二学期成绩信息
fstream infil("学生成绩信息2.txt",ios::in);
if(!infil)
{
cerr<<"open error!"<<endl;
return ;
}infil>>a>>b>>d>>f>>g>>h;
for(i=0;i<j;i++)
{ infil>>stu[i].num>>stu[i].name>>stu[i].sex>>stu[i].sco.math>>stu[i].sco.English>>stu[i].sco.computer;
if(stu[i].sco.math||stu[i].sco.English||stu[i].sco.computer<60)
q++;
}
cout<<"不及格人数为:"<<q<<endl;
ofstream outfil("fail_2.txt",ios::out);//定义输出文件流对象,以输出方式打开磁盘文件
if(!outfil)
{
cerr<<"open error!"<<endl;
return;
}
cout<<"不及格信息:"<<endl;
for(i=0;i<j;i++)
{
if(stu[i].sco.math<60) {cout<<"姓名:"<<stu[i].name<<"数学:"<<stu[i].sco.math<<endl; outfil<<"姓名:"<<stu[i].name<<"数学:"<<stu[i].sco.math<<endl;}
if(stu[i].sco.English<60) { cout<<"姓名:"<<stu[i].name<<"英语:"<<stu[i].sco.English<<endl; outfil<<"姓名:"<<stu[i].name<<"英语:"<<stu[i].sco.English<<endl;}
if(stu[i].sco.computer<60) {cout<<"姓名:"<<stu[i].name<<"计算机:"<<stu[i].sco.computer<<endl; outfil<<"姓名:"<<stu[i].name<<"计算机:"<<stu[i].sco.computer<<endl;}
else cout<<endl;
}
}
int main()
{
int m,n;
do{
cout<<"********************************学生成绩管理系统******************************"<<endl;
cout<<"1.输入学生基本信息"<<"2.输入学生成绩"<<"3.数据查询"<<"4.统计数据"<<"5.退出系统"<<endl;
cout<<"******************************************************************************"<<endl;
cout<<"请选择功能对应的数字键";
cin>>m;
switch(m)
{
case 1:
InputInfo();break;
case 2:
do
{
cout<<"1.第一学期。"<<"2.第二学期。"<<"3.完成。"<<endl;
cin>>n;
switch(n)
{
case 1:score1();break;
case 2:score2();break;
case 3: break;
default :cout<<"输入错误";
}
}while(n<3);break;
case 3: search();break;
case 4:count();break;
case 5: break;
default :cout<<"再见,欢迎使用!"<<endl;
}
}while (m!=5);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -