Java实现四则运算

GitHub:https://github.com/Wamelon/operations

组员:黄锐斌,张正浩

1题目:实现一个自动生成小学四则运算题目的命令行程序。

2说明:

自然数:0, 1, 2, …。

  • 真分数:1/2, 1/3, 2/3, 1/4, 1’1/2, …。
  • 运算符:+, −, ×, ÷。
  • 括号:(, )。
  • 等号:=。
  • 分隔符:空格(用于四则运算符和等号前后)。
  • 算术表达式:

e = n | e1 + e2 | e1 − e2 | e1 × e2 | e1 ÷ e2 | (e),

其中e, e1和e2为表达式,n为自然数或真分数。

  • 四则运算题目:e = ,其中e为算术表达式。

 

3需求:

1. 使用 -n 参数控制生成题目的个数,例如

Myapp.exe -n 10

将生成10个题目。

2. 使用 -r 参数控制题目中数值(自然数、真分数和真分数分母)的范围,例如

Myapp.exe -r 10

将生成10以内(不包括10)的四则运算题目。该参数可以设置为1或其他自然数。该参数必须给定,否则程序报错并给出帮助信息。

3. 生成的题目中计算过程不能产生负数,也就是说算术表达式中如果存在形如e1 − e2的子表达式,那么e1 ≥ e2

4. 生成的题目中如果存在形如e1 ÷ e2的子表达式,那么其结果应是真分数

5. 每道题目中出现的运算符个数不超过3个。

6. 程序一次运行生成的题目不能重复,即任何两道题目不能通过有限次交换+×左右的算术表达式变换为同一道题目。例如,23 + 45 = 和45 + 23 = 是重复的题目,6 × 8 = 和8 × 6 = 也是重复的题目。3+(2+1)1+2+3这两个题目是重复的,由于+是左结合的,1+2+3等价于(1+2)+3,也就是3+(1+2),也就是3+(2+1)。但是1+2+33+2+1是不重复的两道题,因为1+2+3等价于(1+2)+3,而3+2+1等价于(3+2)+1,它们之间不能通过有限次交换变成同一个题目。

生成的题目存入执行程序的当前目录下的Exercises.txt文件,格式如下:

1. 四则运算题目1

2. 四则运算题目2

……

其中真分数在输入输出时采用如下格式,真分数五分之三表示为3/5,真分数二又八分之三表示为2’3/8。

7. 在生成题目的同时,计算出所有题目的答案,并存入执行程序的当前目录下的Answers.txt文件,格式如下:

1. 答案1

2. 答案2

特别的,真分数的运算如下例所示:1/6 + 1/8 = 7/24。

8. 程序应能支持一万道题目的生成。

9. 程序支持对给定的题目文件和答案文件,判定答案中的对错并进行数量统计,输入参数如下:

Myapp.exe -e <exercisefile>.txt -a <answerfile>.txt

统计结果输出到文件Grade.txt,格式如下:

Correct: 5 (1, 3, 5, 7, 9)

Wrong: 5 (2, 4, 6, 8, 10)

 

PSP表格

 

 

一、需求分析

     (1)能够自动生成简单运算的式子,可以定义其的数量和大小。

     (2)运算符要包括“+,-,×,÷”,其中还要支持括号和分数的计算。

     (3)程序要能够自己生成答案,并保存在文件夹中。

    (4)可以计算做对以及做错的题数和题号

二、功能设计

通过控制台生成四则运算,进行查重,将式子写入Exercise.txt中,

计算每道题的结果写入Answer.txt

在zuoda.txt中作答,通过比较,在Grade.txt中生成成绩

 

三、设计实现

 

 

四、关键代码

 

