2012年5月5日星期六

do u understand the following c++ code?


#include <iostream>
#include <ctime>
#include <cstdlib>
#include <algorithm>

using namespace std;

void menudisplay();
int menuoption();
int datavalidation (char data[100]);
void datavalidation2 (char data[100],int j,int guess[4]);
int datavalidation3 (char name[30]);
void option1 (int noplayer[1], char nameno[5][30],int length[5]);
void option2(int randomnumber[4],int noplayer[1],int length[5],char nameno[5][30],double dif[5],int notime[5]);
void rating (int notime[5],double dif[5],int noplayer[1],int length[5],char nameno[5][30]);
void random(int randomnumber[4]);
void testguess(int j,int guess[4]);
int conditionguess(int guess[4],int randomnumber[4],int j);
void displayallplayer(int noplayer[1], char nameno[5][30],int length[5]);

int main()
{
char data[100],nameno[5][30];
int option,counter=0,noplayer[1],length[5],randomnumber[4],notime[5],counter2=0;
double dif[5];

do
{
menudisplay();
option=menuoption();

if(option==1)
{
counter=1;
counter2=0;
option1(noplayer, nameno, length);
}
else if(option==2)
{
if(counter==0)
{
cout<<"Sorry. Please register first before you play the game.\n\n";
system("pause");
}
else
{
counter2=1;
option2(randomnumber,noplayer,length,nameno,dif,notime);
//cout<<notime[0]<<notime[1];
counter=0;
}

}
else if(option==3)
{
if(counter2==0)
{
cout<<"Sorry. Please play first before you want to show the ranking.\n\n";
system("pause");
}
else
{
rating(notime,dif,noplayer,length,nameno);
}

}

}while(option!=4);

}

void menudisplay()
{
system("cls");
cout<<"Menu\n"
<<"----\n"
<<"1.To register number of participants and name\n"
<<"2.To start the game\n"
<<"3.To show ranking\n"
<<"4.To quit\n"
<<"\nOption: ";
}

int menuoption()
{
int option;
char data[100];

fflush(stdin);
cin.getline(data,100);
option=datavalidation(data);
if(option>4&&option<=10)
{
cout<<"Error!\n";
system("pause");
}
else
{}
return option;
}

int datavalidation (char data[100])
{
int alpha=0,digit=0,space=0,correctdata,place;
for(int i=0;i<strlen(data);i++)
{
if(isdigit(data[i]))
{
++digit;
place=i;
}
else if(isspace(data[i]))
{
++space;
}
else
{}
}
if(digit==1&&digit+space==strlen(data))
{
correctdata=data[place];
return correctdata-48;
}
else if(space==strlen(data))
{
return 100;
}
else
{
return 10;
}
}

void datavalidation2 (char data[100],int j,int guess[4])
{
int alpha=0,digit=0,space=0,place[100],k=0;
for(int i=0;i<strlen(data);i++)
{
if(isdigit(data[i]))
{
++digit;
place[k]=i;
k++;
}
else if(isspace(data[i]))
{
++space;
}
else
{}
}
if(digit==4&&digit+space==strlen(data)&&place[0]==place[1]-1&&place[1]==place[2]-1&&place[2]==place[3]-1)
{
guess[3]= data[place[3]]-48;
guess[2]= data[place[2]]-48;
guess[1]= data[place[1]]-48;
guess[0]= data[place[0]]-48;
}
else if(space==strlen(data))
{
return testguess(j,guess);
}
else
{
cout<<"INVALID INPUT"<<endl;
return testguess(j,guess);
}
}

int datavalidation3 (char name[30])
{
int space=0;
for(int i=0;i<strlen(name);i++)
{
if(isspace(name[i]))
{
++space;
}
else
{}
}
if(space==strlen(name))
{
return 0;
}
else
{
return 1;
}
}

void option1 (int noplayer[1], char nameno[5][30],int length[5])
{
char data[100],name[30];
int condition;
do
{
system("cls");
menudisplay();
cout<<1;
cout<<"\n\nPlease enter the number of participants <1-5>: ";
fflush(stdin);
cin.getline(data,100);
noplayer[0]=datavalidation(data);

if(noplayer[0]>5&&noplayer[0]<=10)
{
cout<<"Error!\n\n";
system("pause");
}
else
{}
}while(noplayer[0]<1||noplayer[0]>5);
for(int no=1;no<=noplayer[0];no++)
{
do
{
cout<<"Please enter name "<<no<<": ";
fflush(stdin);
cin.getline(name,30);
condition=datavalidation3(name);

if (condition==0)
{
system("cls");
menudisplay();
cout<<1;
cout<<"\n\nPlease enter the number of participants <1-5>: "<<noplayer[0]<<endl;

for(int no1=1;no1<no;no1++)
{
cout<<"Please enter name "<<no1<<": ";
for(int i=0;i<length[no1-1];i++)
{
cout<<nameno[no1-1][i];
}
cout<<"\n";
}

}
}while(condition==0);
for(int i=0;i<strlen(name);i++)
{
nameno[no-1][i]=name[i];

}
length[no-1]=strlen(name);
}
//displayallplayer(noplayer,nameno,length);
}

