标题:请教一个关于随机数和计时的问题
只看楼主
grmmylbs
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:54
帖 子:1409
专家分:5845
注 册:2016-2-14
得分:0 
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

enum
{
    sum = 0,
    dec,
    mult,
    divi
};
int main()
{
    int a, b, c;
    int opration;
    int loop = 0, flag = 0;
    int count = 0;
    time_t starttime;
    time_t endtime;
    srand(time(&starttime));
    
    while (loop < 10)
    {
        flag = 0;
        opration = rand() % 4;
        while (1)
        {
            a = rand() % 100;
            b = rand() % 100;

            switch (opration)
            {
            case sum:
            {
                if ((a + b) < 100)
                {
                    printf("%d + %d = ", a, b);
                    scanf("%d", &c);
                    if (c == a + b)
                    {
                        count++;
                    }
                    flag = 1;
                }
                break;
            }
            case dec:
            {
                if (((a - b) < 100) && (a > b))
                {
                    printf("%d - %d = ", a, b);
                    scanf("%d", &c);
                    if (c == a - b)
                    {
                        count++;
                    }
                    flag = 1;
                }
                else if (((b - a) < 100) && (a < b))
                {
                    printf("%d - %d = ", b, a);
                    scanf("%d", &c);
                    if (c == b - a)
                    {
                        count++;
                    }
                    flag = 1;
                }
                break;
            }
            case mult:
            {
                if (((a * b) < 100) && (a > 1) && (b > 1))    //乘数大于1,可以根据要求修改
                {
                    printf("%d × %d = ", a, b);
                    scanf("%d", &c);
                    if (c == a * b)
                    {
                        count++;
                    }
                    flag = 1;
                }
                break;
            }
            case divi:
            {
                if ((a > b) && (b > 1) && (a % b == 0))    //除数数大于1,可以根据要求修改
                {
                    printf("%d ÷ %d = ", a, b);
                    scanf("%d", &c);
                    if (c == a / b)
                    {
                        count++;
                    }
                    flag = 1;
                }
                else if ((a < b) && (a > 1) && (b % a == 0))
                {
                    printf("%d ÷ %d = ", b, a);
                    scanf("%d", &c);
                    if (c == b / a)
                    {
                        count++;
                    }
                    flag = 1;
                }
                break;
            }
            default:
                break;
            }
            if (flag)
            {
                break;
            }
        }
        loop++;
    }
    time(&endtime);
    printf("使用时间%ds\n", endtime- starttime);
    printf("作对%d题\n", count);
}
2016-06-20 14:12
惧惧
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2016-6-19
得分:0 
回复 11楼 grmmylbs
time_t  和  clock_t 有什么区别呢?
2016-06-20 14:18



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-466128-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015576 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved