本文实例展示了C语言循环结构与时间函数用法,对于C语言的学习来说是非常不错的参考借鉴材料。分享给大家供大家参考之用。具体如下:
完整实例代码如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
/**********************************************
** 《Beginning C 4th Edition》 Notes codes
** Created by Goopand
** Compiler: gcc 4.7.0
***********************************************/
/* Simon games : memory ability test */
#include <stdio.h> /* for printf(),scanf() function */
#include <ctype.h> /* for tolower() function */
#include <stdbool.h> /* for bool type: true, false */
#include <time.h> /* for time() function */
#include <stdlib.h> /* for rand(),srand() function */
int main( void )
{
char nextGame = 'Y' ; /* go ahead for another game */
bool correct = false ; /* guess correctly */
int counter = 0; /* number of sequences guessed correctly */
int seq_len = 0; /* length of sequence */
int input_num = 0; /* stores an input digit */
time_t seed = 0; /* seed value for random number sequence */
//int i = 0; /* for loop variable */
clock_t start_clock = 0; /* record start clock time (not second) */
int time_taken = 0; /* Time used for game in seconds */
printf ( "Here is a simon game for training memory ability .\\n" );
printf ( "\\nThe screen will show a number sequence for 1 second , " );
printf ( "then the sequence will disappear. You are suggested to "
"input the same sequence to pass the game.\\n" );
printf ( "\\nNotice this: each digit is seperated by a space. \\n" );
printf ( "\\nNow press Enter to play .. Good luck !\\n" );
scanf ( "%c" ,&nextGame); //Waiting for user's input
do
{
correct = true ;
counter = 0 ; /* successful tries */
seq_len = 2 ; /* Initial length of a digit sequence */
time_taken = clock ();
while (correct)
{
/* On each third successful try, increase the squence length */
seq_len += (counter++ % 3 == 0);
//if "==" expression is true,returns 1; else returns 0
/* time(NULL) returns number of seconds since 1970-01-01 */
seed = time (NULL);
/* Generate and display a sequence of random numbers */
srand ((unsigned int )seed);
//initialize the seed for rand() function
for ( int i=0; i<seq_len; i++)
printf ( "%d " , rand () % 10);
//Output a random digit and a space
/* Wait one second */
start_clock = clock () ;
//returns start clock time,clock() is a timer
for (; clock () - start_clock < CLOCKS_PER_SEC;) ;
//CLOCKS_PER_SEC is a const defined in time.h
/* Now overwrite the digit sequence */
printf ( "\\r" );
//control char "\\r" : locator back to beginning of the line
for ( int i=0; i<seq_len; i++)
printf ( " " );
//two spaces to overwrite the sequence
printf ( "\\r" );
/* Check wether the input sequence is correct */
srand ((unsigned int )seed); //Restart the random sequence
for ( int i=0; i<seq_len; i++)
{
scanf ( "%d" ,&input_num); //Read an input number
if (input_num != rand () % 10 )
{
correct = false ;
break ;
}
}
printf ( "%s\\n" ,correct ? "Correct !" : "Wrong !" );
}
/* Calculate total time to play the game in seconds */
time_taken=( clock () - time_taken) / CLOCKS_PER_SEC ;
/* Output the total game score */
printf ( "\\n\\nYour score is %d" , --counter * 100 / time_taken);
fflush (stdin); //Empty the input buffer
printf ( "\\nDo you want to play again (y/n)? " );
scanf ( "%c" ,&nextGame);
} while ( tolower (nextGame) == 'y' );
return 0;
}
|
相关文章
猜你喜欢
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 102
-
2025-06-04 27
-
PHP中CURL的CURLOPT_POSTFIELDS参数使用细节
2025-05-29 49 -
2025-05-29 103
-
2025-05-29 37
热门评论