C语言编程 猜数游戏

C语言编程 猜数游戏,第1张

#include

#include

//用到了rand函数,所以要有这个头文件

#include

//用到了time函数,所以要有这个头文件

int

main()

{

int

number

//number用于存储随机数

int

guess=0

//guess用于存储玩家猜的数

srand((unsigned)

time(null))//用系统时间作为rand函数使用的种子

number=rand()%100

//随机除以100,取余数

number++

//余数加1

printf("猜数字游戏\n")

printf("该数字在1到100之间\n")

while(guess!=number)

{

printf("请输入您所猜的数:")

scanf("%d",&guess)

//如果玩家猜的数较小,给予提示

if

(guess

number)

{

printf("大了\n")

}

}

//猜中则循环结束,输出猜中的数字

printf("猜对了,这个数字就是:%d\n",number)

return

0

}

num=randi(100,[1 1])

i=0

while (i<3)

elseif(a>num)

disp 'High'

i=i+1

else

end

if (i==3)

disp 'You lose'

end

#include <stdio.h>

int main()

printf("enter the integer you guess:")

scanf("%d", &t)

if (data == t)

{

printf("the data is:%d\n", data)

break

}

else if(data >t)

else

count++

}while(1)

if (count <= 3)

else if(count <= 7)

return 0

编程环境

MATLAB由一系列工具组成。这些工具方便用户使用MATLAB的函数和文件,其中许多工具采用的是图形用户界面。包括MATLAB桌面和命令窗口、历史命令窗口、编辑器和调试器、路径搜索和用于用户浏览帮助、工作空间、文件的浏览器。

随着MATLAB的商业化以及软件本身的不断升级,MATLAB的用户界面也越来越精致,更加接近Windows的标准界面,人机交互性更强, *** 作更简单。而且新版本的MATLAB提供了完整的联机查询、帮助系统,极大的方便了用户的使用。

源码如下:

/* File: guess.c */

#include <stdio.h>  /* standard input & output support */

#include <stdlib.h> /* srand() rand() */

#include <time.h>   /* time() */

/* 宏定义 */

#define NUMBER_LENGTH   5   /* 随机数长度 */

#define NUMBER_LIMIT    10  /* 随机数限制, 每一位0-9 */

#define INPUT_LENTH     128 /* 输入缓冲区大小 */

char goal[NUMBER_LENGTH]    = {0}  /* 保存随机数 */

char flag[NUMBER_LIMIT]     = {0}  /* 保存随机数标志, 保证不重复 */

char input[INPUT_LENTH]     = {0}  /* 保存输入 */

/* 初始化用于保存数据的数组 */

void initData()

{

int i = 0

while (i < NUMBER_LENGTH)

goal[i++] = 0

i = 0

while (i < NUMBER_LIMIT)

{

flag[i++] = 0

}

}

/* 初始化用于保存缓冲区的数组 */

void initBuffer()

{

int i = 0

while (i < INPUT_LENTH)

input[i++] = 0

}

/* 显示猜测结果 */

void display()

{

int count = 0

int i = 0

while (i < NUMBER_LENGTH)

{

if (input[i] == goal[i])

{

printf("%c", 'o')

count++

}

else

{

printf("%c", 'x')

}

i++

}

printf("\nRIGHT: %d bit(s)\n", count)

if (count == NUMBER_LENGTH)

{

printf("You win! The number is %s.\n", goal)

exit(0)

}

}

/* 生成随机数 */

void general()

{

/* 以时间作为时间种子保证生成的随机数真正具有随机性质 */

srand((unsigned int)time(NULL))

int i = 0

while (i < NUMBER_LENGTH)

{

char tmp

do

{

tmp = '0' + ((i != 0) ? (rand() % 10) : (1 + rand() % 9))

} while (flag[tmp] != 0)

flag[tmp] = 1

goal[i++] = tmp

}

}

/* 输入方法,用于猜测 */

void guess()

{

printf("Please input the number you guessed:\n")

scanf("%s", input)

display()

initBuffer()

}

/* 主函数,程序主框架 */

int main (int argc, const char * argv[])

{

initData()

initBuffer()

general()

while (1) guess()

return 0

}

==============================================

运行结果见附图,希望我的回答能够对你有所帮助。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/11377506.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-15
下一篇2023-05-15

发表评论

登录后才能评论

评论列表(0条)

    保存