//主页
public
class HomePage { public static void main(String[] args)throws Exception { System.out.print("请输入生成的题目数量:"); String input1 = input(); while (!isInt(input1)) { System.out.println("输入错误,请重新输入!"); input1 = input(); } System.out.print("请输入题目的数值范围:"); String input2 = input(); while (!isInt(input2)) { System.out.println("输入错误,请重新输入!"); input2 = input(); } question ques = new question(); ques.Exam(Integer.parseInt(input1), Integer.parseInt(input2)); System.out.println("出题完毕,请到zuoda.txt作答!"); System.out.println("如需批改请按‘1’,任意键退出"); String input3 = input(); if (input3.equals("1")){ CompareAnswer.compare(); System.out.println("批改已完成,请到Grade.txt查看"); } else System.exit(0);
}
//生成式子
public
void Exam(int count,int range) throws Exception { int re=0; String[] strr = new String[count+1]; String[] strr2 = new String[count+1]; String[] repeat =new String[count+1]; FileWriting.fileClear(); String subject; for(int i=0;i<count;i++) { CreateFormula create=new CreateFormula(); subject=create.Creating(range); //去掉多余括号 subject=deleteKuoHao(subject); str=Check.chaChong(subject); strr[i]=str; strr2[i]=subject; int xy=1; for(int h=1;h<i;h++) { if(strr[h].equals(str)) { xy=0; repeat[re]=h+","+strr2[h]+" Repeat "+i+","+subject; re++; break; } } if(xy==0) { i--; continue; } else { String result=Calculate.getResult(subject); FileWriting.writter((i+1)+":"+subject); FileWriting.writter1((i+1)+":"+result); FileWriting.writter2((i+1)+":"); System.out.print("第"+(i+1)+"题,"+subject+"\n"); } }
//生成算式
public class CreateFormula {
    private static char[] operate = { '+', '-', '*', '÷' };

    public String Creating(int range){
        String subject = "";
        Random r = new Random();
        int  operator = r.nextInt(3)+1;//操作符个数
        int kuohao=0;
        boolean flag=true;
        for(int i=1;i<=operator;i++)
        {
            
            Random rand=new Random();
            int temp=rand.nextInt(3)+1;
            if(temp==1)//决定是否加入左括号
            {
                subject+="(";
                kuohao++;
                subject+=this.getNum(range);
                subject += this.getOperate();
            } else if (temp == 2 && flag) {//决定是否加入左括号
                flag=false;
                subject="("+subject;
                kuohao++;
                subject+=this.getNum(range);
                subject += this.getOperate();
            }else if(temp==3&& kuohao!=0){//决定是否加入右括号
                subject+=this.getNum(range);
                subject+=")";
                kuohao--;
                subject += this.getOperate();
            }else{
                subject+=this.getNum(range);
                subject += this.getOperate();
            }
            
        }
        subject+=this.getNum(range);
        for(int i=kuohao;i>0;i--)
        {
            subject=subject+")";
        }
        return subject;    
    }
//计算
public
class Calculate { public static String getResult(String s) { String result=Calculate.PRNCal(s); String[] ary = result.split("/"); int a = Integer.parseInt(ary[0].trim()); int b = Integer.parseInt(ary[1].trim()); if (Math.abs(a) < Math.abs(b)) { result = a + "/" + b; } else if (a == b) { result = 1 + ""; } else if (a == -b) { result = -1 + ""; } else { int yu; int da; if (a == 0 || b == 0) { yu = 0; da = 0; } else { yu = a % b; da = a / b; } if (yu == 0) result = da + ""; else result = da + "'" + Math.abs(yu) + "/" + Math.abs(b); } return result; }
//对比答案
public
class CompareAnswer { private static String filePath2 = "answer.txt"; private static String filePath3 = "zuoda.txt"; public static void compare() throws Exception { String right = ""; String error = ""; int correct=0; int wrong=0; InputStreamReader reader = new InputStreamReader(new FileInputStream( filePath2), "gbk"); BufferedReader br = new BufferedReader(reader); String s = null; InputStreamReader reader1 = new InputStreamReader(new FileInputStream( filePath3), "gbk"); BufferedReader br1 = new BufferedReader(reader1); String s1 = null; int i = 0; right = ""; error = ""; while (((s = br.readLine()) != null) && (s1 = br1.readLine()) != null) { i++; if (s.trim().equals(s1.trim())) { correct++; right = right + i + ","; } else { wrong++; error = error + i + ","; } } String str1 = "Correct:" + correct; if (right.length() <= 1) str1 = str1 + "(" + ")"; else str1 = str1 + "(" + right.substring(0, right.length() - 1)+ ")"; String str2 = "Wrong:" + wrong; if (error.length() <= 1) str2 = str2 + "(" + ")"; else str2 = str2 + "(" + error.substring(0, error.length() - 1)+ ")"; FileWriting.fileClear(); FileWriting.writter3(str1); FileWriting.writter3(str2); } }

 

五、测试运行

 

 

 

 

六、小结

由于平时较少与人合作项目,此次与队友合作花费时间也较长,中间也遇到不少问题,不过遇到问题可以相互讨论,是一个人无法完成的。

 

 

 

 

 

猜你喜欢

转载自www.cnblogs.com/Wamelon/p/9724086.html