首页 > 代码库 > 四则运算
四则运算
题目要求: • 能自动生成小学四则运算题目(注意是给小学生用的) •除了整数外,还要支持真分数的四则运算
题目分析:1.由于是给小学生用的,所以要求运算的时候不能有负数,因此,减法运算时,前面的数一定要大于后面的数。
2.除法运算的规则,分母不能为0。
3.题目要求支持真分数,由真分数的概念知,分子不能大于分母的数。
4.因为题目要求是随机30道题,所以运用随机生成数函数srand()和rand()。
#include<stdio.h>
#include<Windows.h>
#include<time.h>
void main()
{
int a, b, c, d,i,m,n;
float p, q;
srand(unsigned( time(NULL)));
for (i = 0; i < 30; i++)
{
a = rand() % 100;
b = rand() % 100;
c = rand() % 5;
switch (c)
{
case 0: printf("%d + %d =\n", a, b);
break;
case 1:if (a < b)
{
d = a;
a = b;
b = d;
}
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;
case 4:
a = rand() % 100;
b = rand() % 100;
m = rand() % 100;
n = rand() % 100;
c = rand() %4 ;
while (b == 0)
{
b = rand() % 100;
}
while (n == 0)
{
n = rand() % 100;
}
if (a > b)
{
d = a;
a = b;
b = d;
}
if (m > n)
{
d = m;
m = n;
n = d;
}
switch (c)
{
case 0:printf("%d/%d + %d/%d =\n", a, b, m, n);
break;
case 1:
p = (float)a / (float)b;
q = (float)m / (float)n;
if (p < q)
{
d = a;
a = m;
m = d;
d = b;
b = n;
n = d;
}
printf("%d/%d - %d/%d =\n", a,b,m,n);
break;
case 2:printf("%d/%d * %d/%d =\n", a, b, m, n);
break;
case 3:
a = rand() % 100;
b = rand() % 100;
m = rand() % 100;
n = rand() % 100;
c = rand() % 4;
while (m == 0)
{
m = rand() % 100;
}
printf("%d/%d / %d/%d =\n", a, b, m, n);
break;
}
}
}
system("pause");
}
四则运算