#include "head.h"
#include <iostream>
#include <cctype>
#include <iomanip>
#include <cstdlib>
#include <cstring>
using namespace std;
void new_line()
{
char symbol;
do{
cin.get(symbol);
}while(symbol != '\n');
}
void teams::f_input(ifstream& if_stream)
{
if_stream>>name>>rank>>comp_session>>win_num>>equally_num>>negative_num>>goals_num>>losses_num;
}
void teams::input()
{
char c;
bool blag;
cout<<"**本场比赛的结果(win、equally、negative):";
do{
blag=0;
cin>>c;
new_line();
c = tolower(c);
switch(c)
{
case 'w':
win_num++; break;
case 'e':
equally_num++; break;
case 'n':
negative_num++; break;
default :
blag=1;
cout<<"**输入错误\n**请重新输入:";
break;
}
}while(blag);
comp_session++;
cout<<"**进球数:"; cin>>goals_num;
cout<<"**失球数:"; cin>>losses_num;
refurbish();
}
char *teams::get_name()
{
return name;
}
void teams::get_rank(int r)
{
rank = r;
}
void teams::refurbish()
{
goal_dif_num = goals_num-losses_num;
integral = win_num*3+equally_num;
// s_rate = win_num*1.0/comp_session;
}
void teams::out_to_screen()
{
cout.setf(ios::left);
cout<<setw(16)<<name
<<setw(5)<<rank
<<setw(9)<<comp_session
<<setw(9)<<win_num
<<setw(9)<<equally_num
<<setw(9)<<negative_num
<<setw(7)<<goals_num
<<setw(7)<<losses_num
<<setw(5)<<integral
<<endl;
cout.setf(ios::right);
}
void teams::f_output(ofstream& of_stream)
{
of_stream<<'\n'<<name<<" "
<<rank<<" "<<comp_session<<" "
<<win_num<<" "<<equally_num
<<" "<<negative_num<<" "
<<goals_num<<" "
<<losses_num;
}
bool compare(const teams& a,const teams& b)
{
if(a.integral==b.integral)
{
if(a.goal_dif_num==b.goal_dif_num)
{
if(a.goals_num==b.goals_num)
{
cout<<"由于积分、净胜球数、进球数都相同,故随机排名……"<<endl;
int r;
r = rand();
r = r%2;
return r;
}
else if(a.goals_num>b.goals_num)
return 1;
else
return 0;
}
else if(a.goal_dif_num>b.goal_dif_num)
return 1;
else
return 0;
}
else if(a.goal_dif_num>b.goal_dif_num)
return 1;
else
return 0;
}
safe_conduct::safe_conduct(const char file[])
{
ifstream f_in;
f_in.open(file);
if(f_in.fail())
{
cout<<"文件打开失败!"<<endl;
exit(1);
}
f_in>>user_name>>password;
f_in.close();
}
void safe_conduct::rejigger()
{
char new_uname[11],new_pw[11],ok_new_pw[11];
cout<<"新用户名:";
cin>>new_uname;
bool blag;
do{
blag=0;
cout<<"新密码:";
cin>>new_pw;
for(char *i=new_pw;*i!='\0';i++) //检查密码是否合法
{
if(*i<='!'||*i>='~')
{
blag=1;
cout<<"密码不符合规范!!请重新输入!!"<<endl;
break;
}
}
cout<<"确认密码:";
cin>>ok_new_pw;
if(strcmp(new_pw,ok_new_pw)!=0)
{
blag=1;
cout<<"密码不同!"<<endl;
break;
}
/*for(int j=0;new_pw[j]!='\0'&&ok_new_pw[j]!='\0';j++)
{
if(new_pw[j]!=ok_new_pw[j])
{
blag=1;
cout<<"密码不同!"<<endl;
break;
}
} */
}while(blag);
strcpy(user_name,new_uname);
strcpy(password,new_pw);
}
void safe_conduct::save_rejigger(const char file[])
{
fstream f_o;
f_o.open(file,ios::in|ios::out);
if(f_o.fail())
{
cout<<"文件打开错误!"<<endl;
exit(1);
}
f_o.setf(ios::left);
f_o<<user_name<<" "<<password;
cout<<"用户名:"<<user_name<<" "<<"密码:"<<password<<endl;
f_o.close();
}
void safe_conduct::validate()
{
int input_num=0;
char un[11],pw[11];
cout<<"请输入用户名和密码(默认用户名:admin 密码:admin):"<<endl;
bool blag; //标志错误
do{
blag=0;
cout<<"用户名:";
cin>>un;
cout<<"密码:";
cin>>pw;
if(strcmp(un,user_name)!=0)
{
blag=1;
cout<<"用户名输入错误!"<<endl;
}
/*for(int i=0;user_name[i]!='\0';i++) //验证用户名是否正确
{
if(user_name[i]!=un[i])
{
blag=1;
cout<<"用户名输入错误!"<<endl;
break;
}
}*/
if(!blag)
{
for(int i=0;password[i]!='\0';i++) //验证密码是否正确
{
if(password[i]!=pw[i])
{
blag=1;
cout<<"密码输入错误!"<<endl;
}
}
}
if(blag)
{
input_num++;
switch(input_num)
{
case 1: cout<<"您还可以输入两次!"<<endl; break;
case 2: cout<<"您还可以输入一次!"<<endl; break;
case 3: cout<<"您没有权限继续输入,程序将要关闭!"<<endl; exit(0);
}
}
}while(blag);
}
int menu()
{
int m;
cout<<"***************************"<<'\n'
<<"** 1-统计各球队的成绩 **"<<'\n'
<<"** 2-打印各球队的信息 **"<<'\n'
<<"** 3-排名各球队 ******"<<'\n'
<<"** 4-更改用户名和密码 **"<<'\n'
<<"** 5-保存到文件 ******"<<'\n'
<<"** 0-退出 ************"<<'\n'
<<"***************************"<<endl;
cout<<"请选择:";
cin>>m;
return m;
}