亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C語(yǔ)言循環(huán)結(jié)構(gòu)與時(shí)間函數(shù)用法實(shí)例教程

 更新時(shí)間:2014年08月23日 14:47:17   投稿:shichen2014  
這篇文章主要介紹了C語(yǔ)言循環(huán)結(jié)構(gòu)與時(shí)間函數(shù)用法,是C語(yǔ)言中非常重要的一個(gè)技巧,需要的朋友可以參考下

本文實(shí)例展示了C語(yǔ)言循環(huán)結(jié)構(gòu)與時(shí)間函數(shù)用法,對(duì)于C語(yǔ)言的學(xué)習(xí)來(lái)說(shuō)是非常不錯(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í)例源自國(guó)外網(wǎng)站,為一個(gè)數(shù)字游戲,感興趣的讀者可以調(diào)試運(yùn)行一下,體會(huì)代碼的原理,加深對(duì)C語(yǔ)言循環(huán)結(jié)構(gòu)域時(shí)間函數(shù)的認(rèn)識(shí)。

相關(guān)文章

  • C++中Copy-Swap實(shí)現(xiàn)拷貝交換

    C++中Copy-Swap實(shí)現(xiàn)拷貝交換

    本文主要介紹了C++中Copy-Swap實(shí)現(xiàn)拷貝交換,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • C語(yǔ)言中 int main(int argc,char *argv[])的兩個(gè)參數(shù)詳解

    C語(yǔ)言中 int main(int argc,char *argv[])的兩個(gè)參數(shù)詳解

    這篇文章主要介紹了C語(yǔ)言中 int main(int argc,char *argv[])的兩個(gè)參數(shù)詳解的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • 10個(gè)步驟Opencv輕松檢測(cè)出圖片中條形碼

    10個(gè)步驟Opencv輕松檢測(cè)出圖片中條形碼

    這篇文章主要為大家詳細(xì)介紹了Opencv輕松檢測(cè)出圖片中條形碼的10個(gè)步驟,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • C語(yǔ)言版掃雷游戲

    C語(yǔ)言版掃雷游戲

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言版掃雷游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • C++構(gòu)建函數(shù)使用介紹

    C++構(gòu)建函數(shù)使用介紹

    構(gòu)造函數(shù)主要作用在于創(chuàng)建對(duì)象時(shí)為對(duì)象的成員屬性賦值,構(gòu)造函數(shù)由編譯器自動(dòng)調(diào)用,無(wú)須手動(dòng)調(diào)用;析構(gòu)函數(shù)主要作用在于對(duì)象銷毀前系統(tǒng)自動(dòng)調(diào)用,執(zhí)行一 些清理工作
    2022-08-08
  • C++實(shí)現(xiàn)圖書(shū)管理系統(tǒng)課程設(shè)計(jì)(面向?qū)ο?

    C++實(shí)現(xiàn)圖書(shū)管理系統(tǒng)課程設(shè)計(jì)(面向?qū)ο?

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)圖書(shū)管理系統(tǒng)課程設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 淺析C++中的動(dòng)態(tài)內(nèi)存分配

    淺析C++中的動(dòng)態(tài)內(nèi)存分配

    這篇文章主要為大家詳細(xì)介紹了C++中動(dòng)態(tài)內(nèi)存分配的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-03-03
  • 淺談C++內(nèi)存分配及變長(zhǎng)數(shù)組的動(dòng)態(tài)分配

    淺談C++內(nèi)存分配及變長(zhǎng)數(shù)組的動(dòng)態(tài)分配

    下面小編就為大家?guī)?lái)一篇淺談C++內(nèi)存分配及變長(zhǎng)數(shù)組的動(dòng)態(tài)分配。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-09-09
  • C++實(shí)現(xiàn)圖書(shū)館系統(tǒng)

    C++實(shí)現(xiàn)圖書(shū)館系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)圖書(shū)館系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 五個(gè)嵌入式C語(yǔ)言中的實(shí)用技巧分享

    五個(gè)嵌入式C語(yǔ)言中的實(shí)用技巧分享

    這篇文章主要和大家分享一下五個(gè)嵌入式C語(yǔ)言中的實(shí)用技巧,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C語(yǔ)言有一定的幫助,需要的可以參考一下
    2022-12-12

最新評(píng)論