C语言中如何循环利用函数,C语言循环结构与时间函数用法实例教程

/**********************************************

** 《Beginning C 4th Edition》 Notes codes

** Created by Goopand

** Compiler: gcc 4.7.0

***********************************************/

/* Simon games : memory ability test */

#include /* for printf(),scanf() function */

#include /* for tolower() function */

#include /* for bool type: true, false */

#include /* for time() function */

#include /* 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值