时间函数举例4:猜数字游戏。

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
int main()
{   /*时间函数4:猜数字游戏*/
    int i,guess;
    char c;
    double var;
    time_t a,b;
    clock_t start,end;
    srand(time(NULL));
    printf("\ndo you want to play a game.('y'or'n')\n");
    loop:
    while((c=getchar())=='y')
    {
        i=rand()%100;
        printf("please input the number you guess.\n");
        scanf("%d",&guess);
        start=clock();
        a=time(NULL);
        while(guess!=i)
        {
            if (i>guess)
            {
                printf("\nplease input a little bigger.\n");
                scanf("%d",&guess);
            }
            else
            {
                printf("\nplease input a little smaller.\n");
                scanf("%d",&guess);
            }
        }
        end=clock();
        b=time(NULL);      //时间的单位是浮点型的
        printf("\1\1:you took the time is %6.3fseconds\n",var=(double)(end-start)/18.2);
        printf("\1\1:you took the time is %6.3fseconds\n",difftime(b,a));
        if(var<15)
            printf("\1\1:you are clever.\n");
            else if(var<25)
                printf("\1\1:you are normal.\n");
                else
                    printf("\1\1:you are stupid.\n");
        printf("\1\1:congratulations to you.\1\1\n");
        printf("\1\1:you guess the number is %d\1\1\n",i);
     }
     printf("do you want to play it again.(\"y\"or\"n\")\n");
     if ((c=getch())=='y')  //getch  getchar是不同的
     goto loop;
     return 0;
}

原文地址:https://www.cnblogs.com/wangjingyuwhy/p/2943370.html