#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int maxn=1023;
string SName[maxn];//
记录学生姓名
int SID[maxn];//
记录学生学号
int SEvent[maxn]; //
记录学生报名项目编号
int TotalN=0;//
记录总报名学生数
int Sgender[maxn];//
记录学生性别
int TotalItems=0;//
记录总项目数
int ItemLimit[maxn][2];//
记录每个项目需要学生
string ItemName[maxn];//
记录每个项目的名称
string Password;
string NewPassword;
bool Cracked=false;
string Transform_Code(string P)//
密码的加密解密
{
int tt=P.length();
int i;
string ans=P;
for (i=0;i<=tt-1;i++) ans[tt-1-i]=P[i];
return ans;
}
void Initialize()//
读取数据文件,初始化程序
{
ifstream infile,indata;
infile.open("Items.txt",ios::in);
infile>>TotalItems;
cout<<"
本次运动会共有
"<<TotalItems<<"
个项目。分别是:
"<<endl;
int i;
for (i=1;i<=TotalItems;i++)
{
infile>>ItemName[i]>>ItemLimit[i][0]>>ItemLimit[i][1];
}
infile.close();
indata.open("Data.txt",ios::in);
indata>>TotalN;
for (i=1;i<=TotalN;i++)
{
indata>>SID[i]>>SName[i]>>Sgender[i]>>SEvent[i];
ItemLimit[SEvent[i]][Sgender[i]]--;
}
string TmpP;
indata>>TmpP;
Password=Transform_Code(TmpP);
NewPassword=Password;
indata.close();
for (i=1;i<=TotalItems;i++)
{
cout<<ItemName[i]<<"
,余
"<<ItemLimit[i][0]<<"
女;
";
cout<<"
余
"<<ItemLimit[i][1]<<"
男
"<<endl;
}
cout<<endl;
}
string getGender(int g)
{
if (g==0) return "
女
";
else return "
男
";
}
void PrintEntry(int boolean,bool NotGenderIn)
{
//
输出所有记录到屏幕或数据文件,参数决定输出到屏幕或是数据文件
int i;
if (boolean==0)
{
if (NotGenderIn)
{
for(i=1;i<=TotalN;i++)
{
cout<<SID[i]<<" "<<SName[i]<<" ";
cout<<getGender(Sgender[i])<<" "<<ItemName[SEvent[i]]<<endl;
}
}
else
{
for(i=1;i<=TotalN;i++)
if (Sgender[i]==0)
{
cout<<SID[i]<<" "<<SName[i];
cout<<" "<<getGender(Sgender[i])<<" "<<ItemName[SEvent[i]]<<endl;
}
for(i=1;i<=TotalN;i++)
if (Sgender[i]==1)
{
cout<<SID[i]<<" "<<SName[i];
cout<<" "<<getGender(Sgender[i])<<" "<<ItemName[SEvent[i]]<<endl;
}
}
cout<<endl;
}
else
{
ofstream outfile;
outfile.open("Data.txt",ios::out);
outfile<<TotalN<<endl;
for(i=1;i<=TotalN;i++)
{
outfile<<SID[i]<<" "<<SName[i]<<" "<<Sgender[i]<<" "<<SEvent[i]<<endl;
}
outfile<<NewPassword<<endl;
outfile.close();
}
}
void ItemsStatistics()//
输出报名余额
{
int i;
for (i=1;i<=TotalItems;i++)
{
cout<<i<<"."<<ItemName[i]<<"
,余
"<<ItemLimit[i][0]<<"
女;
";
cout<<"
余
"<<ItemLimit[i][1]<<"
男
"<<endl;
}
cout<<endl;
}
void AddEntry()//
添加或修改已有记录
{
int i,temp,rSID;
bool NotExisted=true;
cout<<"
请输入学号:
";
cin>>temp;
for (i=1;i<=TotalN;i++) if (SID[i]==temp)
{
NotExisted=false;//
先查询是否已经存在要添加的学号
rSID=i;
break;
}
if (NotExisted)
{
TotalN++;
SID[TotalN]=temp;
cout<<"
请输入姓名:
";
cin>>SName[TotalN];
int gt=2;
do
{
cout<<"
请输入性别编号(
0.
女;
1.
男)
:
";
cin>>gt;
if ((gt!=0)&&(gt!=1))
cout<<"
性别编号输入错误,请重选。
"<<endl<<endl;
}
while ((gt!=0)&&(gt!=1));//
防止用户输入
0
或
1
以外的数字
Sgender[TotalN]=gt;
cout<<endl;
ItemsStatistics();
do
{
cout<<"
请选择该学生报名项目:
";
cin>>gt;
if (ItemLimit[gt][Sgender[TotalN]]<=0)
cout<<"
该项目已经饱和,请重选。
"<<endl<<endl;
}
while (ItemLimit[gt][Sgender[TotalN]]<=0);//
防止项目饱和
SEvent[TotalN]=gt;
ItemLimit[SEvent[TotalN]][Sgender[TotalN]]--;
cout<<"
添加完成。
"<<endl<<endl;
}
else
{
ItemLimit[SEvent[rSID]][Sgender[rSID]]++;
ItemsStatistics();
cout<<"
请选择该学生修改后的报名项目:
";
cin>>SEvent[rSID];
ItemLimit[SEvent[rSID]][Sgender[rSID]]--;
cout<<"
修改完成。
"<<endl<<endl;
}
}
void SwapS(int an,int bn)//
交换学生报名信息
{
string Tstr;
int Tsid;
Tsid=SID[an];
SID[an]=SID[bn];
SID[bn]=Tsid;
Tstr=SName[an];
SName[an]=SName[bn];
SName[bn]=Tstr;
Tsid=SEvent[an];
SEvent[an]=SEvent[bn];
SEvent[bn]=Tsid;
Tsid=Sgender[an];
Sgender[an]=Sgender[bn];
Sgender[bn]=Tsid;
}
void qs(int *d,int l,int r)//
快速排序算法,按照学号排序
{
int i,j,mid;
i=l;
j=r;
mid=d[(l+r)/2];
do
{
while (d[i]<mid) i++;
while (mid<d[j]) j--;
if (i<=j)
{
SwapS(i,j);
i++;
j--;
}
}
while (i<=j);
if (l<j) qs(d,l,j);
if (i<r) qs(d,i,r);
}
int Inquiry(int num)//
按照学号查询
{
int i;
for(i=1;i<=TotalN;i++)
{
if (SID[i]==num)
{
cout<<SID[i]<<" "<<SName[i]<<" "<<ItemName[SEvent[i]]<<endl<<endl;
return 0;
}
}
cout<<"
没有找到:学号为
"<<num<<"
的记录
"<<endl;
cout<<endl;
}
int SInquiry(string pp)//
按照姓名查询
{
int p;
bool NotFound=true;
for(p=1;p<=TotalN;p++)
{
if (SName[p]==pp)
{
cout<<SID[p]<<" "<<SName[p]<<" "<<ItemName[SEvent[p]]<<endl;
NotFound=false;
}
}
if (NotFound) cout<<"
没有找到:姓名为
"<<pp<<"
的记录
"<<endl;
cout<<endl;
}
int DeleteItem(int StuID)//
删除一条记录
{
int i;
for(i=1;i<=TotalN;i++)
{
if (SID[i]==StuID)
{
SID[i]=SID[TotalN];
SName[i]=SName[TotalN];
SEvent[i]=SEvent[TotalN];
TotalN--;
cout<<"
删除完成。
"<<endl<<endl;
return 0;
}
}
cout<<"
没有找到:学号为
"<<StuID<<"
的记录。
"<<endl<<endl;
}
void InitializeUI()//
初始化用户界面
{
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<<"9.
修改密码
"<<endl;
cout<<"c.
清除屏幕
"<<endl;
cout<<"s.
保存但不退出
"<<endl;
cout<<"0.
保存并退出
"<<endl;
cout<<endl;
}
bool ValidPswrd()//
要求用户输入密码并验证
{
if (Cracked) return true;//
如果已经输入过则不再询问
bool ValidPswrd;
string inputStr;
cout<<"
请输入密码:
";
cin>>inputStr;
if (Password==inputStr)
{
Cracked=true;
return true;
}
else return false;
}
int main()
{
system("title
运动会管理系统
DOS
版——
41005
班
C++
课程设计
");//
更改程序标题
system("color 0B");//
更改字体颜色
Initialize();
InitializeUI();
char X;
while (X!='E')
{
string TmpStr;
do
{
cout<<"
请选择一项操作后按
[Enter]
:
";
cin>>TmpStr;
}
while ((TmpStr.length()>1)||(TmpStr.length()==0));
cout<<endl;
X=TmpStr[0];
switch (X)
{
case '1':
{
PrintEntry(0,false);
break;
}
case '2':
{
if (ValidPswrd())
{
AddEntry();
}
else cout<<"
密码错误。
"<<endl<<endl;
break;
}
case '3':
{
if (ValidPswrd())
{
int Un;cout<<"
请输入要删除的学号:
";
cin>>Un;
DeleteItem(Un);
}
else cout<<"
密码错误。
"<<endl<<endl;
break;
}
case '4':
{
string N;
cout<<"
请输入要查询的姓名:
";
cin>>N;
SInquiry(N);
break;
}
case '5':
{
int Un;
cout<<"
请输入要查询的学号:
";
cin>>Un;
Inquiry(Un);
break;
}
case '6':
{
ItemsStatistics();
break;
}
case '7':
{
qs(SID,1,TotalN);
PrintEntry(0,true);
break;
}
case '8':
{
qs(SID,1,TotalN);
PrintEntry(0,false);
break;
}
case '9':
{
string Tp;
cout<<"
请输入您的旧密码:
";
cin>>Tp;
if (Tp==Password)
{
string ttp1,ttp2;
do
{
ttp1="ttp1", ttp2="ttp2";
cout<<"
请输入一个新密码:
";
cin>>ttp1;
cout<<"
请确认您的输入:
";
cin>>ttp2;
if (ttp1!=ttp2)
cout<<"
两次密码不一致,请重新输入。
"<<endl<<endl;
}
while (ttp1!=ttp2);
NewPassword=Transform_Code(ttp1);
Password=ttp2;//
此操作很重要,使新密码立即生效
cout<<"
修改成功。
"<<endl<<endl;
}
else cout<<"
密码错误。
"<<endl<<endl;
break;
}
case '0':
{
PrintEntry(1,false);
cout<<"
数据已经写入到:
Data.txt"<<endl<<endl;
return 0;
}
case 's':
{
PrintEntry(1,false);
cout<<"
数据已经写入到:
Data.txt"<<endl<<endl;
break;
}
case 'c':
{
system("cls");
InitializeUI();
break;
}
default:break;
}
}
return 0;
}