void option2(int randomnumber[4],int noplayer[1],int length[5],char nameno[5][30],double dif[5],int notime[5])
{
int guess[4],c;
time_t start,end;

for(int no=1;no<=noplayer[0];no++)
{
random(randomnumber);
system("cls");
for(int i=0;i<length[no-1];i++)
{
cout<<nameno[no-1][i];

}
cout<<", it's your turn. Please press any key to start ";
system("pause > nul");
int j=1;
time (&start);
do
{
testguess(j,guess);
//cout<<guess[0]<<guess[1]<<guess[2]<<guess[3]<<endl;
//cout<<randomnumber[0]<<randomnumber[1]<<randomnumber[2]<<randomnumber[3];
c=conditionguess(guess,randomnumber,j);
if(c!=4)
{
j++;
}
else
{}
}while(j<=10&&c!=4);

time (&end);
dif[no-1] = difftime (end,start);

notime[no-1]=j;

if(j=10&&c!=4)
{
cout<<endl<<"You are lose. The correct answer is "<<randomnumber[0]<<randomnumber[1]<<randomnumber[2]<<randomnumber[3]<<" and u used "<<dif[no-1]<<"s"<<endl;
}
else
{
cout<<"Congratulation!!! u used "<<dif[no-1]<<"s"<<endl;

}

system("pause");
//cout<<endl<<dif[0];

}
}

void rating (int notime[5],double dif[5],int noplayer[1],int length[5],char nameno[5][30])
{
int smallest;

for(int first=0; first<noplayer[0]; first++)
{
smallest = notime[first];

for(int second=first+1; second<noplayer[0]; second++)
{
if(notime[second]<smallest)
{
swap(notime[second],notime[first]);
swap(dif[second],dif[first]);
swap(nameno[second],nameno[first]);
swap(length[second],length[first]);
}

else if((notime[second]==smallest) && dif[second]<dif[first])
{
swap(notime[second],notime[first]);
swap(dif[second],dif[first]);
swap(nameno[second],nameno[first]);
swap(length[second],length[first]);
}
}
}


cout<<"Ranking List"<<endl
<<"---------------"<<endl;
for(int j=0; j<noplayer[0]; j++)
{
cout<< j+1<<". ";
for(int i=0;i<length[j];i++)
{
cout<< nameno[j][i];
}
cout<<endl;
}

system("pause");
}

void random(int randomnumber[4])
{
srand(time(NULL));
randomnumber[0]= rand()%9+1;
do
{
randomnumber[1]=rand()%10;
}while(randomnumber[1]==randomnumber[0]);

do
{
randomnumber[2]=rand()%10;
}while(randomnumber[2]==randomnumber[0]||randomnumber[2]==randomnumber[1]);

do
{
randomnumber[3]=rand()%10;
}while(randomnumber[3]==randomnumber[0]||randomnumber[3]==randomnumber[1]||randomnumber[3]==randomnumber[2]);

system("cls");
cout<<"The random number generated is ";
for(int i=0;i<=3;i++)
{
cout<<randomnumber[i];

}
cout<<endl;
system("pause");
}

void testguess(int j,int guess[4])
{
char data[100];
cout<<"\nGUESS #"<<j<<": ";
cin.getline(data,100);
datavalidation2 (data,j,guess);
}

int conditionguess(int guess[4],int randomnumber[4], int j)
{
int c=0,w=0,p=0;
for(int i=0;i<=3;i++)
{
if (guess[i]==randomnumber[i])
{
c++;
}
}
if (guess[0]!=randomnumber[0]&&guess[0]!=randomnumber[1]&&guess[0]!=randomnumber[2]&&guess[0]!=randomnumber[3])
{
w++;
}
if(guess[1]!=randomnumber[0]&&guess[1]!=randomnumber[1]&&guess[1]!=randomnumber[2]&&guess[1]!=randomnumber[3])
{
w++;
}
if(guess[2]!=randomnumber[0]&&guess[2]!=randomnumber[1]&&guess[2]!=randomnumber[2]&&guess[2]!=randomnumber[3])
{
w++;
}
if(guess[3]!=randomnumber[0]&&guess[3]!=randomnumber[1]&&guess[3]!=randomnumber[2]&&guess[3]!=randomnumber[3])
{
w++;
}
p=4-c-w;
if (j<=9)
{
cout<<c<<"c "<<w<<"w "<<p<<"p"<<endl;
}
return c;
}

void displayallplayer(int noplayer[1], char nameno[5][30],int length[5])
{
for(int no=1;no<=noplayer[0];no++)
{
for(int i=0;i<length[no-1];i++)
{
cout<<nameno[no-1][i];
}
cout<<"\n";
}
system("pause");
}

没有评论: