`
xrtkong
  • 浏览: 15465 次
  • 性别: Icon_minigender_1
  • 来自: 济南
最近访客 更多访客>>
社区版块
存档分类
最新评论

循环实例子:do while;while,for,伪bool型

 
阅读更多

注意:在C语言中,变量必须声明在函数开始的地方

 

 

#include<stdio.h>
#include<ctype.h>


#include<stdlib.h>
#include<time.h>
#define true 1   
#define false 0 
int main(){
   
    char another_game ='Y' ;
    int correct = false;
    int counter = 0 ;
    int sequence_length = 0 ;
    time_t seed = 0 ;
    int number = 0 ;

    time_t now = 0 ;
    int time_taken = 0 ;
    int i =0;
    printf(" \n To play Simple Simon ,");
    printf(" \n Watch carefully ,sa the digits are only displayed for a second !");
    printf(" \n the computer will remove them , and then prompt you ");
    printf(" to enter the same sequence.");
    printf(" \n When you do , you must put spaces between the digits.\n");
    printf(" \n GOOD lUCK !\n Press Enter to play \n");
    scanf("%C",&another_game);

    do{
        correct = true ;
        counter = 0;
        sequence_length = 2 ;
        time_taken = clock();

        while(correct){
            sequence_length += counter++% 3 == 0 ;
            seed = time(NULL);

            now = clock();
            srand((unsigned int)seed);
            //i要在开始的时候声明,在此int i = 0 ; 是不对的
            for(  i = 1 ; i <= sequence_length ; i ++) {
                printf("%d",rand() % 10);
            }

            for(;clock() - now <CLOCKS_PER_SEC;);

            printf("\r");

            for( i =0 ;i <= sequence_length ; i++){
                printf(" ");
            }

            if(counter == 1){
                printf("\n Now you enter the sequence - don't forget"
                    " the spaces \n ");

            }else{
                printf("\r");
            }

            srand((unsigned int)seed);

            for(i = 1 ; i <= sequence_length ; i++){
               
                scanf("%d",&number);

                if(number != rand() %10){
                    correct = false ;
                    break ;
                }
            }
            printf("%s \n",correct ? "Correct!":"Wrong!");
           
        }
        time_taken = (clock() - time_taken);
        printf("\n\n Your score is %d",--counter * 100 / time_taken);
        fflush(stdin);
        printf("\n DO you want to play again (y/n)? ");
        scanf("%c",&another_game);

    }while(toupper(another_game) == 'Y' );
    return 0 ;
}

分享到:
评论

相关推荐

    C# for CSDN 乱七八糟的看不懂

    abstract as base bool break byte case catch char checked class const continue decimal default delegate do double else enum event explicit extern false finally fixed float for foreach goto if implicit ...

    C++大学教程,一本适合初学者的入门教材(part1)

    2.17 do/while重复结构 2.18 break和continue语句 2.19 逻辑运算符 2.20 混淆相等(.==)与赋值(=)运算符 2.21 结构化编程小结 2.22 有关对象的思考:确定问题中的对象 小结 术语 自测练习 自测练习答案 ...

    C++大学教程,一本适合初学者的入门教材(part2)

    2.17 do/while重复结构 2.18 break和continue语句 2.19 逻辑运算符 2.20 混淆相等(.==)与赋值(=)运算符 2.21 结构化编程小结 2.22 有关对象的思考:确定问题中的对象 小结 术语 自测练习 自测练习答案 ...

    HGE_系列教材(1-9)

    息循环等。 HGE 架构在DirectX 8.0 之上,能够跑在大多数的Windows 系统上。 1. 选用HGE 的理由: 1)专业化--- 专注于2D 领域 2)简单化--- 非常容易使用 3)技术优势--- 基于Direct3D API 有较好的性能和特性 4)...

    十天学会ASP.net--我认为ASP.NET比ASP难很多,希望大家做好准备

    Bool型(true/false) DataTime datExample; 日期型("09/19/2002") int intExample; 整数(32位有符号整数) double dblExample; 浮点数(64位双精度浮点数) 二、各种运算: = 赋值运算 + 加法运算 - 减法运算 * 乘法...

    C++MFC教程

    下面的伪代码演示了消息循环的用法: while(1) { id=getMessage(...); if(id == quit) break; translateMessage(...); } 当该程序没有消息通知时getMessage就不会返回,也就不会占用系统的CPU时间。 下图为...

    c#学习笔记.txt

    迭代语句do, for, foreach, in, while 跳转语句break, continue, default, goto, return 异常处理语句throw, try-catch, try-finally Checked 和 Uncheckedchecked, unchecked fixed 语句Fixed lock 语句Lock (1) ...

    C语言深度揭秘

    1.8,do、while、for关键字......................................... 28 1.8.1,break与 continue的区别...........................28 1.8.2,循环语句的注意点.....................................29 1.9,...

    C语言深度剖析 陈正冲

    1.8,do、while、for 关键字................................................................................................... 28 1.8.1,break 与continue 的区别............................................

    C 语言 深 度 剖析

    1.8,do、while、for 关键字................................................................................................... 28 1.8.1,break 与continue 的区别............................................

    C语言深度解剖_word版

    1.8,do、while、for 关键字................................................................................................... 28 1.8.1,break 与continue 的区别............................................

    C语言深度解剖(完美版).pdf

    1.8,do、while、for 关键字................................................................................................... 28 1.8.1,break 与continue 的区别............................................

    C语言深度解剖

    1.8,do、while、for 关键字................................................................................................... 28 1.8.1,break 与 continue 的区别...........................................

    C 语言深度解剖--解开程序员面试笔试的秘密

    1.8,do、while、for 关键 字............................................................................................. ...... 28 1.8.1,break 与 continue 的区 别......................................

Global site tag (gtag.js) - Google Analytics