📄 manage.cpp
字号:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
//////////////////////////////////////声明一个结构体类型Student///////////////////////////////////
struct Student
{
int num;
char name[10];
char sex[3];
int age;
int Chinese;
int English;
int Match;
int C;
int Physics;
int average;
};
int main()
{
/////////////////////////////////////声明被调用的函数//////////////////////////////////
void StatStudent();
void ModifStudent();
void DemandStudent();
void end();
void BuildStudent();
/////////////////////////////////////////显示菜单//////////////////////////////////////
cout<<" 欢迎进入学生成绩管理系统 "<<endl;
cout<<"|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^|"<<endl;
cout<<"|1 数据输入 2 数据查询 3 数据修改|"<<endl;
cout<<"|4 统计数据 0 退出系统 |"<<endl;
cout<<"|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^|"<<endl;
cout<<endl;
///////////////////////////////////////选择相应操作/////////////////////////////////////
cout<<"请输入所需相应操作的序号:";
int flag;
cin>>flag;
if(flag==1)
{BuildStudent(); //进入数据输入。
main(); //返回主函数。
}
if(flag==2)
{DemandStudent(); //进入数据查询
}
if(flag==3)
{cout<<"请输入密码:"; //权限修改,需要输入密码。
int code;
cin>>code;
if(code==123)
{cout<<"输入正确!"<<endl;
ModifStudent(); //密码正确,进入修改。
}
else
{cout<<"输入错误!"<<endl;
main(); //密码错误,返回主菜单。
}
}
if(flag==4)
{StatStudent(); //进入统计数据
}
if(flag==0)
{end(); //输入0退出系统。
}
if(flag!=0&&flag!=1&&flag!=2&&flag!=3&&flag!=4)
{cout<<"输入错误,请重新选择:"<<endl; //如果输入的非0、1、2、3、4返回主菜单重新选择。
main();
}
return 0;
}
/////////////////////////////////////退出系统显示内容//////////////////////////////
void end()
{
cout<<"|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^|"<<endl;
cout<<"| 感谢对本系统的支持,欢迎您再次使用。 |"<<endl;
cout<<"| 作者:网络061 徐欣 |"<<endl;
cout<<"|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^|"<<endl;
}
////////////////////////////////数据输入////////////////////////////////////
static int amount=1; //定义一个静态全局变量,以记录本次录入的学生个数。
void BuildStudent()
{
ofstream outfile("d:\\data.txt",ios::app); //定义文件流对象,打开磁盘文件"d:\\data.txt"。
if(! outfile) //如果打开失败,退出系统。
{cout<<"open error!"<<endl;
exit(1);
}
for(int a=1;a!=0;a++)
{
static int n; //定义静态变量n,防止连续输入造成的学号重复用。
amount++;
Student stu[100];
cout<<"请输入学生学号:"<<endl;
cout<<"(输入0返回主菜单)"<<endl;
cons: cin>>stu[a-1].num;
if(stu[a-1].num==0) break; //如果输入的为0,结束循环即结束本函数。
if(stu[a-1].num==n)
{cout<<"学号已存在,请重新输入:"; //防止连续输入造成的学号重复。
goto cons;
}
n=stu[a-1].num;
if(stu[a-1].num!=0) cout<<"请按顺序输入\"姓名 性别 年龄\"学生信息:"<<endl;
cin>>stu[a-1].name;
cin>>stu[a-1].sex;
cin>>stu[a-1].age;
cout<<"请按顺序输入\"语文 英语 数学 C语言 物理\"学生成绩:"<<endl;
cin>>stu[a-1].Chinese;
cin>>stu[a-1].English;
cin>>stu[a-1].Match;
cin>>stu[a-1].C;
cin>>stu[a-1].Physics;
stu[a-1].average=(stu[a-1].Chinese+stu[a-1].English+stu[a-1].Match+stu[a-1].C+
stu[a-1].Physics)/5;
outfile<<setw(7)<<stu[a-1].num<<setw(7)<<stu[a-1].name<<setw(7)<<stu[a-1].sex<<setw(7)
<<stu[a-1].age<<setw(7)<<stu[a-1].Chinese<<setw(7)<<stu[a-1].English<<setw(7)
<<stu[a-1].Match<<setw(7)<<stu[a-1].C<<setw(7)<<stu[a-1].Physics<<setw(4)
<<stu[a-1].average; //向文件输出数据。
outfile<<endl;
cout<<"是否继续输入数据:Y/N"<<endl;
getchar();
if(getchar()==78) //输入N结束循环即结束本函数。
{ifstream infile("d:\\data.txt",ios::in);
if(! infile)
{cerr<<"open error!"<<endl;
exit(1);
}
int i=0;
while(!infile.eof())
{infile>>stu[i].num; //从文件读取数据。
infile>>stu[i].name;
infile>>stu[i].sex;
infile>>stu[i].age;
infile>>stu[i].Chinese;
infile>>stu[i].English;
infile>>stu[i].Match;
infile>>stu[i].C;
infile>>stu[i].Physics;
infile>>stu[i].average;
i++; //记录结构体数组元素个数。
}
cout<<"成功输入"<<amount-1<<"个学生数据"<<endl;
cout<<"文件中现有"<<i-1<<"个学生数据"<<endl;
cout<<"|学号 |姓名 |性别 |年龄 |语文 |英语 |数学 |C语言 |物理 |平均分"<<endl;
for(int b=0;b<i-1;b++) //显示现有数据。
{cout<<setiosflags(ios::left);
cout<<"|"<<setw(7)<<stu[b].num<<"|"<<setw(7)<<stu[b].name<<"|"<<setw(7)<<stu[b].sex
<<"|"<<setw(7)<<stu[b].age<<"|"<<setw(7)<<stu[b].Chinese<<"|"<<setw(7)
<<stu[b].English<<"|"<<setw(7)<<stu[b].Match<<"|"<<setw(8)<<stu[b].C<<"|"<<setw(7)
<<stu[b].Physics<<"|"<<stu[b].average;
cout<<endl;
}
break;
}
else continue;
}
}
//////////////////////////////////////////////查询数据////////////////////////////////////////////////
void DemandStudent()
{
Student stu[100];
ifstream infile("d:\\data.txt",ios::in); //定义一个文件流对象,以输入方式打开磁盘文件"d:\\data.txt"。
if(! infile)
{cerr<<"open error!"<<endl;
exit(1);
}
int i=0;
while(!infile.eof()) //从文件读入结构体知道读完为止,顺序存放在结构体数组stu中。
{infile>>stu[i].num;
infile>>stu[i].name;
infile>>stu[i].sex;
infile>>stu[i].age;
infile>>stu[i].Chinese;
infile>>stu[i].English;
infile>>stu[i].Match;
infile>>stu[i].C;
infile>>stu[i].Physics;
infile>>stu[i].average;
i++;
}
int find_num;
cout<<"请输入所要查询学生的学号:"<<endl;
cin>>find_num;
for(int b=0;b<i-1;b++)
if(find_num==stu[b].num) //如果输入数字于以结构体元素stu[b].num相等,输出相应的结构体。
{cout<<"所要查询学生的信息如下:"<<endl;
cout<<"|学号 |姓名 |性别 |年龄 |语文 |英语 |数学 |C语言 |物理 |平均分"<<endl;
cout<<setiosflags(ios::left);
cout<<"|"<<setw(7)<<stu[b].num<<"|"<<setw(7)<<stu[b].name<<"|"<<setw(7)<<stu[b].sex
<<"|"<<setw(7)<<stu[b].age<<"|"<<setw(7)<<stu[b].Chinese<<"|"<<setw(7)
<<stu[b].English<<"|"<<setw(7)<<stu[b].Match<<"|"<<setw(8)<<stu[b].C<<"|"<<setw(7)
<<stu[b].Physics<<"|"<<stu[b].average;
cout<<endl;
break;
}
if(b==i-1) cout<<"学号不存在"<<endl; //如果循环次数等于读取时的循环次数减一时,显示"学号不存在"。
cout<<"是否继续查询?Y/N"<<endl;
getchar();
if(getchar()==78) main();
else DemandStudent();
}
/////////////////////////////////////修改数据////////////////////////////////////////////////
void ModifStudent()
{
Student stu[100];
ifstream infile("d:\\data.txt",ios::in); //定义一个文件流对象,以输入方式打开磁盘文件"d:\\data.txt"。
if(! infile)
{cerr<<"open error!"<<endl;
exit(1);
}
int i=0;
while(!infile.eof()) //从文件读入结构体知道读完为止,顺序存放在结构体数组stu中。
{infile>>stu[i].num;
infile>>stu[i].name;
infile>>stu[i].sex;
infile>>stu[i].age;
infile>>stu[i].Chinese;
infile>>stu[i].English;
infile>>stu[i].Match;
infile>>stu[i].C;
infile>>stu[i].Physics;
infile>>stu[i].average;
i++;
}
int find_num;
cout<<"请输入所要修改学生的学号:"<<endl;
cin>>find_num;
for(int b=0;b<i-1;b++)
if(find_num==stu[b].num)
{cout<<"所要修改学生的信息如下:"<<endl;
cout<<"|学号 |姓名 |性别 |年龄 |语文 |英语 |数学 |C语言 |物理 |平均分"<<endl;
cout<<setiosflags(ios::left);
cout<<"|"<<setw(7)<<stu[b].num<<"|"<<setw(7)<<stu[b].name<<"|"<<setw(7)<<stu[b].sex
<<"|"<<setw(7)<<stu[b].age<<"|"<<setw(7)<<stu[b].Chinese<<"|"<<setw(7)
<<stu[b].English<<"|"<<setw(7)<<stu[b].Match<<"|"<<setw(8)<<stu[b].C<<"|"<<setw(7)
<<stu[b].Physics<<"|"<<stu[b].average;
cout<<endl;
cout<<"请选择所要修改的科目:1、语文 2、英语 3、数学 4、C语言 5、物理"<<endl;
int flag;
int rep_score;
conm: cin>>flag;
switch(flag) //选择所要修改的科目。
{case 1:cout<<"请所要修改成绩:";cin>>rep_score;stu[b].Chinese=rep_score;break;
case 2:cout<<"请所要修改成绩:";cin>>rep_score;stu[b].English=rep_score;break;
case 3:cout<<"请所要修改成绩:";cin>>rep_score;stu[b].Match=rep_score;break;
case 4:cout<<"请所要修改成绩:";cin>>rep_score;stu[b].C=rep_score;break;
case 5:cout<<"请所要修改成绩:";cin>>rep_score;stu[b].Physics=rep_score;break;
default:cout<<"输入错误,请重新选择:1、语文 2、英语 3、数学 4、C语言 5、物理"
<<endl;goto conm; //如果输入的非1、2、3、4、5返回重新选择。
}
cout<<"修改成功!"<<endl;
stu[b].average=(stu[b].Chinese+stu[b].English+stu[b].Match+stu[b].C+
stu[b].Physics)/5;
cout<<"|学号 |姓名 |性别 |年龄 |语文 |英语 |数学 |C语言 |物理 |平均分"<<endl;
cout<<setiosflags(ios::left); //显示修改后的学生信息。
cout<<"|"<<setw(7)<<stu[b].num<<"|"<<setw(7)<<stu[b].name<<"|"<<setw(7)<<stu[b].sex
<<"|"<<setw(7)<<stu[b].age<<"|"<<setw(7)<<stu[b].Chinese<<"|"<<setw(7)
<<stu[b].English<<"|"<<setw(7)<<stu[b].Match<<"|"<<setw(8)<<stu[b].C<<"|"<<setw(7)
<<stu[b].Physics<<"|"<<stu[b].average;
cout<<endl;
ofstream outfile("d:\\data.txt",ios::out); //把修改后的全部数据对文件"d:\\data.txt"原有数据进行覆盖。
if(! outfile)
{cout<<"open error!"<<endl;
exit(1);
}
for(int a=0;a<i-1;a++)
{outfile<<setw(7)<<stu[a].num<<setw(7)<<stu[a].name<<setw(7)<<stu[a].sex<<setw(7)
<<stu[a].age<<setw(7)<<stu[a].Chinese<<setw(7)<<stu[a].English<<setw(7)
<<stu[a].Match<<setw(7)<<stu[a].C<<setw(7)<<stu[a].Physics<<setw(4)
<<stu[a].average;
outfile<<endl;
}
break;
}
if(b==i-1) cout<<"学号不存在"<<endl;
cout<<"是否继续?Y/N"<<endl;
getchar();
if(getchar()==78) main();
else ModifStudent();
}
////////////////////////////////////////////统计数据///////////////////////////////////////////////
void StatStudent()
{
void FailStat();
void AverageStat();
void NumStat();
cout<<"|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^|"<<endl;
cout<<"|1、按学号升序显示全部学生的信息。 |"<<endl;
cout<<"|2、按平均分降序显示全部学生的信息,并保存到磁盘文件\".txt\"。 |"<<endl;
cout<<"|3、统计不及格课程学生的信息。 |"<<endl;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -