(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;
}
最后, 注意程序的结构
#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;
}
(11月4日)我的第一个小程序: 猜字游戏(TC2.0, Dev-c++4.9.9.2 编译通过)
的一百个c语言小程序
C/C++编译的程序占用内存的划分
在VC中编译、运行程序的小知识点
一周政经纵横(9月7日-9月11日) - 我的钢铁
Dev c Usage
在C++ 程序中调用被 C编译器编译后的函数,为什么要加 extern “C”声明?
【转】Android 开发手记二 C可执行程序编译实例
奥巴马的第一个总统办公日(组图)
C#教程第一课:简单的欢迎程序
怀孕第一个月的保健要点(二)
如何编译这段Tuxedo Pro*C程序? 环境设置与编译选项
yanghuw的专栏,第一个Spring程序
yanghuw的专栏,第一个Spring程序
搜狐游戏诚聘游戏精英(2010年3月5日更新)
佛教Dev小常识
某女生编的c语言程序(搞笑!)
培养0-18个月宝宝注意力的游戏
培养0-18个月宝宝注意力的游戏
11个月宝宝必玩的十个益智游戏
11个月宝宝必玩的十个益智游戏
2001年 美南海撞毁我军用飞机(历史上的今天4月1日)
49个让你成功的人生细节(11月25日)
做人做事的74个准则(10月4日)