首页 > 代码库 > 四则运算题目生成程序
四则运算题目生成程序
需求分析:
对于刚学习完四则运算的小朋友们来说,是需要一定的练习的。练习过程中能及时发现运算算错是很重要的。
功能设计:
能随机产生100以内简单的加减乘除,以及分子分母均不超过20的分数运算。并计算正确率。
程序设计:
基于分数运算编写的函数:
Fraction getResult2(Fraction num1, Fraction num2, char signal){
Fraction result; int gcd; int gbs;
gcd=maxGys(num1.b, num2.b);
gbs = num1.b*num2.b / gcd;
num1.a = gbs / num1.b*num1.a;
num2.a = gbs / num2.b*num2.a;
num1.b = gbs;
num2.b = gbs;
switch (signal)
{
case ‘+‘:
result.a = num1.a + num2.a;
result.b = gbs; break;
case ‘-‘:
result.a = num1.a - num2.a;
result.b = gbs; break;
case ‘*‘:
result.a = num1.a * num2.a;
result.b = num1.b * num2.b; break;
case ‘/‘:
result.a = num1.a * num2.b;
result.b = num1.b * num2.a; break;
}
int tot = maxGys(result.a, result.b);
result.a = result.a / tot;
result.b = result.b / tot;
return result;
}
基于整数运算编写的函数:
void takeTest1()
{
int giveResult;
int num1;
int num2;
char signal;
srand((unsigned)time(NULL));
signal = getSignal();
num1 = rand() % 100 + 1;
num2 = rand() % 100 + 1;
if (signal == ‘-‘)
{
if (num1<num2)
{
int temp;
temp = num1;
num1 = num2;
num2 = temp;
}
}
if (signal == ‘/‘)
{
if (num2 == 0)
{
int temp;
temp = num1;
num1 = num2;
num2 = temp;
}
}
printf("%d%c%d=",num1,signal, num2);
scanf("%d", &giveResult);
fflush(stdin);
if (getResult1(num1, num2, signal) == giveResult){
printf("right!\n");
right++;
}
else
{
printf("正确答案:%d\n", getResult1(num1, num2, signal));
}
}
主函数:
int main()
{
int num;
float m;
printf("来几题\n");
scanf("%d", &num);
for (int i = 0; i < num; i++)
{
int c = rand() % 2 + 1;
switch (c)
{
case 1: takeTest1(); break;
case 2: takeTest2(); break;
}
}
m = (float)right / (float)num;
printf("正确率为:%f\n", m);
system("pause");
return 0;
}
截图:
PSP:
小结:
感觉有很多重复性代码,功能不够完善,可能存在隐患。
码市链接:
https://git.oschina.net/hanonymous/calculator-four
四则运算题目生成程序