结对编程1

 

团队成员:   我:陈俊达 201421122076

     伙伴:林仙平 201421122079                                        

需求分析:

  1、在作业1的基础上加入GUI。      

       2、记录用户的对错总数程序退出再启动的时候能把以前的对错数量保存并在此基础上增量计算。

  3、有计时功能,能显示用户开始答题后的消耗时间。

      4、用户可以根据自己需要选择语言(只有中文简体与英文两种可选)。

思维导图:

                

代码规范:

           

 

项目截图 :

                   

代码展示:

package GUI;

public class CountExp {
    //计算表达式答案
    public static String countAnswer(String exp) {
        int result = 0;
        int a1 = 0, a2 = 0;
        int b = 0;// 为0说明符号后面的数为负数,1 为正数
        int bb = 0;// 符号后面的数字长度-1
        int w = 0;// w用来表示符号前面打数字打位数-1
        int ww = 0;// ww为0说明算术符号前面的数是正数,为1说明是负数
        String exp1 = "";// 接收去括号后的表达式
        String exp2 = "";// 接收去乘除号后打表达式
        String exp3 = "";// 用来接收分数
        // 去括号
        for (int i = 0; i < exp.length(); i++) {
            if (exp.charAt(i) == '(') {
                int i1 = 0;
                int i2 = 0;
                // 判断算术符号前面的数是否多位
                if (exp.charAt(i + 2) != '-' && exp.charAt(i + 2) != '+') {
                    i1 = i1 + 1;
                    if (exp.charAt(i + 3) != '-' && exp.charAt(i + 3) != '+')
                        i1 = i1 + 1;
                }
                // 判断算术符号后面的数是否多位
                if (exp.charAt(i + i1 + 4) != ')') {
                    i2 = i2 + 1;
                    if (exp.length() > (i + i1 + 5)
                            && exp.charAt(i + i1 + 5) != ')')
                        i2 = i2 + 1;
                }
                a1 = Integer.parseInt(exp.substring(i + 1, i + 2 + i1));
                a2 = Integer.parseInt(exp
                        .substring(i + 3 + i1, i + 4 + i1 + i2));
                if (exp.charAt(i + 2) == '+')
                    exp1 = exp1 + (a1 + a2);
                else
                    exp1 = exp1 + (a1 - a2);
                i = i + 4 + i1 + i2;

            } else
                exp1 = exp1 + exp.charAt(i);
        }
        // 循环先算公式里的乘跟除
        for (int j = 0; j < exp1.length(); j++) {
            if (exp1.charAt(j) == '*') {
                w = 0;// w用来表示符号前面打数字的位数-1
                ww = 0;// ww为0说明算术符号前面的数是正数,为1说明是负数
                if ((j - 2) > 0 && exp1.charAt(j - 2) != '-'
                        && exp1.charAt(j - 2) != '+'
                        && exp1.charAt(j - 2) != '*'
                        && exp1.charAt(j - 2) != '÷') {
                    w = w + 1;
                    if ((j - 2 - w) > 0 && exp1.charAt(j - 2 - w) != '-'
                            && exp1.charAt(j - 2 - w) != '+'
                            && exp1.charAt(j - 2) != '*'
                            && exp1.charAt(j - 2) != '÷')
                        w = w + 1;
                }

                if ((j - 2 - w) > 0
                        && exp1.charAt(j - 2 - w) == '-'
                        && ((j - 3 - w) > 0 && exp1.charAt(j - 3 - w) == '-' || (exp1
                                .charAt(j - 3 - w) == '+')))
                    ww = ww + 1;

                a1 = Integer.parseInt(exp1.substring(j - w - 1, j));
                if (ww == 1)
                    a1 = 0 - a1;
                b = 0;// 为0说明符号后面的数为负数,1 为正数
                bb = 0;// 符号后面的数字长度-1
                // 判断算术符号后面的数是否是负数
                if (exp1.charAt(j + 1) == '-')
                    b = b + 1;
                // 判断算术符号后面的数是否多余一位
                if (exp1.length() > (j + 2 + b)
                        && (exp1.charAt(j + 2 + b) != '-'
                                && exp1.charAt(j + 2 + b) != '+'
                                && exp1.charAt(j + 2 + b) != '*' && exp1
                                .charAt(j + 2 + b) != '÷')) {
                    bb = bb + 1;
                    if (exp1.length() > (j + 3 + b)
                            && (exp1.charAt(j + 3 + b) != '-'
                                    && exp1.charAt(j + 3 + b) != '+'
                                    && exp1.charAt(j + 3 + b) != '*' && exp1
                                    .charAt(j + 3 + b) != '÷'))
                        bb = bb + 1;
                }
                a2 = Integer
                        .parseInt(exp1.substring(j + 1 + b, j + 2 + b + bb));
                if (b == 1)
                    a2 = 0 - a2;
                exp2 = exp2.substring(0, j - 1 - w - ww);
                exp2 = exp2 + (a1 * a2);
                exp2 = exp2 + exp1.substring(j + 2 + b + bb, exp1.length());
                exp1 = exp2;
                exp2 = "";
                j = -1;
                continue;

            } else if (exp1.charAt(j) == '÷') {// 遇到除号 ,则进行除法运算,结果为分数
                w = 0;// w用来表示符号前面打数字打位数-1
                ww = 0;// ww为0说明算术符号前面的数是正数,为1说明是负数
                if ((j - 2) > 0 && exp1.charAt(j - 2) != '-'
                        && exp1.charAt(j - 2) != '+') {
                    w = w + 1;
                    if (exp1.charAt(j - 2 - w) != '-'
                            && exp1.charAt(j - 2 - w) != '+')
                        w = w + 1;
                }
                if ((j - 2 - w) > 0
                        && exp1.charAt(j - 2 - w) == '-'
                        && (exp1.charAt(j - 3 - w) == '-' || (exp1.charAt(j - 3
                                - w) == '+')))
                    ww = ww + 1;
                a1 = Integer.parseInt(exp1.substring(j - w - 1, j));

                b = 0;// 为0说明符号后面的数为负数,1 为正数
                bb = 0;// 符号后面的数字长度-1
                // 判断算术符号后面的数是否是负数
                if (exp1.charAt(j + 1) == '-')
                    b = b + 1;
                // 判断算术符号后面的数是否多余一位
                if (exp1.length() > (j + 2 + b)
                        && (exp1.charAt(j + 2 + b) != '-'
                                && exp1.charAt(j + 2 + b) != '+'
                                && exp1.charAt(j + 2 + b) != '*' && exp1
                                .charAt(j + 2 + b) != '÷')) {
                    bb = bb + 1;
                    if (exp1.length() > (j + 3 + b)
                            && (exp1.charAt(j + 3 + b) != '-'
                                    && exp1.charAt(j + 3 + b) != '+'
                                    && exp1.charAt(j + 3 + b) != '*' && exp1
                                    .charAt(j + 3 + b) != '÷'))
                        bb = bb + 1;
                }
                a2 = Integer
                        .parseInt(exp1.substring(j + 1 + b, j + 2 + b + bb));

                if (a1 % a2 == 0) {
                    exp2 = "" + (a1 / a2);
                    return exp2;
                } else if (a1 < a2) {
                    exp2 = "" + a1 + "/" + a2;
                    return exp2;
                } else {
                    int e1 = a1 / a2;
                    int e2 = a1 - a2 * e1;
                    exp2 = "" + e1 + "’" + e2 + "/" + a2;
                    return exp2;
                }

            } else
                exp2 = exp2 + exp1.charAt(j);
        }
        int fs = 10;// 用来记录表达式括号的位置

        // 进行加减运算
        for (int xx = 0; xx < exp2.length(); xx++) {
            if (exp2.charAt(xx) == '/')
                fs = xx;
        }
        int aa = 0;// 用来暂a1,a2的值
        if (fs == 10) {
            for (int fss = 1; fss < exp2.length(); fss++) {
                if (exp2.charAt(fss) == '+') {
                    b = 0;// 符号后面的数字长度-1
                    bb = 0;// 为0说明符号后面的数为负数,1 为正数
                    w = 0;// w用来表示符号前面打数字打位数-1
                    ww = 0;// ww为0说明算术符号前面的数是正数,为1说明是负数
                    if (exp2.charAt(0) == '-')
                        bb = bb + 1;
                    b = fss - bb;
                    a1 = Integer.parseInt(exp2.substring(0, fss));
                    if ((fss + 2) < exp2.length()
                            && exp2.charAt(fss + 2) != '+'
                            && exp2.charAt(fss + 2) != '-') {
                        w = w + 1;
                        if ((fss + 3) < exp2.length()
                                && exp2.charAt(fss + 3) != '+'
                                && exp2.charAt(fss + 3) != '-') {
                            w = w + 1;
                            if ((fss + 4) < exp2.length()
                                    && exp2.charAt(fss + 4) != '+'
                                    && exp2.charAt(fss + 4) != '-') {
                                w = w + 1;
                            }
                        }
                    }
                    a2 = Integer.parseInt(exp2.substring(fss + 1, fss + 2 + ww
                            + w));

                    exp2 = (a2 + a1)
                            + exp2.substring(fss + 2 + w + ww, exp2.length());
                    fss = 0;
                    if (exp.length() < 2)
                        break;
                } else if (exp2.charAt(fss) == '-') {
                    if ((fss < exp.length()) && exp2.charAt(fss) == '-') {
                        b = 0;// 符号后面的数字长度-1
                        bb = 0;// 为0说明符号后面的数为负数,1 为正数
                        w = 0;// w用来表示符号前面打数字打位数-1
                        ww = 0;// ww为0说明算术符号前面的数是正数,为1说明是负数
                        if (exp2.charAt(0) == '-')
                            bb = bb + 1;
                        b = fss - bb;
                        a1 = Integer.parseInt(exp2.substring(0, fss));
                        if (exp2.charAt(fss + 1) == '-')
                            ww = ww + 1;
                        if ((fss + 2 + ww) < exp2.length()
                                && exp2.charAt(fss + 2 + ww) != '+'
                                && exp2.charAt(fss + 2 + ww) != '-') {
                            w = w + 1;
                            if ((fss + 3 + ww) < exp2.length()
                                    && exp2.charAt(fss + 3 + ww) != '+'
                                    && exp2.charAt(fss + 3 + ww) != '-')
                                w = w + 1;
                        }
                        a2 = Integer.parseInt(exp2.substring(fss + 1 + ww, fss
                                + 2 + ww + w));
                        exp2 = (a1 - a2)
                                + exp2.substring(fss + 2 + w + ww,
                                        exp2.length());
                        fss = 0;
                        if (exp.length() < 2)
                            break;
                    }
                }
            }
        }
        return exp2;
    }
}
package GUI;

