四则运算

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

int a[100],b[100];

char c[100],sym[4]={'+','-','*','/'};

int answer(int i);

void printanswer(int n);

void main()

{

    int n,o,count;

    int i;

    char r;

       srand((unsigned)time(NULL));

        while(1)

        {

            count=0;

            printf("输入需要的题目数(0退出):");

            scanf("%d",&n);

            if(n==0) exit(0);

            for(i=0;i<n;i++)

            {

            a[i]=rand()%101;

            b[i]=rand()%101;

            c[i]=sym[rand()%4];

            printf("%d%c%d=",a[i],c[i],b[i]);

            scanf("%d",&o);

            if(o==answer(i)) {

                printf("正确!\n");

                count++;

            }

            else printf("错误!答案是%d\n",answer(i));

            }

            printf("答对了%d题\n",count);

        }

}

int answer(int i)

{

    int o;

        if(c[i]==sym[0]) o=a[i]+b[i];

        if(c[i]==sym[1]) o=a[i]-b[i];

        if(c[i]==sym[2]) o=a[i]*b[i];

        if(c[i]==sym[3]) o=a[i]/b[i];

        return o;

}



 

 

你可能感兴趣的:(四则运算)