(11月4日)我的第一个小程序: 猜字游戏(TC2.0, Dev-c++4.9.9.2 编译通过)

来源:百度文库 编辑:神马文学网 时间:2024/04/25 01:04:35
注意srand((int))time(NULL)); 和 rand();的用法, 还有, 用scanf录入数据时必须是地址(说起容易, 做时忘记), 切记切忌
最后, 注意程序的结构
#include
#include
#include
#include
#define GenRand (rand()%10)      //generate digit between 0-10
void initArray(int * ArrayC, int n)                 //init N random digits
{
int i = 0, arrayTmp[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
srand((int)time(NULL));                                  //current time as rand() seed
while(1)
{
ArrayC[i] = GenRand;
if (arrayTmp[ArrayC[i]] == 0)
arrayTmp[ArrayC[i]] = 1;
else continue;
i++;
if (i == n)
break;
}
}
int isOverlap(int* ArrayB, int n)                    //if there are the same digit in arrayB, return 1, else return 0
{
int num[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int i;
for(i = 0; i < n; i++)
{
if(num[ArrayB[i]] == 0)                                     //if ArrayB[i] already exist
num[ArrayB[i]] = 1;
else
return 1;
}
return 0;
}
void receiveData(int* ArrayA, int n)                         //receive user‘s Data
{
int i;
while(1)
{
L1:    printf("Please input %d different digit: \n", n);
for(i = 0; i < n; i++)
{
scanf("%d", ArrayA+i);
}
/*printf("Your %d digits is: ", n);
for (i = 0; i < n; i ++)
{
printf("%d ", ArrayA[i]);
}
printf("\n");*/
for (i = 0; i < n; i++)
{
if (!isdigit(ArrayA[i]+48))                                         // judge arrayA[i] is a digit, not a number bigger than 10
{
printf("Your have wrong input, only digit can be accepted, try again.\n");
goto L1;
}
}
if (isOverlap(ArrayA, n))                                           //user‘s digit should not duplicated
{
printf("Your have wrong input, same digit are not allowed, try again.\n");
continue;
}
break;
}
}
int printResult(int* a, int* b, int n)   /*value 1 means the answer is absulotely right*/
{
int numOfA = 0, numOfB = 0, i;
int arrayTmp[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
for (i = 0; i < n; i++)                      //find the num of a[i]=b[i]  : number of A
{
if(a[i] == b[i])
numOfA++;
}
for (i = 0; i < n; i++)
{
arrayTmp[a[i]] = 1;                //  arrayTmp[a[i]] = 1,   mark the a[i] bit in arrayTmp
}
for (i = 0; i < n; i++)                  //   if arrayTmp[b[i]] == 1 means this bit has been marked by a[i], it means arrayA also has it
{
if (arrayTmp[b[i]] == 1)
numOfB++;                       //at this moment numOfB = numOfA+numOfB
}
numOfB -= numOfA;
printf("The Answer is: ");
for (i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
printf("\n");
printf("Your digit are: ");
for (i = 0; i < n; i++)
{
printf("%d ", b[i]);
}
printf("\n");
printf("Answer: ");
printf("%dA%dB\n", numOfA, numOfB);
if(numOfA == n)
{
printf("Bingo!!!!Your are so clever\n");
return 1;
}
return 0;
}
int main()
{
int a[4], b[4], i = 0;
char cont;
printf("Let‘s start the game: Are you ready? (y / n) \n");
cont = getch();
while(1)
{
if (cont == ‘n‘) break;
if (cont == ‘y‘)
{
initArray(a, 4);
while(!i)
{
receiveData(b, 4);
i = printResult(a, b, 4);
}
printf("Congratulations, do you want to try again? (y / n)\n;
cont = getch();
}
else
{
printf("You should type ‘y‘ or ‘n‘: \n");
cont = getch();
}
}
printf("Game Over\n");
system("pause");
return 1;
}