• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

C语言循环结构与时间函数用法实例教程

c语言 搞代码 4年前 (2022-01-06) 25次浏览 已收录 0个评论

这篇文章主要介绍了C语言循环结构与时间函数用法,是C语言中非常重要的一个技巧,需要的朋友可以参考下

本文实例展示了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 t<em style="color:transparent">来源gao.dai.ma.com搞@代*码网</em>hird 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语言循环结构域时间函数的认识。

以上就是C语言循环结构与时间函数用法实例教程的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:C语言循环结构与时间函数用法实例教程
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址