二年级四则运算的扩展,可以指定题目数量,并可支持真分数的运算
Posted princehai
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二年级四则运算的扩展,可以指定题目数量,并可支持真分数的运算相关的知识,希望对你有一定的参考价值。
新加的要求:
1.可指定题目数量;
2.支持真分数运算。
代码编写:
#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 = ", a, b);
break;
case 1:if (a < b)
{
d = a;
a = b;
b = d;
}
printf("%d - %d = ", a, b);
break;
case 2:printf("%d * %d = ", a, b);
break;
case 3:
while (b == 0)
{
b = rand() % 100;
}
printf("%d / %d = ", 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 = ", 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 = ", a,b,m,n);
break;
case 2:printf("%d/%d * %d/%d = ", 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 = ", a, b, m, n);
break;
}
}
}
system("pause");
}
#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 = ", a, b);
break;
case 1:if (a < b)
{
d = a;
a = b;
b = d;
}
printf("%d - %d = ", a, b);
break;
case 2:printf("%d * %d = ", a, b);
break;
case 3:
while (b == 0)
{
b = rand() % 100;
}
printf("%d / %d = ", 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 = ", 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 = ", a,b,m,n);
break;
case 2:printf("%d/%d * %d/%d = ", 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 = ", a, b, m, n);
break;
}
}
}
system("pause");
}
运行结果截图:
以上是关于二年级四则运算的扩展,可以指定题目数量,并可支持真分数的运算的主要内容,如果未能解决你的问题,请参考以下文章