a.需求分析:
自动生成小学四则运算题目的命令行 “软件”,满足以下需求:
- 除了整数以外,还要支持真分数的四则运算,真分数的运算,例如:1/6 + 1/8 = 7/24
- 运算符为 +, −, ×, ÷
- 并且要求能处理用户的输入,并判断对错,打分统计正确率。
- 要求能处理用户输入的真分数, 如 1/2, 5/12 等
- 使用 -n 参数控制生成题目的个数,例如执行下面命令将生成10个题目
Myapp.exe -n 10
b.功能设计:
生成整数四则运算和分数四则运算,并判断对错,打分统计正确率
c.设计实现:
分函数zs()主要负责整数四则运算,随机产生两个随机数和随机运算符并计算;分函数gys是求最大公约数函数;分函数fs()主要负责分数四则运算随机产生两个随机数和随机运算符并调用第三个分函数gys()进行计算;主函数main()主要实现随机调用两个分函数zs()和fs()并统计正确率
d.代码说明:
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
#include "string.h"
int
gcd(
int
x,
int
y)
{
int
r;
r=x%y;
while
(r!=0)
{
x=y;
y=r;
r=x%y;
}
return
y;
}
int
zhengshu(){
int
left,mid,right,result,int_number;
srand
((unsigned)
time
(NULL));
left=
rand
()%10;
mid=
rand
()%4;
right=
rand
()%10;
printf
(
"%d"
,left);
switch
(mid)
{
case
(0):result=left+right;
printf
(
" + "
);
break
;
case
(1):result=left-right;
printf
(
" - "
);
break
;
case
(2):result=left*right;
printf
(
" ×"
);
break
;
case
(3):result=left/right;
printf
(
" ÷"
);
break
;
}
printf
(
"%d ="
,right);
scanf
(
"%d"
,&int_number);
if
(int_number == result)
{
printf
(
"回答正确! 下一题:
"
);
score=score+1;
}
else
{
printf
(
"回答错误! 下一题:
"
);
}
}
int
fenshu(){
int
left_x,left_y,mid,right_x,right_y,x,y,GYS,int_number1,int_number2;
srand
((unsigned)
time
(NULL));
do
{
left_x=
rand
()%10+1;
left_y=
rand
()%10+1;
mid=
rand
()%4;
right_x=
rand
()%10+1;
right_y=
rand
()%10+1;
}
while
(!(left_x
printf
(
"%d/%d"
,left_x,left_y);
switch
(mid)
{
case
(0):x=left_x*right_y+left_y*right_x;
y=left_y*right_y;
printf
(
" + "
);
break
;
case
(1):x=left_x*right_y-left_y*right_x;
y=left_y*right_y;
printf
(
" - "
);
break
;
case
(2):x=left_x*right_x;
y=left_y*right_y;
printf
(
" ×"
);
break
;
case
(3):x=left_x*right_y;
y=left_y*right_x;
printf
(
" ÷"
);
break
;
}
printf
(
"%d/%d ="
,right_x,right_y);
GYS=gcd(x,y);
x=x/GYS;
y=y/GYS;
scanf
(
"%d/%d"
,&int_number1,&int_number2);
if
(int_number1==x&&int_number2==y)
{
printf
(
"回答正确! 下一题:
"
);
score=score+1;
}
else
{
printf
(
"回答错误! 下一题:
"
);
}
}
int
main(){
int
h,count;
int
score=0;
printf
(
"请输入题目数?"
);
scanf
(
"%d"
,&count);
for
(
int
i=0;i
{
h=
rand
()%2;
if
(h==0)
{zs();}
else
{fs();}
}
printf
(
"正确率为:%lf"
,score*1.0/count);
}