import java.util.Random;

public class CreateExp {
    public static String CreateAtith(int r1, int leval) {
        Random random = new Random();
        String[] array = { "+", "-", "*", "÷" };
        String exp = "";
        int f;// 符号个数
        if (leval == 0) {
            f = 1;
        } else {
            f = random.nextInt(2) + 2;
        }
        int w = 5;
        int s = 0;
        int ff = 0;
        int m = 4;
        if (f > 1) {
            w = random.nextInt(f);// 随机一个符号生成的位置
        }
        ff = random.nextInt(m);
        if (w == 0 && ff < 2) {
            exp = exp + ('(');
            s++;
        }
        exp = exp + (random.nextInt(r1) + 1);
        for (int i = 0; i < f; i++) {
            exp = exp + (array[ff]);
            if (ff > 2)
                i = 5;
            m = 3;
            ff = random.nextInt(m);
            // 判断加括号打位置,如果后面符号为"*", "÷",则不加括号
            if (s == 0 && i == (w - 1) && ff < 2) {
                exp = exp + ('(');
                s++;
            }
            exp = exp + (random.nextInt(r1) + 1);
            if (s == 1 && i == w) {
                exp = exp + (')');
                s++;
            }
        }
        return exp;
    }
}
package GUI;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

public class FileInOut {
    // 获取历史记录,返回数组第1个是对的题数,第2个是错的题数
    public int[] getrecord() throws IOException {
        int[] arr = new int[2];
        String[] strRead = new String[3];
        File userinfo = new File("Record.txt");
        try {
            InputStream in = new FileInputStream(userinfo);// 文件对象
            InputStreamReader read;
            read = new InputStreamReader(in, "GBK");// 设置读取格式
            BufferedReader reader = new BufferedReader(read);
            for (int i = 0; i < 3; i++) {
                strRead[i] = reader.readLine();
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            System.out.println("读文件入出错");
        }
        arr[0] = Integer.parseInt(strRead[1]);
        arr[1] = Integer.parseInt(strRead[2]);
        return arr;
    }

    // 重写记录文件Record.txt
    public void recompose(int[] in) {
        try {
            FileOutputStream out = new FileOutputStream("Record.txt");
            OutputStreamWriter outWriter = new OutputStreamWriter(out, "GBK");
            BufferedWriter bufWrite = new BufferedWriter(outWriter);
            bufWrite.write("对错记录:第二行是对的题数、第三行是错的题数\r\n");
            for (int i = 0; i < in.length; i++)
                bufWrite.write(in[i] + "\r\n");
            bufWrite.close();
            outWriter.close();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("写入出错");
        }
    }
}
package GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

//主窗口
public class GUIcalculator extends JFrame implements ActionListener {
    int language;// 记录选择的语言
    JTextArea quesNumJTA = new JTextArea(1, 10);
    JTextArea answerJTA = new JTextArea(1, 10);
    JPanel panel = new JPanel();
    JPanel languageChoJP = new JPanel();
    JPanel quseNumJP = new JPanel();
    JPanel difficultyChooseJP = new JPanel();
    JPanel answerJP = new JPanel();
    JPanel nextJP = new JPanel();
    JPanel countJP = new JPanel();
    JPanel warningJP = new JPanel();
    JLabel numQuizJL = new JLabel("how many questions u want test?");
    JLabel difficultyQuizJL = new JLabel("easy or difficult?");
    JLabel showQuestionJL = new JLabel();// 放题目label
    JLabel resultJudgeJL = new JLabel();
    JLabel showCountJL = new JLabel();
    JLabel warningJL = new JLabel();
    JButton sureJB = new JButton("sure");
    JButton easyJB = new JButton("easy");
    JButton difficultJB = new JButton("difficult");
    JButton nextJB = new JButton("next");
    JButton submitJB = new JButton("submit");
    JButton reenterJB = new JButton("reenter");
    JButton chineseJB = new JButton("中文");
    JButton englishJB = new JButton("English");
    int trueCount = 0;// 正确题数
    int failCount = 0;// 错误题数
    String questNum;// 题目数量String类型
    int expNum;// 题目数量int类型
    int explevel;// 记录表达式难度级别
    int expdone = 1;// 
    int done = 0;//记录已经计算的题目
    long startTime;//结束时间
    long endTime;//开始时间
    CreateExp create = new CreateExp();//
    CountExp count = new CountExp();//
    String[] exparr = null;// 放题目的数组
    String[] expanswer = null;// 放答案的数组

