C語言循環(huán)結(jié)構(gòu)與時(shí)間函數(shù)用法實(shí)例教程
本文實(shí)例展示了C語言循環(huán)結(jié)構(gòu)與時(shí)間函數(shù)用法,對(duì)于C語言的學(xué)習(xí)來說是非常不錯(cuò)的參考借鑒材料。分享給大家供大家參考之用。具體如下:
完整實(shí)例代碼如下:
/**********************************************
** 《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;
}
本文實(shí)例源自國外網(wǎng)站,為一個(gè)數(shù)字游戲,感興趣的讀者可以調(diào)試運(yùn)行一下,體會(huì)代碼的原理,加深對(duì)C語言循環(huán)結(jié)構(gòu)域時(shí)間函數(shù)的認(rèn)識(shí)。
上一篇:C++實(shí)現(xiàn)二叉樹非遞歸遍歷方法實(shí)例總結(jié)
欄 目:C語言
本文標(biāo)題:C語言循環(huán)結(jié)構(gòu)與時(shí)間函數(shù)用法實(shí)例教程
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/3452.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)數(shù)怎么表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改


