📄 大学生成绩管理系统(c++版).cpp
字号:
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <fstream>
using namespace std;
string IDtofind;
bool menu1()
{
cout<<" Welcome to the Student Score MIS"<<endl;
cout<<"************************ Main Menu* *************************"<<endl;
cout<<"* 1. Add Records 2. Require Records *"<<endl;
cout<<"* 3. Delete Records 4. Modify Records *"<<endl;
cout<<"* 5. Sort Records 6. Save Records *"<<endl;
cout<<"* 7. Load Records 8. Exit *"<<endl;
cout<<"*************************************************************"<<endl;
return true;
}
bool menu2()
{
cout<<"*********************Require Records ************************"<<endl;
cout<<"* 1 Require Certain Record by Num *"<<endl; //9
cout<<"* 2 Require All the Records *"<<endl; //10
cout<<"* 3 Back to MainMenu *"<<endl;
cout<<"*************************************************************"<<endl; //11
return true;
}
bool menu3()
{
cout<<"*********************Sort Records ****************************"<<endl;
cout<<"* 1 Sort Records by Num *"<<endl; //12
cout<<"* 2 Sort Records by name *"<<endl; //13
cout<<"* 3 Sort Records by Average Score *"<<endl; //14
cout<<"* 4 Back to MainMenu *"<<endl; //15
cout<<"**************************************************************"<<endl;
return true;
}
struct student
{
string ID;
string name;
double yuwen;
double shuxue;
double yingyu;
double average;
};
bool sID(student a,student b)
{
return a.ID>b.ID;
}
bool sName(student a,student b)
{
return a.name<b.name;
}
bool sAver(student a,student b)
{
return a.average>b.average;
}
class body
{
vector<student> v;
public:
bool add();
bool del(string ID);
vector<student>::iterator findbyID(string ID);
bool traverse();
bool modify(string ID);
bool sortbyID();
bool sortbyName();
bool sortbyAverage();
bool load(string filename);
bool save(string filename);
};
bool body::add()
{
student temp;
cout<<" add a record of student"<<endl<<"please input the name of student:"<<endl;
cin>>temp.name;
cout<<"please input the num:"<<endl;
cin>>temp.ID;
cout<<"please input the score of yuwen:"<<endl;
cin>>temp.yuwen;
cout<<"please input the score of shuxue:"<<endl;
cin>>temp.shuxue;
cout<<"please input the score of yingyu:"<<endl;
cin>>temp.yingyu;
temp.average=(temp.yingyu+temp.shuxue+temp.yuwen)/3;
v.push_back(temp);
return true;
}
bool body::del(string ID)
{
vector<student>::iterator it;
it=findbyID(ID);
if(it==v.end())
{
cout<<"not found" <<endl;
return false;
}
else
{
v.erase(it);
return true;
}
}
bool findID(student s)
{
return s.ID==IDtofind;
}
vector<student>::iterator body::findbyID(string ID)
{
IDtofind=ID;
vector<student>::iterator temp;
temp=find_if(v.begin(),v.end(),findID);
student s;
if(temp==v.end())
{
cout<<"not found"<<endl;
return v.end();
}
else
{
s=(*temp);
cout<<s.ID<<" "<<s.name<<" "<<s.yuwen<<" "<<s.shuxue<<" "<<s.yingyu<<" "<<s.average<<endl;
return temp;
}
}
bool body::traverse()
{
int i;
cout<<"Num\tName\tyuwen\tshuxue\tyingyu\taverage"<<endl;
for(i=0;i<v.size();i++)
cout<<v.at(i).ID<<"\t"<<v.at(i).name<<"\t"<<v.at(i).yuwen<<"\t"<<v.at(i).shuxue<<"\t"<<v.at(i).yingyu<<"\t"<<v.at(i).average<<endl;
return true;
}
bool body::modify(string ID)
{
vector<student>::iterator it;
student temp;
it=findbyID(ID);
if(it==v.end())
{
cout<<"not found"<<endl;
return false;
}
else
{
temp=(*it);
cout<<" modify a record of student"<<endl<<"please input the new name of student:"<<endl;
cin>>temp.name;
cout<<"please input the new num:"<<endl;
cin>>temp.ID;
cout<<"please input the new score of yuwen:"<<endl;
cin>>temp.yuwen;
cout<<"please input the new score of shuxue:"<<endl;
cin>>temp.shuxue;
cout<<"please input the new score of yingyu:"<<endl;
cin>>temp.yingyu;
temp.average=(temp.yingyu+temp.shuxue+temp.yuwen)/3;
v.at(it-v.begin())=temp;
return true;
}
}
bool body::sortbyID()
{
sort(v.begin(),v.end(),sID);
return true;
}
bool body::sortbyName()
{
sort(v.begin(),v.end(),sName);
return true;
}
bool body::sortbyAverage()
{
sort(v.begin(),v.end(),sAver);
return true;
}
bool body::load(string filename)
{
fstream fp;
fp.open(filename.c_str(),ios_base::in);
int i,sum;
student temp;
fp>>sum;
for(i=0;i<sum;i++)
{
fp>>temp.ID>>temp.name>>temp.yuwen>>temp.shuxue>>temp.yingyu;
temp.average=(temp.yingyu+temp.shuxue+temp.yuwen)/3;
v.push_back(temp);
}
fp.close();
return true;
}
bool body::save(string filename)
{
fstream fp;
fp.open(filename.c_str(),ios_base::out);
int i;
student temp;
fp<<v.size()<<endl;
for(i=0;i<v.size();i++)
{
temp=v.at(i);
fp<<temp.ID<<" "<<temp.name<<" "<<temp.yuwen<<" "<<temp.shuxue<<" "<<temp.yingyu<<endl;
}
fp.close();
return true;
}
int main()
{
body b;
int input,add=0;
string s;
/*s="ab.txt";
FILE *fp;
fp=fopen(s.c_str(),"w");
fprintf(fp,"%d",90);*/
menu1();
cin>>input;
while(input!=8)
{
switch(input)
{
case 1:
b.add();
menu1();
break;
case 2:
add=8;
menu2();
break;
case 3:
cout<<"please input the Num of the student to delete:"<<endl;
cin>>s;
b.del(s);
menu1();
break;
case 4:
cout<<"please input the Num of the student to modify:"<<endl;
cin>>s;
b.modify(s);
menu1();
break;
case 5:
add=11;
menu3();
break;
case 6:
cout<<"please input the filename to save:"<<endl;
cin>>s;
b.save(s);
menu1();
break;
case 7:
cout<<"please input the filename to load:"<<endl;
cin>>s;
b.load(s);
menu1();
break;
case 9:
cout<<"please input the Num of the student to require:"<<endl;
cin>>s;
b.findbyID(s);
menu2();
break;
case 10:
b.traverse();
menu2();
break;
case 11:
add=0;
menu1();
break;
case 12:
b.sortbyID();
cout<<"OK"<<endl;
menu3();
break;
case 13:
b.sortbyName();
cout<<"OK"<<endl;
menu3();
break;
case 14:
b.sortbyAverage();
cout<<"OK"<<endl;
menu3();
break;
case 15:
add=0;
menu1();
break;
default:
break;
}
cin>>input;
input+=add;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -