C语言循环结构与时间函数用法实例教程
更新时间:2020年4月25日 17:41 点击:2529
本文实例展示了C语言循环结构与时间函数用法,对于C语言的学习来说是非常不错的参考借鉴材料。分享给大家供大家参考之用。具体如下:
完整实例代码如下:
/********************************************** ** 《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; }
本文实例源自国外网站,为一个数字游戏,感兴趣的读者可以调试运行一下,体会代码的原理,加深对C语言循环结构域时间函数的认识。
上一篇: 扩展KMP算法(Extend KMP)
下一篇: 算法之排序算法的算法思想和使用场景总结
相关文章
- 这篇文章主要为大家详细介绍了C语言实现放烟花的程序,有音乐播放,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-23
- 本篇文章主要介绍C语言中char的知识,并附有代码实例,以便大家在学习的时候更好的理解,有需要的可以看一下...2020-04-25
- eval函数在php中是一个函数并不是系统组件函数,我们在php.ini中的disable_functions是无法禁止它的,因这他不是一个php_function哦。 eval()针对php安全来说具有很...2016-11-25
- 在php中eval是一个函数并且不能直接禁用了,但eval函数又相当的危险了经常会出现一些问题了,今天我们就一起来看看eval函数对数组的操作 例子, <?php $data="array...2016-11-25
Python astype(np.float)函数使用方法解析
这篇文章主要介绍了Python astype(np.float)函数使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-08- 这篇文章主要介绍了在java中获取List集合中最大的日期时间操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-15
- 这篇文章主要介绍了Python中的imread()函数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-16
- 本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
- 下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
- 这篇文章主要介绍了详解如何将c语言文件打包成exe可执行程序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-25
- 这篇文章主要介绍了教你怎么用Java获取国家法定节假日,文中有非常详细的代码示例,对正在学习java的小伙伴们有非常好的帮助,需要的朋友可以参考下...2021-04-23
- 这篇文章主要介绍了.NET/C# 使用Stopwatch测量运行时间,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
- 这篇文章主要介绍了Java8处理List的双层循环问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-19
mysql中获取一天、一周、一月时间数据的各种sql语句写法
创建表:复制代码 代码如下:create table if not exists t( id int, addTime datetime default '0000-00-00 00:00:00′)添加两条初始数据:insert t values(1, '2012-07-12 21:00:00′);insert t values(2, '2012-07...2014-05-31- CREATE FUNCTION ChangeBigSmall (@ChangeMoney money) RETURNS VarChar(100) AS BEGIN Declare @String1 char(20) Declare @String2 char...2016-11-25
Android开发中findViewById()函数用法与简化
findViewById方法在android开发中是获取页面控件的值了,有没有发现我们一个页面控件多了会反复研究写findViewById呢,下面我们一起来看它的简化方法。 Android中Fin...2016-09-20- 这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
- strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。该函数返回字符串的其余部分(从匹配点)。如果未找到所搜索的字符串,则返回 false。语法:strstr(string,search)参数string,必需。规定被搜索的字符串。 参数sea...2013-10-04
PHP函数分享之curl方式取得数据、模拟登陆、POST数据
废话不多说直接上代码复制代码 代码如下:/********************** curl 系列 ***********************///直接通过curl方式取得数据(包含POST、HEADER等)/* * $url: 如果非数组,则为http;如是数组,则为https * $header:...2014-06-07- Foreach 函数(PHP4/PHP5)foreach 语法结构提供了遍历数组的简单方式。foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出错误信息。...2013-09-28