    // 主界面
    public GUIcalculator() {
        super("四则运算");
        setSize(500, 500);
        setLocation(450, 350);
        super.setResizable(false);
        // 放题目的控件
        quesNumJTA.setText("");
        answerJTA.setText("");
        sureJB.setActionCommand("sure");
        sureJB.addActionListener(this);
        easyJB.setActionCommand("easy");
        easyJB.addActionListener(this);
        difficultJB.setActionCommand("difficult");
        difficultJB.addActionListener(this);
        nextJB.setActionCommand("next");
        nextJB.addActionListener(this);
        submitJB.setActionCommand("submit");
        submitJB.addActionListener(this);
        reenterJB.setActionCommand("reenter");
        reenterJB.addActionListener(this);
        chineseJB.setActionCommand("chinese");
        chineseJB.addActionListener(this);
        englishJB.setActionCommand("english");
        englishJB.addActionListener(this);
        languageChoJP.add(chineseJB);
        languageChoJP.add(englishJB);
        // 题目 +答案框+确定按钮
        quseNumJP.add(numQuizJL);
        quseNumJP.add(quesNumJTA);
        quseNumJP.add(sureJB);
        getContentPane().add(panel);
        panel.add(languageChoJP);
        difficultyChooseJP.add(difficultyQuizJL);
        difficultyChooseJP.add(easyJB);
        difficultyChooseJP.add(difficultJB);
        answerJP.add(showQuestionJL);
        answerJP.add(answerJTA);
        answerJP.add(submitJB);
        nextJP.add(resultJudgeJL);
        nextJP.add(nextJB);
        countJP.add(showCountJL);
        warningJP.add(warningJL);
        warningJP.add(reenterJB);
    }

