#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;
int M=150;
int i;
class Sport
{
public:
void setschools()
{
cout<<"请输入学院名称:";
cin>>place;
}
string getschools()
{
return place;
}
private:
string place;
};
class Schools
{
public:
void setnum()
{
cout<<"请输入学号:";
cin>>num;
}
void setname()
{
cout<<"请输入名字:";
cin>>name;
}
void setitem1()
{
cout<<"请输入项目1:";
cin>>item1;
}
void setitem2()
{
cout<<"请输入项目2:";
cin>>item2;
}
void setitem3()
{
cout<<"请输入项目3:";
cin>>item3;
}
void setscores1()
{
cout<<"请输入得分:";
cin>>scores1;
}
void setscores2()
{
cout<<"请输入得分:";
cin>>scores2;
}
void setscores3()
{
cout<<"请输入得分:";
cin>>scores3;
}
void theme()
{
cout<<setw(7)<<"学号"<<setw(7)<<"姓名"<<setw(7)<<"项目1"<<setw(7)<<"分数"<<setw(7)<<"项目2"<<setw(7)<<"分数"<<setw(7)<<"项目3"<<setw(7)<<"分数"<<endl;
}
void output()
{
cout<<setw(7)<<num<<setw(7)<<name<<setw(7)<<item1<<setw(7)<<scores1<<setw(7)<<item2<<setw(7)<<scores2<<setw(7)<<item3<<setw(7)<<scores3<<endl;
}
string getname()
{
return name;
}
long getnum()
{
return num;
}
string getitem1()
{
return item1;
}
string getitem2()
{
return item2;
}
string getitem3()
{
return item3;
}
int getscores1()
{
return scores1;
}
int getscores2()
{
return scores2;
}
int getscores3()
{
return scores3;
}
int summ()
{
return scores1+scores2+scores3;
}
private:
string name;
long num;
string item1,item2,item3;//不要用string
int scores1,scores2,scores3;
};
//输入男子与女子比赛的成绩
void Students(Schools s[],Sport p[],int n)
{
int i=0;
int N=0;
string place;
string str;
ofstream outfile("sports.txt",ios::app|ios::binary);
if(!outfile)
{
cout<<"系统打开失败,请重试或维修"<<endl;
}
else
{
p[i].setschools();
outfile<<p[i].getschools()<<endl;
cout<<"======输入学生记录======"<<endl;
if(n==0)
{
while(true)
{
s[i].setnum();
for(int j=0;j<n;j++)
{
if(s[i].getnum()==s[j].getnum())
{
cout<<"此人已存在,请重新输入!\n";
s[i].setnum();
}
}
s[i].setname();
s[i].setitem1();
s[i].setscores1();
if(s[i].getscores1()>10&&s[i].getscores1()<0)
{
cout<<"得分出错!!"<<endl;
s[i].setscores1();
}
s[i].setitem2();
s[i].setscores2();
if(s[i].getscores2()>10&&s[i].getscores2()<0)
{
cout<<"得分出错!!"<<endl;
s[i].setscores2();
}
s[i].setitem3();
s[i].setscores3();
if(s[i].getscores3()>10&&s[i].getscores3()<0)
{
cout<<"得分出错!!"<<endl;
s[i].setscores3();
}
outfile<<s[i].getnum()<<" "<<s[i].getname()<<" "<<s[i].getitem1()<<" "<<s[i].getscores1()<<" "<<s[i].getitem2()<<" "<<s[i].getscores2()<<" "<<s[i].getitem3()<<" "<<s[i].getscores3()<<endl;
i++;
i++;
i=n;
cout<<"继续输入成绩请按 y :";
cin>>str;
if(i==150)
{
cout<<"人数达到上限!!"<<endl;
break;
}
if(str=="y")
{
continue;
}
else
{
break;
}
}
}
else//插入学生信息
{
if(n==150)
{
cout<<"人数已达到150个,已达到上限,无法插入!"<<endl;
}
else
{
i=n;
while(true)
{
s[i].setnum();
for(int j=0;j<n;j++)
{
if(s[i].getnum()==s[j].getnum())
{
cout<<"此人已存在,请重新输入!\n";
s[i].setnum();
}
}
s[i].setname();
s[i].setitem1();
s[i].setscores1();
if(s[i].getscores1()>10&&s[i].getscores1()<0)
{
cout<<"得分出错!!"<<endl;
s[i].setscores1();
}
s[i].setitem2();
s[i].setscores2();
if(s[i].getscores2()>10&&s[i].getscores2()<0)
{
cout<<"得分出错!!"<<endl;
s[i].setscores2();
}
s[i].setitem3();
s[i].setscores3();
if(s[i].getscores3()>10&&s[i].getscores3()<0)
{
cout<<"得分出错!!"<<endl;
s[i].setscores3();
}
outfile<<s[i].getnum()<<" "<<s[i].getname()<<" "<<s[i].getitem1()<<" "<<s[i].getscores1()<<" "<<s[i].getitem2()<<" "<<s[i].getscores2()<<" "<<s[i].getitem3()<<" "<<s[i].getscores3()<<endl;
i++;
i++;
i=n;
cout<<"继续输入成绩请按 y :";
cin>>str;
if(i==150)
{
cout<<"人数达到上限!!"<<endl;
break;
}
if(str=="y")
{
continue;
}
else
{
break;
}
}
}
}
outfile.write((char*)&s[i],sizeof(s[i]));
}
outfile.write((char*)&p[i],sizeof(p[i]));
outfile.close();
system("pause");
}
//输出显示成绩的信息
void Output(Schools s[],Sport p[],int n)
{
int i;
string place;
ifstream infile;
infile.open("sports.txt",ios::in|ios::binary);
if(!infile.is_open())
{
cout<<"系统打开失败!请重新启动*"<<endl;
}
s[0].theme();
for(int i=0;i<M;i++)
{
infile.read((char*)&s[i],sizeof(s[i]));
s[i].output();
}
infile.close();
}
//该学生总分排序及学院的总分
void Sum(Schools s[],int n)
{
int i,j,k,sum;
ifstream infile("sports.txt",ios::in|ios::binary);
if(!infile)
{
cout<<"系统打开失败!请重新启动*"<<endl;
}
for(i=0;i<M;i++)
{
infile.read((char*)&s[i],sizeof(s[i]));
}
if(n!=0)
{
Schools p;
for(i=0;i<n;i++)
{
sum+=(s[i].getscores1()+s[i].getscores2()+s[i].getscores3());
for(j=0;j<n-i-1;j++)
{
if(s[j].summ()<s[j+1].summ())
{
p=s[j];
s[j]=s[j+1];
s[j+1]=p;
}
}
}
cout<<"学院总分:"<<sum<<endl;
s[0].theme();
for(k=0;k<n;k++)
{
s[k].output();
}
cout<<endl;
}
else
{
cout<<"暂无学生项目分数,请选择1=输入学生项目分数"<<endl;
}
infile.close();
system("pause");
}
//查找信息
/* void Find(Schools s[],int n)
{
long num;
int i;
string t;
ifstream infile("sports.txt",ios::in);
if(!infile)
{
cout<<"系统打开失败!请重新启动*"<<endl;
}
for(i=0;i<M;i++)
{
infile.read((char*)&s[i],sizeof(s[i]));
}
while(true)
{
cout<<"请输入您要查找的学号:";
cin>>num;
int first=0;
for(int i=0;i<n;i++)
{
if(s[i].getnum()==num)
{
first++;
}
}
if(first==0)
{
cout<<"没有该学号学生的项目成绩!!"<<endl;
}
else
{
s[0].theme();
for(int i;i<n;i++)
{
if(s[i].getnum()==num)
{
s[i].output();
}
}
cout<<"继续查找请按y:";
cin>>t;
if(t=="y")
{
continue;
}
else
{
break;
}
}
cout<<"输入学号有误,请重新输入!!"<<endl;
cout<<"继续查找请按y:";
cin>>t;
if(t=="y")
{
continue;
}
else
{
break;
}
}
infile.close();
system("pause");
}
//删除学生的项目成绩
void Delete(Schools s[],int n)
{
int num;
string t;
ifstream infile("sports.txt",ios::app);
if(!infile)
{
cout<<"系统打开失败!请重试*"<<endl;
}
ofstream outfile("sportscoppy.txt",ios::out);
if(!outfile)
{
cout<<"系统打开失败!请重试*"<<endl;
}
while(true)
{
cout<<"请输入要删除的学生的学号:";
cin>>num;
int first=0;
Schools a[100];
for(int i=0;i<n;i++)
{
if(s[i].getnum()!=num)
{
a[first]=s[i];
first++;
}
}
if(first==num)
{
cout<<"该学生没有参加比赛!!"<<endl;
exit(0);
}
else
{
num=first;
for(int j=0;j<n;j++)
{
s[j]=s[j];
}
cout<<num<<" 学生删除成功!"<<endl;
}
cout<<"继续删除其他学生请按y:";
cin>>t;
if(t=="y")
{
continue;
}
else
{
break;
}
}
infile.close();
outfile.close();
remove("sports.txt");
remove("sportscoppy.txt");
}
*/
void Menue()
{
cout<<"\t======================================================================================"<<endl;
cout<<"\t|| 1、录入学生比赛各项目的成绩 2、输出学生比赛各项目的成绩 ||"<<endl;
cout<<"\t|| 3、查找学生比赛各项目的成绩 4、删除学生比赛各项目的成绩 ||"<<endl;
cout<<"\t|| 5、输出该院比赛成绩总和及学生排名 6、输出各学院的总和 ||"<<endl;
cout<<"\t|| 7、谢谢各位的使用P.^-^.V,everything is possible!期待您的再次使用! ||"<<endl;
cout<<"\t======================================================================================"<<endl;
}
void Selet()
{
int N=0;
int i;
Schools s[M];
Sport p[M];
int o;
cout<<"请选择功能:";
cin>>o;
switch (o)
{
case 1:
Students(s,p,N);
Menue();
Selet();
break;
case 2:
Output(s,p,N);
Menue();
Selet();
break;
case 3:
// Find(s,N);
Menue();
Selet();
break;
case 4:
// Delete(s,N);
Menue();
Selet();
break;
case 5:
Sum(s,N);
Menue();
Selet();
break;
case 6:
// Allsum(s,p,N);
Selet();
break;
case 7:
cout<<"\t 谢谢各位的使用P.^-^.V,everything is possible!期待您的再次使用!"<<endl;
break;
}
}
int main()
{
cout<<"\t\t 欢迎使用What版运动会计分系统!!"<<endl;
Menue();
Selet();
return 0;
}
模板参考:根据denglishang学生管理系统按照自我需求改写。