四则运算题目生成程序
一.作业规范要求
这个作业属于哪个课程
|----------------------- |------------------------------|
这个作业要求在哪里
| 这个作业的目标 | <
1.可以自动生成四则运算题目并且支持真分数的四则运算,真分数的运算
2.用户可以填写答案
3.程序可以判断对错并生成正确答案
4.可以输出正确率
|
| 学号 | <3180701235> |
二.题目要求
写一个能自动生成小学四则运算题目的程序,然后在此基础上扩展:
1)除了整数以外,还要支持真分数的四则运算,例如:1/6+1/8=7/24
2)程序要求能处理用户的输入,判断对错,累积分数
3)程序支持可以由用户自行选择加、减、乘、除运算
4)使用-n参数控制生成题目的个数,例如Myapp.exe -n 10,将生成10个题目
三.代码提交
java类有:
1.Week
2.number
3.question
Week类
·
import java.util.Scanner;
public class Week {
public static void main (String args[]) {
Scanner scan = new Scanner(System.in);
question q = new question();
int r = Integer.parseInt(args[0]);
int n = Integer.parseInt(args[1]);
int l = Integer.parseInt(args[2]);
int count = 0;
for (int i = 0; i < n; i ++ ) {
q.initialize(r,l);
q.print(l);
if (scan.nextLine().equals (q.res)) {
System.out.println(" !\n");
count ++;
}
else{
System.out.println(" ! : " + q.res + "\n");
}
}
System.out.println(" " + count + " :" + 100 * count / n + "!\n");
scan.close();
}
}
·
number类
·
public class number {
int n;
int d;
}
·
question类
·
import java.util.Stack;
public class question {
char opr[]={'+','-','*','/'};
number oprd[]=new number[100];
char op[]=new char[100];
number result=new number();
String res;
private Object PRT;
public void initialize(int r, int l){
int i,j;
for(i=0;i<1;i++){
oprd[i]=new number();
oprd[i].d=(int) (Math.random()r)+1;
oprd[i].n=(int) (Math.random()r)+1;
if(oprd[i].d>oprd[i].n){
oprd[i].d/=oprd[i].n;
oprd[i].n=1;
}
yf(oprd[i]);
}
for (i=0;i<l-1;i++){
op[i]=opr[(int) (Math.random()*4)];
}
op[l-1]= '=';
Stack<Character>operator=new Stack<Character>();
Stack<number>operand=new Stack<number>();
number x= new number(),y=new number(),z=new number();
i=j=0;
boolean ture = false;
while (ture){
operand.push(oprd[i++]);
if(j==l-1) break;
while(!operator.isEmpty() && PRT(op[j]) <=PRT(operator.peek())){
y=operand.pop();
x=operand.pop();
z=solve(x,y,((Stack<Character>) operator).pop());
operand.push(z);
}
operator.push(op[j++]);
}
while (operand.size() !=1){
y=operand.pop();
x=operand.pop();
z=solve(x,y,operator.pop());
operand.push(z);
}
result=operand.pop();
if(result.n==0) res=String.valueOf(0);
else if(result.d == 1) res =String.valueOf(result.n );
else res =String.valueOf(result.n) + '/' + String.valueOf(result.d);
}
public number solve(number x, number y, char op){
number t1=new number(),t2= new number();
int lcm=LCM(x.d,y.d);
t1.n=x.n * lcm /x.d;
t1.n=y.n * lcm /y.d;
t1.d=t2.d = lcm;
switch(op){
case '+':
t1.n += t2.n;
break;
case '-':
t1.n -= t2.n;
break;
case '':
t1.n = x.n * y.n;
t1.d = x.d * y.d;
break;
case '/':
t1.n = x.n * y.d;
t1.d = x.d * y.n;
break;
default:
break;
}
yf(t1);
return t1;
}
public int PRT(char c){
if(c'+' || c'/') return 0;
else if (c== '' || c=='/') return 1;
else return -1;
}
public void print(int l){
String exp = new String();
for (int i=0;i<1;i++){
if(oprd[i].d ==1) exp+= String.valueOf(oprd[i].n);
else exp +=String.valueOf(oprd[i].n) + '/' + String.valueOf(oprd[i].d);
exp += " " + op[i] + " ";
}
System.out.println(exp);
}
public int GCD (int a,int b){
int gcd=1;
for (int i=1;i<=a;i++){
if(a%i == 0 && b%i == 0){
gcd=i;
}
}
return gcd;
}
public int LCM (int a, int b){
return a * b /GCD(a,b);
}
public void yf (number p){
int gcd=GCD (p.d,p.n);
p.d /=gcd;
p.n /=gcd;
}
}
·
四、程序测试


psp
| PSP2.1 | Personal Software Process Stages | Time (%) Senior Student | Time (%) |
|---|---|---|---|
| Planning | 计划 | 6 | 8 |
| · Estimate | 估计这个任务需要多少时间 | 8 | 10 |
| Development | 开发 | 88 | 125 |
| · Analysis | 需求分析 (包括学习新技术) | 6 | 10 |
| · Design Spec | 生成设计文档 | 5 | 8 |
| · Design Review | 设计复审 | 4 | 6 |
| · Coding Standard | 代码规范 | 3 | 3 |
| · Design | 具体设计 | 15 | 18 |
| · Coding | 具体编码 | 30 | 35 |
| · Code Review | 代码复审 | 7 | 9 |
| · Test | 测试(自我测试,修改代码,提交修改) | 12 | 21 |
| Reporting | 报告 | 6 | 6 |
| · | 测试报告 | 3 | 2 |
| · | 计算工作量 | 2 | 1 |
| · | 并提出过程改进计划 | 3 | 2 |
实验小结
实现真分数四则运算比较困难
浙公网安备 33010602011771号