当前位置:网站首页>[C language] guessing numbers game
[C language] guessing numbers game
2022-07-28 20:17:00 【White Hibiscus KK】
List of articles
Preface
Now many games have the function of drawing lottery cards , In fact, this is similar to guessing numbers , Generate a random number , Then you guess , If you guess right, you will win the prize . Guess a certain number of times and you'll be guaranteed . To realize the small game of guessing numbers , First, let the program generate random numbers , That's what we need rand、srand and time These three functions , Secondly, we need to understand timestamp .
rand function
First look at the picture below :
From this picture, we can see rand The return value of the function is int type , The formal parameter is empty , Its header file <stdlib.h> And its return value range is 0 To RAND_MAX. and RAND_MAX stay VS Medium is 0x7fff, Convert to binary 32767. therefore rand The range of returned random numbers is 0 To 32727.
In fact, we use rand Function can generate random numbers , Then why should we use srand Function? ?
Because only the random number generated for the first time is random , If the program is executed many times , The random number generated after the second time will be the same as that generated for the first time . This is a kind of Pseudorandom .
So only rand Functions are not enough , We need to use it. rand Function before , First use strand function .
srand function

to glance at strand function , The return value is void, Formal parameters are int Data of type , Use strand function , First of all, give strand Pass a int Data of type , According to the value passed in , The random numbers generated are also different . But this is contradictory . Before generating a random number, first pass in a random number , To solve this problem , Use a timestamp .
Time stamp

We can't give it a random number , But we can pass in time as a parameter , Time changes , The random number generated in this way is variable . How to get a timestamp ?C Language also provides us with a time Function can help us get timestamp .
time The header file for is <time.h>,time The function prototype of is a little complicated , It doesn't matter if you don't understand , Will use it . We just want to time The value of is passed as a parameter to strand function .strand The formal parameter of is unsigned int Type of , and time The return value of time_t Type of , We can cast .time The formal parameter of is a parameter of pointer type , We use it NULL That's all right. .
srand((unsigned int)time(NULL));
After solving this problem , We still have to solve rand The problem of generating the range of random numbers , We If you want to get one 1~N Between the numbers , To generate random number pairs N Take the rest +1. For the convenience of guessing numbers , The range of generated random numbers is controlled within 1 To 100. It's not easy to guess numbers at once , If you guess wrong, keep guessing , Guess right and quit . Here is the complete code .
Complete code + Program diagram
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void menu()
{
printf("|-------------------|\n");
printf("|-----1. Play a game ------|\n");
printf("|-----0. sign out --------|\n");
printf("|-------------------|\n");
}
void game()
{
int ret = rand()%100+1;
int m = 0;
while (1)
{
scanf("%d", &m);
if (m > ret)
{
printf(" Guess the !\n");
}
else if (m < ret)
{
printf(" Guess a little !\n");
}
else
{
printf(" congratulations , Guessed it !\n");
break;
}
}
}
int main()
{
srand((unsigned int)time(NULL));
int n = 0;
do
{
menu();
printf(" Please select :");
scanf("%d", &n);
switch (n)
{
case 1:
printf(" Start guessing numbers \n");
game();
break;
case 0:
printf(" Exited \n");
break;
default:
printf(" Wrong choice , Please reselect \n");
break;
}
} while(n);
return 0;
}
The program running chart is as follows :
边栏推荐
- In the second half of 2022, the system integration project management engineer certification starts on August 20
- CDGA|工业互联网行业怎么做好数据治理?
- Power Bi 2021 calendar DAX code
- A chip company fell in round B
- Saltstack advanced
- 云原生编程挑战赛火热开赛,51 万奖金等你来挑战!
- Implementation of strcat in C language
- MySQL command statement (personal summary)
- [C language] string reverse order implementation (recursion and iteration)
- 1. C language variable type, global variable, local variable
猜你喜欢
![[C language] header file of complex number four operations and complex number operations](/img/f9/b389fe5367f1fa6cd18aaac856bc0d.png)
[C language] header file of complex number four operations and complex number operations
![[C language] simulation implementation of strlen (recursive and non recursive)](/img/73/e92fe714515491f1ea366d6924c9ec.png)
[C language] simulation implementation of strlen (recursive and non recursive)
![[experiment sharing] CCIE BGP reflector experiment](/img/e4/1ddd611c8438cb6ca1be32f34fa67a.png)
[experiment sharing] CCIE BGP reflector experiment
![[C language] function](/img/81/c185e2bb5eccc13433483f9558f52a.png)
[C language] function

【CodeForces】Educational Codeforces Round 132 (Rated for Div. 2)

熊市下PLATO如何通过Elephant Swap,获得溢价收益?

9. Pointer of C language (4) pointer and one-dimensional array, pointer operation

Store and guarantee rancher data based on Minio objects

私有化部署的即时通讯平台,为企业移动业务安全保驾护航
![[C language] initial C language reflection and summary](/img/21/826d144867f7a73ec2cd8896a5250a.png)
[C language] initial C language reflection and summary
随机推荐
C language functions and pointers
一文读懂如何部署具有外部数据库的高可用 K3s
Why is customer support important to SaaS?
Deploy LNMP automatically with saltstack
mmo及时战斗游戏中的场景线程分配
Basic mathematical knowledge (update)
NEIL: Extracting Visual Knowledge from Web Data
8. Compilation errors of C language and Chinese explanation
[C language] header file of complex number four operations and complex number operations
【CodeForces】Educational Codeforces Round 132 (Rated for Div. 2)
Source code analysis of scripy spider
最大交换[贪心思想&单调栈实现]
[C language] summary of methods for solving the greatest common divisor
软考中级(系统集成项目管理工程师)高频考点
Quick sort template
C language function
Maximum exchange [greedy thought & monotonic stack implementation]
C language - question brushing column
adb remount of the / superblock failed: Permission denied
Const pointer of C language and parameter passing of main function