最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C語言循環(huán)結構與時間函數用法實例教程

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

本文實例展示了C語言循環(huán)結構與時間函數用法,對于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語言循環(huán)結構域時間函數的認識。

相關文章

  • 人臉檢測中AdaBoost算法詳解

    人臉檢測中AdaBoost算法詳解

    這篇文章主要為大家詳細介紹了人臉檢測中AdaBoost算法的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • C++中map和set封裝實現示例

    C++中map和set封裝實現示例

    我們知道,map與set所使用的都是紅黑樹,下面這篇文章主要給大家介紹了關于C++中map和set封裝實現的相關資料,文中通過圖文以及實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-02-02
  • VSCode添加頭文件(C/C++)的實現示例

    VSCode添加頭文件(C/C++)的實現示例

    這篇文章主要介紹了VSCode添加頭文件(C/C++)的實現示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-08-08
  • C語言代碼實現簡易掃雷

    C語言代碼實現簡易掃雷

    這篇文章主要為大家詳細介紹了C語言代碼實現簡易掃雷,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • 深入遍歷二叉樹的各種操作詳解(非遞歸遍歷)

    深入遍歷二叉樹的各種操作詳解(非遞歸遍歷)

    本篇文章是對遍歷二叉樹的各種操作進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • 線程按指定順序輸出字符到數組的實例代碼

    線程按指定順序輸出字符到數組的實例代碼

    這篇文章主要介紹了線程按指定順序輸出字符到數組的實例代碼,需要的朋友可以參考下
    2014-02-02
  • C++實現教務管理系統(tǒng)

    C++實現教務管理系統(tǒng)

    這篇文章主要為大家詳細介紹了C++實現教務管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • C++中臨時對象的常見產生情況及其解決的方案

    C++中臨時對象的常見產生情況及其解決的方案

    這篇文章主要是探討常見的臨時對象產生的情況,及其如何避免和解決這種臨時對象產生的方式。具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • 詳細分析C++ 異常處理

    詳細分析C++ 異常處理

    這篇文章主要介紹了C++ 異常處理的的相關資料,文中示例代碼非常詳細,幫助大家更好的理解和學習,感興趣的朋友可以了解下
    2020-06-06
  • C++中的模板類繼承和成員訪問問題

    C++中的模板類繼承和成員訪問問題

    這篇文章主要介紹了C++中的模板類繼承和成員訪問問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11

最新評論

赤峰市| 宝山区| 禄劝| 仲巴县| 华蓥市| 普陀区| 莱芜市| 青阳县| 根河市| 定西市| 梨树县| 蒙自县| 高密市| 洪洞县| 宁陵县| 郓城县| 沁水县| 勃利县| 武山县| 贺州市| 阿荣旗| 桃园市| 永安市| 黎川县| 合水县| 江津市| 烟台市| 微博| 耿马| 泊头市| 陆河县| 金堂县| 龙海市| 格尔木市| 胶南市| 富锦市| 揭阳市| 榆中县| 运城市| 杭锦后旗| 临桂县|