四则运算

Posted dearqiqi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了四则运算相关的知识,希望对你有一定的参考价值。

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
    int a,b,c,i,d;
    srand((time(NULL)));
    for(i=0;i<20;i++)
    {
    a=rand()%100;
    b=rand()%100;
    c=rand()%4;

        switch(c)
        {
    case 0:
        printf("%d+%d=\\n",a,b);
        printf("请输入正确答案:\\n");
        int e;
        scanf("%d",&e);
        if(a+b==e)
        printf("恭喜您回答正确!\\n");
        else
            printf("您回答错误,正确答案是%d\\n",a+b);
        break;
    case 1:
        if(a<b)
        {
            d=a;
            a=b;
            b=d;
        }
        printf("%d-%d=\\n",a,b);
        printf("请输入正确答案:\\n");
        int f;
        scanf("%d",&f);
        if(a-b==f)
            printf("恭喜您回答正确!\\n");
        else
            printf("您回答错误,正确答案是%d\\n",a-b);
        break;

    case 2:
        printf("%d*%d=\\n",a,b);
        printf("请输入正确答案:\\n");
        int g;
        scanf("%d",&g);
        if(a*b==g)
            printf("恭喜您回答正确!\\n");
        else
            printf("您回答错误,正确答案是%d\\n",a*b);
        break;
    case 3:
        if(a*b==0)
        {
            a++;
            b++;
            if(a%b==0)
            {
                 printf("%d/%d=\\n",a,b);
                 printf("请输入正确答案:\\n");
                 int h;
        scanf("%d",&h);
        if(a/b==h)
            printf("恭喜您回答正确!\\n");
        else
            printf("您回答错误,正确答案是%d\\n",a/b);
        break;
            }
            else
                {
                    a++;
                    while(a%b!=0)
                    {
                          a++;
                    }
                    printf("%d/%d=\\n",a,b);
                          printf("请输入正确答案:\\n");
                    int j;
                    scanf("%d",&j);
                    if(a/b==j)
                        printf("恭喜您回答正确!\\n");
                    else
                        printf("您回答错误,正确答案是%d\\n",a/b);

                }

        }
        else
        {
            if(a%b==0)
            {
                 printf("%d/%d=\\n",a,b);
                 printf("请输入正确答案:\\n");
                 int i;
        scanf("%d",&i);
        if(a/b==i)
            printf("恭喜您回答正确!\\n");
        else
            printf("您回答错误,正确答案是%d\\n",a/b);
        break;
            }
            else
                {
                    a++;
                    while(a%b!=0)
                    {
                          a++;
                    }
                    printf("%d/%d=\\n",a,b);
                          printf("请输入正确答案:\\n");
                    int k;
                    scanf("%d",&k);
                    if(a/b==k)
                        printf("恭喜您回答正确!\\n");
                    else
                        printf("您回答错误,正确答案是%d\\n",a/b);

                }

        }


       }
    }
return 0;
}
技术分享图片

 
































































































































以上是关于四则运算的主要内容,如果未能解决你的问题,请参考以下文章

C语言中随机产生四则运算符号

用C语言随机产生三个数,三个数之间进行四则运算,四则运算也随机产生

用c++语言编写四则运算计算器

java四则运算

定义一个方法,实现四则运算的功能。java

四则运算2开发简介