#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAX_TIMES 10
#define MIN_NUMBER 0
#define MAX_NUMBER 100
int MakeNuumber();
void GuessNumber(int n);
int IsValidNum(int num);
int IsRight(const int number,const int guess);
int main()
{
int number;
char reply;
srand(time(NULL));
do
{
number = MakeNumber();
GuessNumber(number);
scanf(" %c",&reply);
while(getchar() != '\n')
{
;
}
}
while (reply == 'y' || reply == 'y');
}
void GuessNumber(int number)
{
int guess,count = 1,right = 0;
do
{
printf("Try %d:",count);
int ret = scanf("%d",&guess);
while(getchar() != '\n')
{
;
}
if(ret != 1 || !IsValidNum(guess))
{
printf("Input error!\n");
fflush(stdin);
continue;
}
count++;
right = IsRight(number, guess);
}
while(!right && count <= MAX_TIMES);
if(right)
{
printf("Congratulations! You're so cool!\n");
}
else
{
printf("Mission failed after %d attempts.\n",MAX_TIMES);
}
}
int IsValidNum(int num)
{
if(num >= MIN_NUMBER && num <= MAX_NUMBER)
{
return 1;
}
else
{
return 0;
}
}
int MakeNumber(void)
{
int number;
number = (rand() % (MAX_NUMBER - MIN_NUMBER + 1)) + MIN_NUMBER;
return number;
}
int IsRight(const int number,const int guess)
{
if(guess < number)
{
printf("Wrong! Too low.\n");
return 0;
}
else if(guess > number)
{
printf("Wrong! Too high.\n");
return 0;
}
else
{
return 1;
}
}
“ 你是否想继续”这个功能的代码我还没有做出来,但是勉强可以用