20172315『Java程序设计』课程 结对编程练习_四则运算第二周阶段总结

相关过程截图

中缀转后缀及后缀计算测试截图

题目生成截图

计算正确率截图

关键代码解释

将中缀表达式转换为后缀表达式并对后缀表达式进行计算

import java.util.Stack;
import java.util.regex.Pattern;
public class StringToArithmetic {

    public StringToArithmetic() {

    }

    // 将中缀表达式字符串计算得到结果
    public static double stringToArithmetic(String string) {
        return suffixToArithmetic(infixToSuffix(string));
    }



    // 将中缀表达式转换为后缀表达式
    public static String infixToSuffix(String exp) {

        Stack<Character> s = new Stack<Character>();
        // 要输出的后缀表达式字符串
        String suffix = "";
        int length = exp.length(); // 输入的中缀表达式的长度
        for (int i = 0; i < length; i++) {
            char temp;

            char ch = exp.charAt(i);
            switch (ch) {

                case ' ':
                    break;

                case '(':
                    s.push(ch);
                    break;


                case '+':
                case '-':
                    while (s.size() != 0) {
                        temp = s.pop();
                        if (temp == '(') {

                            s.push('(');
                            break;
                        }
                        suffix += temp;
                    }

                    s.push(ch);
                    break;


                case '×':
                case '÷':
                    while (s.size() != 0) {
                        temp = s.pop();

                        if (temp == '+' || temp == '-' || temp == '(') {
                            s.push(temp);
                            break;
                        } else {
                            suffix += temp;
                        }
                    }

                    s.push(ch);
                    break;


                case ')':

                    while (!s.isEmpty()) {
                        temp = s.pop();
                        if (temp == '(') {
                            break;
                        } else {
                            suffix += temp;
                        }
                    }
                    break;

                default:
                    suffix += ch;
                    break;
            }

        }
        while (s.size() != 0) {
            suffix += s.pop();
        }

        return suffix;
    }

    public static double suffixToArithmetic(String exp) {

        Pattern pattern = Pattern.compile("\\d+||(\\d+\\.\\d+)");

        String[] strings = exp.split("");
        Stack<Double> stack = new Stack<Double>();
        for (int i = 0; i < strings.length; i++) {

            if (strings[i].equals("")) {
                continue;
            }

            if (pattern.matcher(strings[i]).matches()) {
                stack.push(Double.parseDouble(strings[i]));
            }

            else {

                double y = stack.pop();
                double x = stack.pop();

                stack.push(calculate(x, y, strings[i]));
            }
        }

        return stack.pop();
    }

    private static Double calculate(double x, double y, String string) {
        // TODO Auto-generated method stub

        if (string.trim().equals("+")) {
            return x + y;
        }
        if (string.trim().equals("-")) {
            return x - y;
        }
        if (string.trim().equals("×")) {
            return x * y;
        }
        if (string.trim().equals("÷")) {
            return x / y;
        }
        return (double) 0;
    }

创建题目类

    public class createquestions {
    public static String create(int level){
        String add = "+", sub = "-", multi = "×", div = "÷";
        String result="";
        int r1=(int)(Math.random()*9+1);
        int r2=(int)(Math.random()*9+1);
        int r3=(int)(Math.random()*9+1);
        //三个整数
        int fenzi1=(int)(Math.random()*9+1);
        int fenmu1=(int)(Math.random()*9+1);
        String fenshu1=fenzi1+"/"+fenmu1;
        int fenzi2=(int)(Math.random()*9+1);
        int fenmu2=(int)(Math.random()*9+1);
        String fenshu2=fenzi2+"/"+fenmu2;
        int fenzi3=(int)(Math.random()*9+1);
        int fenmu3=(int)(Math.random()*9+1);
        String fenshu3=fenzi3+"/"+fenmu3;
        //三个分数
       int suiji1=(int)(Math.random()*4);
        //第一个运算符
        int suiji11=(int)(Math.random()*3);
        //生成括号
        int suiji2=(int)(Math.random()*4);
        //第二个运算符
        if (level>=1) {
            if(suiji11==0&&level==2&&suiji1!=0&&suiji1!=1)
                result+="(";
            if (level==3)
                result+=fenshu1;
            else
                result+=r1;
            if (suiji1 == 0)
                result+=add;
            if (suiji1 == 1)
                result+=sub;
            if (suiji1==2)
                result+=multi;
            if(suiji1==3)
                result+=div;
            if(suiji11==1&&level==2&&suiji2!=0&&suiji2!=1)
                result+="(";
            if (level==3)
                result+=fenshu2;
            else
                result+=r2;
            if(suiji11==0&&level==2&&suiji1!=0&&suiji1!=1)
                result+=")";
        }
        if (level>=2){
            if (suiji2==0)
                result+=add;
            if (suiji2 == 1)
                result+=sub;
            if (suiji2==2)
                result+=multi;
            if(suiji2==3)
                result+=div;
            if (level==3)
                result+=fenshu3;
            else
                result+=r3;
            if(suiji11==1&&suiji2!=0&&suiji2!=1)
                result+=")";
        }
        return result;
    }

main类

import java.util.Scanner;

public class calculate {
    public static void main(String[] args) {
        System.out.println("输入你要生成的题数");
        Scanner scan = new Scanner(System.in);
        int tishu = scan.nextInt();
        System.out.println("输入你要生成题目的等级(输入1或2或3)");
        int dengji = scan.nextInt();
        //调用一个循环
        double answer;
        int zhengquelv = 0;
        double[] daan = new double[tishu];
        switch (dengji) {
            case 1:
                for (int i = 0; i < tishu; i++) {
                    String a = createquestions.create(1);
                    String b = a + "=";
                    System.out.print(b);
                    answer = scan.nextDouble();
                    daan[i] = StringToArithmetic.stringToArithmetic(a);
                    if (answer == daan[i])
                        zhengquelv++;
                }break;
            case 2:
                for (int i = 0; i < tishu; i++) {
                    String a = createquestions.create(2);
                    String b = a + "=";
                    System.out.print(b);
                    answer = scan.nextInt();
                    daan[i] = StringToArithmetic.stringToArithmetic(a);
                    if (answer == daan[i])
                        zhengquelv++;
                }break;
            case 3:
                for (int i = 0; i < tishu; i++) {
                    String a = createquestions.create(3);
                    String b = a + "=";
                    System.out.print(b);
                    answer = scan.nextInt();
                    daan[i] = StringToArithmetic.stringToArithmetic(a);
                    if (answer == daan[i])
                        zhengquelv++;

                }break;

        }

        if(dengji<1||dengji>3)
            System.out.println("题目等级输入错误");
        if (dengji >= 1&&dengji <= 3) {
            System.out.println("答案为");
            for (
                    double daanwei : daan)
                System.out.print(daanwei + " ");
            System.out.println("正确率为" + zhengquelv + "/" + tishu);
        }
    }
}

遇到的困难及解决方法

关于判断题目的正确率出现了一些小问题

经过组内商讨,得出了解决办法

写出项目中自己负责的部分

对中缀转后缀和后缀的计算类的编写

个人贡献度划分(按百分比给小组各成员划分贡献度,各成员贡献之和为100%)

20172315胡智韬30%
20172312彭霖30%
20172318陆大岳40%

对结对的小伙伴做出评价给出小伙伴博客链接(重点指出需要改进的地方)

彭霖
陆大岳

给出项目的码云链接

给出小组结对编程照片

posted @ 2018-05-08 23:04  大雪将烬  阅读(146)  评论(1编辑  收藏  举报