    // 重绘窗口
    public void panelRepaint(JPanel a, JPanel b, JPanel c) {
        a.remove(b);
        a.add(c);
        a.revalidate();
        a.repaint();
    }

    // 点击事件
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("chinese")) {// 设置控件文字为中文
            language = 0;
            numQuizJL.setText("你想测试多少题?");
            difficultyQuizJL.setText("你想测试的难易程度?");
            warningJL.setText("请输入一个大于0的整数!!");
            sureJB.setText("确定");
            easyJB.setText("容易");
            difficultJB.setText("难");
            nextJB.setText("继续");
            submitJB.setText("提交");
            reenterJB.setText("重新输入");
            panelRepaint(panel, languageChoJP, quseNumJP);
        }
        // 设置控件文字为英文
        if (e.getActionCommand().equals("english")) {
            language = 1;
            numQuizJL.setText("how many questions u want test?");
            difficultyQuizJL.setText("easy or difficult?");
            warningJL.setText("please enter a number greater than 0!!");
            sureJB.setText("sure");
            easyJB.setText("easy");
            difficultJB.setText("difficult");
            nextJB.setText("next");
            submitJB.setText("subnit");
            reenterJB.setText("reenter");
            panelRepaint(panel, languageChoJP, quseNumJP);
        }
        // 点确定,确定题目数量
        if (e.getActionCommand().equals("sure")) {
            this.questNum = quesNumJTA.getText();// 记录题目数量
            this.expNum = Integer.parseInt(questNum);
            if (!questNum.matches("[0-9]*[1-9][0-9]*")) {// 匹配输入的是否有错误
                panelRepaint(panel, quseNumJP, warningJP);
            } else {
                panelRepaint(panel, quseNumJP, difficultyChooseJP);
            }
        }
        // 点容易,确定题目难度为容易,
        if (e.getActionCommand().equals("easy")) {
            startTime = System.currentTimeMillis();// 记录开始时间
            exparr = new String[expNum+1];
            expanswer = new String[expNum+1];
            // 循环生成题目和答案
            for (int j = 0; j < expNum; j++) {
                String expression = create.CreateAtith(9,0);
                exparr[j] = expression;
                expanswer[j] = count.countAnswer(expression);
                System.out.print(expression + "=");
                System.out.println(count.countAnswer(expression));
            }
            showQuestionJL.setText(exparr[done]);
            panelRepaint(panel, difficultyChooseJP, answerJP);
        }
        // 点困难,确定题目难度为困难
        if (e.getActionCommand().equals("difficult")) {
            startTime = System.currentTimeMillis();
            exparr = new String[expNum+1];
            expanswer = new String[expNum+1];
            // 循环生成题目和答案
            for (int j = 0; j < expNum; j++) {
                String expression = create.CreateAtith(30,1);
                exparr[j] = expression;
                expanswer[j] = count.countAnswer(expression);
                System.out.print(expression + "=");
                System.out.println(count.countAnswer(expression));
            }
            showQuestionJL.setText(exparr[done]);
            panelRepaint(panel, difficultyChooseJP, answerJP);
        }
        // 点下一题,判断是否完成所有题目
        if (e.getActionCommand().equals("next")) {
            if (expdone < Integer.parseInt(questNum)) {
                expdone++;
                panelRepaint(panel, nextJP, answerJP);

            } else {
                // 记录结束时间
                endTime = System.currentTimeMillis();
                FileInOut inout = new FileInOut();
                int[] in = new int[2];// 获取历史对错题记录
                try {
                    in = inout.getrecord();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                in[0] = in[0] + trueCount;
                in[1] = in[1] + failCount;
                if (language == 0) {
                    showCountJL.setText("正确题数:" + trueCount + " 错误题数:"
                            + failCount + " 用时: "
                            + ((endTime - startTime) / 1000) + " s"
                            + " 历史总正确题数: " + in[0] + " 历史总错误题数: " + in[1]);
                } else {
                    showCountJL.setText("true:" + trueCount + " false:"
                            + failCount + " time: "
                            + ((endTime - startTime) / 1000) + " s"
                            + " totaltrue: " + in[0] + " totalfalse: " + in[1]);
                }
                inout.recompose(in);
                panelRepaint(panel, nextJP, countJP);
            }
        }
        // 点提交,会判断题目对错
        if (e.getActionCommand().equals("submit")) {
            if (answerJTA.getText().equals(expanswer[done])) {
                if (language == 0) {
                    resultJudgeJL.setText("正确");
                } else {
                    resultJudgeJL.setText("true");
                }
                done++;
                trueCount++;
                answerJTA.setText("");
                showQuestionJL.setText(exparr[done]);
                panelRepaint(panel, answerJP, nextJP);
            } else {
                if (language == 0) {
                    resultJudgeJL.setText("错误 ! 正确答案为: " + expanswer[done]);
                } else {
                    resultJudgeJL.setText("false correct answer is: "
                            + expanswer[done]);
                }
                failCount++;
                answerJTA.setText("");
                showQuestionJL.setText(exparr[done]);
                done++;
                panelRepaint(panel, answerJP, nextJP);
            }
        }

    }
}
package GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class Main {

    public static void main(String[] args) {
        GUIcalculator GUI = new GUIcalculator();
        GUI.setVisible(true);
    }
}

 

运行截图:

              

                 

 

小组照片:

             

 

耗时估计:

      

小结:

  事实证明结对编程真的是可以带来1+1>2的效果的,以前一个人写代码的时候总是会有拖延症,老是觉得还有时间不用急着写,而现在仙平会一直让我快点做,不要拖他后腿,而且两个人一起写大家可以互相帮忙,不懂的问题可以一起解决,效率可以说是高了很多。

队友评价:

  可以说是一个非常棒的队友了,会催我写代码,会帮我解决不懂的问题。对待编程他特别用心,虽然一直叫脖子疼,但还是会坐到电脑前先把代码敲好,很少见他对一件事情这么认真。

代码地址:

https://coding.net/u/chenjunda/p/second/git/tree/master/ArithmeticGUI

posted @ 2017-10-21 16:26  Junda  阅读(203)  评论(1编辑  收藏  举报