C语言编程一个小学四则运算练习系统。
发布网友
发布时间:2022-04-23 23:40
我来回答
共1个回答
热心网友
时间:2023-10-13 23:33
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
int comp(int);
int init(void);
int main(int argc, char* argv[])
{
int in;
do
{
init();
scanf("%d",&in);
assert(in>=0&&in<=4);
system("cls");
if (in!=0) comp(in);
}
while (in!=0);
return 0;
}
int init(void)
{
system("cls");
printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n0. EXIT\n");
return 0;
}
int comp(int op)
{
int a,b,c;
float cfd;
printf("Input \'-1\' to exit!\n\n");
srand(time(NULL));
cfd=(float)(a/b);
do
{
a=rand()%100+1;
b=rand()%100+1;
switch(op){
case 1:printf("%d+%d=",a,b);
b=a+b;
break;
case 2:while (a-b<0) {
a=rand()%100+1;
b=rand()%100+1;
}
printf("%d-%d=",a,b);
b=a-b;
break;
case 3:printf("%d*%d=",a,b);
b=a*b;
break;
case 4:while (a<b||cfd*b!=a){
a=rand()%100+1;
b=rand()%100+1;
cfd=a/b;
}
printf("%d/%d=",a,b);
b=a/b;
break;
}
scanf("%d",&c);
b==c?printf("\nRight!\n"):printf("\nWrong! It's %d\n",b);
}while (c!=-1);
return 0;
}