30道四则运算

题目如上图,首先我们先确定出所需要的随机数函数rand(),但是这个随机数可能会是一个很大的数,由于我们需要的数为0~100,所以利用余数rand()%100即可得出,由于运算符也是随机的,所以我们可利用switch...case函数来确定运算符即可。但是在具体运算时则需要具体考虑每一种运算。由于不能出现负数,所以减法时被减数要大于减数。除法中则是除数不能为0,分数中分母不能为0,这些情况都要考虑到。

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<math.h>

void menu()
{
    printf("*********************************\n");
    printf("**********1.四则运算*************\n");
    printf("**********2.真分数运算***********\n"); 
    printf("**********3.混合运算*************\n");
    printf("**********4.退出*****************\n");
    printf("*********************************\n");
    printf("请输入选择的题型:");
}


void main()
{
    int a, b, c,d,e,f,h,t, i, m;
    float j, k;
    while (1)
    {
        menu();
        scanf_s("%d", &m);
        switch (m)
        {
        case 1:
        {
            for (i = 0; i < 30; i++)
            {
                a = rand() % 100;
                b = rand() % 100;
                c = rand() % 4;
                switch (c)
                {
                case 0:printf("%d + %d = \n", a, b); break;
                case 1:if (a < b)
                    printf("%d - %d = \n", b, a);
                       else
                           printf("%d - %d = \n", a, b); break;
                case 2:printf("%d * %d = \n", a, b); break;
                case 3:while (b == 0)
                    b = rand() % 100;
                    printf("%d ÷ %d = \n", a, b); break;
                }
            }
            break;
        }
        case 2:
        {
            for (i = 0; i < 30; i++)
            {
                a = rand() % 100;
                c = rand() % 4;
                d = rand() % 100;
                do
                {
                    b = rand() % 100;
                    e = rand() % 100;
                } while (b == 0 || e == 0);
                if (a > b)
                {
                    t = a;
                    a = b;
                    b = t;
                }
                if (d > e)
                {
                    t = d;
                    d = e;
                    e = t;
                }
                switch (c)
                {
                case 0:printf("%d/%d + %d/%d = \n", a, b, d, e); break;
                case 1:
                    j = (float)a / b;
                    k = (float)d / e;
                    if (j < k)
                        printf("%d/%d - %d/%d = \n", d, e, a, b);
                    else
                        printf("%d/%d - %d/%d = \n", a, b, d, e); break;
                case 2:printf("%d/%d * %d/%d = \n", a, b, d, e); break;
                case 3:while (d == 0)
                    d = rand() % 100;
                    printf("%d/%d ÷ %d/%d = \n", a, b, d, e); break;
                }
            }
            break;
        }
        
        case 3:
        {
            for (i = 0; i < 30; i++)
            {
                f = rand() % 100;
                h = rand() % 100;
                c = rand() % 8;

                a = rand() % 100;
                d = rand() % 100;
                do
                {
                    b = rand() % 100;
                    e = rand() % 100;
                } while (b == 0 || e == 0);
                if (a > b)
                {
                    t = a;
                    a = b;
                    b = t;
                }
                if (d > e)
                {
                    t = d;
                    d = e;
                    e = t;
                }
                switch (c)
                {
                case 0:printf("%d + %d = \n", f, h); break;
                case 1:if (f < h)
                    printf("%d - %d = \n", h, f);
                       else
                           printf("%d - %d = \n", f, h); break;
                case 2:printf("%d * %d = \n", f, h); break;
                case 3:while (f == 0)
                    f = rand() % 100;
                    printf("%d ÷ %d = \n", f, h); break;

                case 4:printf("%d/%d + %d/%d = \n", a, b, d, e); break;
                case 5:
                    j = (float)a / b;
                    k = (float)d / e;
                    if (j < k)
                        printf("%d/%d - %d/%d = \n", d, e, a, b);
                    else
                        printf("%d/%d - %d/%d = \n", a, b, d, e); break;
                case 6:printf("%d/%d * %d/%d = \n", a, b, d, e); break;
                case 7:while (d == 0)
                    d = rand() % 100;
                    printf("%d/%d ÷ %d/%d = \n", a, b, d, e); break;
                }
            }
            break;
        }
        case 4:exit(0);
        }
    }
}

 

程序运行结果:

这次编程共花费了我将近一个小时的时间,是因为长时间没有进行编程而且开始之前没有做好分析,还是和以前一样边编程边进行思考,很多东西都是临时决定的,缺乏严谨的问题思考。但是在代码初次编辑完成之后还进行了一些修改,是代码进一步完善。现在未解决的问题还是如何确保所出的题目不重复以及复杂的四则运算(多个数的加减乘除运算以及带括号的数的运算都是没有考虑的,还有待改进。

posted @ 2016-03-12 16:48  阮磊  阅读(203)  评论(1编辑  收藏  举报