请高手帮忙做下C语言题~!

2025-03-04 07:21:49
推荐回答(2个)
回答1:

//---------------------------------------------------------------------------

#include
#include
#include
#include

void head_prt(void)
{
printf(" I have a number between 1 and 1000.\nCan you guess my number?\nPlease type your first guess.\n");
}

int isequal(const int rnd,const int inp)
{
static int n=0;
++n;
if (rnd==inp) {
printf("Excellent! You guessed the number on the %d th try!\nwould you like to play again (y/n)?",n);
n=0;
return 1;
}
else if (inp puts("Too low. Try again.");
}
else puts("Too high. Try again.");

return 0;
}

void flush(void)
{
while (getchar()!='\n') ;
}

int main(void)
{
int gue=0,inp=0;

srand(time(NULL));
gue=rand()%1000+1;
do{
head_prt();
do
{
while (!scanf("%d",&inp)) flush();

}while (!isequal(gue,inp));
gue=rand()%1000+1;
flush();

}while (tolower(getchar())=='y');
return 0;
}
//---------------------------------------------------------------------------

回答2:

#include
#include
#include
int rand_num();
int compare(int,int);
void main()
{
int correct_num=0;
int count=1;
int try_num=0;
int flag;
int choose;
while (1)
{
flag=0;
printf("I have a number between 1 and 1000.\nCan you guess my number?\nPlease type your first guess\n");
correct_num=rand_num();
while (1)
{
scanf("%d",&try_num);
switch (compare(try_num,correct_num))
{
case 0:
printf("Excellent! You guessed the number on the %d th try!\nwould you like to play again (input 1 for yes/2 for no)?\n",count);
scanf("%d",&choose);
if (choose==2)
{
exit(0);
}
else if(choose==1)
{
flag=1;
break;
}
case 1:
printf("Too high. Try again.\n");
break;
case 2:
printf("Too low. Try again.\n");
break;
}
if(flag)
{
count=0;
break;
}
else
count++;
}
}
}
int rand_num()
{
srand(time(0));
return (rand()%1000+1);
}
int compare(int from,int to)
{
if (from==to)
{
return 0;
}
else
return from>to?1:2;
}