} void main() { char *user; int count; char key; cout<<"Welcome to use GuessNumber game!"<<endl; do { cout<<"Please select the difficulty:"<<flush; do { N=getch()-'0'; }while(N<1); cout<<N<<endl; user=new char[N];
count=0; product(); do { input(user); check(user); cout<<'\t'<<a<<'A'<<b<<'B'<<endl; count++; }while(a!=N); cout<<"Congratulations!You get it by guessing for "<<count<<" times."<<endl; cout<<"Would you like to try again?(y/n) "<<flush; do { key=getch(); }while(key!='y'&&key!='n'); cout<<key<<endl; }while(key=='y'); cout<<"Thanks for using!"<<endl;}
//猜数字游戏#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>int N;
char result[10]="123456789";
int a,b;
void product()
{
char temp;
int t; srand(time(0)); //产生真正的随机数 for(int i=0;i<N;i++)
{
t=rand()%9;
if(t!=i)
{
temp=result[i];
result[i]=result[t];
result[t]=temp;
}
}
}void check(char *user)
{
a=b=0;
for(int i=0;i<N;i++)
{
for(int j=0;j<N&&*(result+i)!=*(user+j);j++);
if(j==i)
a++;
else if(j!=N)
b++;
}
}
void input(char *user)
{
char ch;
int k;
cout<<"Please input "<<N<<" numbers:"<<endl;
for(int i=0;i<N;i++)
{
do
{
k=0;
ch=getch();
for(int j=0;j<i&&!k;j++)
{
if(*(user+j)==ch)
k=1;
}
}while(ch<'1'||ch>'9'||k);
*(user+i)=ch;
cout<<ch<<flush;
}
}
void main()
{
char *user;
int count;
char key;
cout<<"Welcome to use GuessNumber game!"<<endl;
do
{
cout<<"Please select the difficulty:"<<flush;
do
{
N=getch()-'0';
}while(N<1);
cout<<N<<endl; user=new char[N];
count=0;
product();
do
{
input(user);
check(user);
cout<<'\t'<<a<<'A'<<b<<'B'<<endl;
count++;
}while(a!=N);
cout<<"Congratulations!You get it by guessing for "<<count<<" times."<<endl;
cout<<"Would you like to try again?(y/n) "<<flush;
do
{
key=getch();
}while(key!='y'&&key!='n');
cout<<key<<endl;
}while(key=='y');
cout<<"Thanks for using!"<<endl;}
^_^...赶紧搜索以前得相关程序吧.我有的回答的啊!