首页 > 其他分享 >结对项目

结对项目

时间:2024-09-28 21:26:22浏览次数:8  
标签:结对 random return String 项目 int denominator numerator

这个作业属于哪个课程 22级计科2班
这个作业要求在哪里 作业要求
这个作业目标 实现一个自动生成小学四则运算题目的命令行程序(也可以用图像界面,具有相似功能)。


Github地址:https://github.com/JiangJiazhe/PairWork/tree/main

项目设计

函数功能

  1. main模块:设置main函数通过命令行设置参数,函数接受到参数后首先判断参数个数是否符合要求;然后根据"-n","-r","-e","-a"四个标志来获取需要的参数,再判断参数有无结对传入以及传入的参数是否满足构成调用generateQuestions或check()函数的要求,若满足则调用不满足要求则报错;

  2. GenerateQuestion模块:设置按数量与数值范围的要求生成题目集的函数generateQuestions(),定义Question类集合获取生成的题目集,利用循环调用generateExpression()函数来生成表达式,并调用evaluate()函数解析表达式生成答案,最后将序号,表达式、答案包装成Question类对象添加进集合里,从而得到问题集,并调用;

  3. GenerateExpression模块:主要函数为生成表达式generateExpression()函数,该函数调用generateSimpleExpression()
    生成简单表达式,调用generateComplexExpression()生成复杂表达式,且设置随机调用;而这两个函数又会调用生成整数generateOperand()函数与生成真分数generateTrueFraction()函数来随机生成整数和真分数以构造出随机算术表达式,最终通过generateExpression()函数返回算术表达式的字符串形式;

  4. ExpressionParsing模块:主要函数为evaluate()函数,通过设置操作数栈和操作符栈,对传进来的表达式进行一个个的读取入栈;当字符为数字类型时入操作数栈,当字符为操作符类型时(通过isOperator()函数判断)入操作符栈;随后通过判断操作符优先级(通过hasPrecedence()函数以及有无括号"()"来判断)计算出答案,该过程调用applyOp()函数实现,且整个计算过程是一边入栈一边计算的,当满足计算要求时计算结果,然后将结果入栈,后续继续计算直到结束;最后返回的是一个真分数类变量,该变量作为答案后续可进行自动字符串转换;

  5. CorrectExpression模块:设置check()函数获取两个文件路径参数,通过BufferedReader类读取文本内容,在函数中利用循环的单行读取来获取两个文件相同序号题目的答案字符串,然后判断两个字符串大小是否相同,相同则代表答案一样,对correct字符串变量进行修改,不相同则对wrong字符串变量进行修改,最终得到两个存放正确编号和错误编号的字符串变量,并将其打印进指定文件;

  6. Save模块:内含saveQuestionsToFile()函数通过循环来打印问题到问题文件中去,saveAnswerToFile()函数通过循环打印答案到答案文件;

部分代码展示

package main;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class CorrectExpression {
    public CorrectExpression() {
    }

    public static void check(String checkFile, String answersFile) throws IOException {
        BufferedReader readerCheck = new BufferedReader(new InputStreamReader(new FileInputStream(checkFile)));
        BufferedReader answerCheck = new BufferedReader(new InputStreamReader(new FileInputStream(answersFile)));
        String checkLine = "";
        String answerLine = "";
        String correct = "";
        String wrong = "";
        int correctNum = 0;
        int wrongNum = 0;

        while((checkLine = readerCheck.readLine()) != null && (answerLine = answerCheck.readLine()) != null) {
            String[] checkString = checkLine.split("\\.");
            String[] answerString = answerLine.split("\\.");
            if (checkString[1].equals(answerString[1])) {
                if (correct.equals("")) {
                    correct = answerString[0];
                    ++correctNum;
                } else {
                    correct = correct + "," + answerString[0];
                    ++correctNum;
                }
            } else if (wrong.equals("")) {
                wrong = answerString[0];
                ++wrongNum;
            } else {
                wrong = wrong + "," + answerString[0];
                ++wrongNum;
            }
        }

        readerCheck.close();
        answerCheck.close();
        OutputStreamWriter resultWriter = new OutputStreamWriter(new FileOutputStream(new File("D:\\Grade.txt")), "UTF-8");
        resultWriter.append("Correct:" + correctNum + "(" + correct + ")");
        resultWriter.append("\n");
        resultWriter.append("Wrong:" + wrongNum + "(" + wrong + ")");
        resultWriter.close();
        System.out.println("Correct:" + correctNum + "(" + correct + ")");
        System.out.println("Wrong:" + wrongNum + "(" + wrong + ")");
    }
}
package main;

import java.util.Stack;

public class ExpressionParsing {
    public ExpressionParsing() {
    }

    public static Fraction evaluate(String expression) {
        Stack<Character> operators = new Stack();
        Stack<Fraction> value = new Stack();

        for(int i = 0; i < expression.length(); ++i) {
            char c = expression.charAt(i);
            if (Character.isDigit(c)) {
                int number;
                for(number = 0; i < expression.length() && Character.isDigit(expression.charAt(i)); ++i) {
                    number = number * 10 + (expression.charAt(i) - 48);
                }

                int denominator = 1;
                int numerator = number;
                if (i < expression.length() && expression.charAt(i) == '\'') {
                    ++i;
                    numerator = expression.charAt(i++) - 48;
                    ++i;
                    denominator = expression.charAt(i++) - 48;
                    numerator += number * denominator;
                } else if (i < expression.length() && expression.charAt(i) == '/') {
                    ++i;
                    denominator = expression.charAt(i++) - 48;
                }

                value.push(new Fraction(numerator, denominator));
                --i;
            } else if (c != ' ' && c != '=') {
                if (c == '(') {
                    operators.push(c);
                } else if (c == ')') {
                    while(!operators.isEmpty() && (Character)operators.peek() != '(') {
                        value.push(applyOp((Character)operators.pop(), (Fraction)value.pop(), (Fraction)value.pop()));
                    }

                    if (!operators.isEmpty()) {
                        operators.pop();
                    }
                } else if (isOperator(c)) {
                    while(!operators.isEmpty() && (Character)operators.peek() != '(' && hasPrecedence(c, (Character)operators.peek())) {
                        value.push(applyOp((Character)operators.pop(), (Fraction)value.pop(), (Fraction)value.pop()));
                    }

                    operators.push(c);
                }
            }
        }

        while(!operators.isEmpty()) {
            value.push(applyOp((Character)operators.pop(), (Fraction)value.pop(), (Fraction)value.pop()));
        }

        return (Fraction)value.pop();
    }

    private static boolean isOperator(char c) {
        return c == '+' || c == '-' || c == 215 || c == 247;
    }

    private static Fraction applyOp(char op, Fraction f1, Fraction f2) {
        switch(op) {
        case '+':
            return f1.add(f2);
        case '-':
            return f2.subtract(f1);
        case '×':
            return f1.multiply(f2);
        case '÷':
            return f2.divide(f1);
        default:
            return new Fraction(1, 1);
        }
    }

    private static boolean hasPrecedence(char op1, char op2) {
        return op1 != 215 && op1 != 247 || op2 != '+' && op2 != '-';
    }
}
package main;

public class Fraction {
    public int numerator;
    public int denominator;

    public Fraction(int numerator, int denominator) {
        if (denominator == 0) {
            throw new IllegalArgumentException("Denominator cannot be zero");
        } else {
            this.numerator = numerator;
            this.denominator = denominator;
            this.reduce();
        }
    }

    private void reduce() {
        int gcd = this.gcd(this.numerator, this.denominator);
        this.numerator /= gcd;
        this.denominator /= gcd;
    }

    private int gcd(int a, int b) {
        return b == 0 ? a : this.gcd(b, a % b);
    }

    public Fraction add(Fraction other) {
        int newNumerator = this.numerator * other.denominator + this.denominator * other.numerator;
        int newDenominator = this.denominator * other.denominator;
        return new Fraction(newNumerator, newDenominator);
    }

    public Fraction subtract(Fraction other) {
        int newNumerator = this.numerator * other.denominator - this.denominator * other.numerator;
        int newDenominator = this.denominator * other.denominator;
        return new Fraction(newNumerator, newDenominator);
    }

    public Fraction multiply(Fraction other) {
        int newNumerator = this.numerator * other.numerator;
        int newDenominator = this.denominator * other.denominator;
        return new Fraction(newNumerator, newDenominator);
    }

    public Fraction divide(Fraction other) {
        if (other.numerator == 0) {
            throw new IllegalArgumentException("Cannot divide by zero");
        } else {
            int newNumerator = this.numerator * other.denominator;
            int newDenominator = this.denominator * other.numerator;
            return new Fraction(newNumerator, newDenominator);
        }
    }

    public String toString() {
        if (this.numerator == 0) {
            return this.numerator.makeConcatWithConstants<invokedynamic>(this.numerator);
        } else if (Math.abs(this.numerator) >= Math.abs(this.denominator)) {
            if (this.denominator == 1) {
                return this.numerator.makeConcatWithConstants<invokedynamic>(this.numerator);
            } else if (this.denominator == -1) {
                return (-this.numerator).makeConcatWithConstants<invokedynamic>(-this.numerator);
            } else {
                int whole = this.numerator / this.denominator;
                return whole + "'" + (this.numerator - whole * this.denominator) + "/" + this.denominator;
            }
        } else {
            return this.numerator + "/" + this.denominator;
        }
    }
}
package main;

import java.util.Random;

public class GenerateExpression {
    private static final String[] OPERATORS = new String[]{"+", "-", "×", "÷"};
    private static final int MIN_OPERAND = 1;
    private static final int MIN_EXPRESSION_PARTS = 2;
    private static final int MAX_RECURSION_DEPTH = 3;
    private static final Random random = new Random();

    public GenerateExpression() {
    }

    public static String generateExpression(int minParts, int maxParts, int MaxNumber) {
        if (minParts <= maxParts && minParts >= 2) {
            int parts = minParts + random.nextInt(maxParts - minParts + 1);
            return parts < 3 ? generateSimpleExpression(parts, MaxNumber) : generateComplexExpression(parts, MaxNumber);
        } else {
            throw new IllegalArgumentException("Invalid range for expression parts");
        }
    }

    private static String generateSimpleExpression(int parts, int MaxNumber) {
        StringBuilder sb = new StringBuilder();
        if (parts == 1) {
            if (random.nextBoolean()) {
                sb.append(generateTrueFraction(MaxNumber));
            } else {
                sb.append(generateOperand(MaxNumber));
            }

            sb.append(' ');
            return sb.toString();
        } else {
            int flat = 0;

            for(int i = 0; i < parts - 1; ++i) {
                if (random.nextInt(5) == 1) {
                    sb.append(generateTrueFraction(MaxNumber));
                } else {
                    sb.append(generateOperand(MaxNumber));
                }

                sb.append(' ');
                String Op = OPERATORS[random.nextInt(OPERATORS.length)];
                if (Op == "-") {
                    ++flat;
                }

                sb.append(Op);
                sb.append(' ');
            }

            if (random.nextInt(5) == 1) {
                sb.append(generateTrueFraction(MaxNumber));
            } else {
                sb.append(generateOperand(MaxNumber));
            }

            return sb.toString();
        }
    }

    private static String generateComplexExpression(int parts, int MaxNumber) {
        if (random.nextBoolean()) {
            return generateSimpleExpression(parts, MaxNumber);
        } else {
            int subParts = random.nextInt(parts - 1) + 1;
            String left = generateSimpleExpression(subParts, MaxNumber);
            String right = generateSimpleExpression(parts - subParts, MaxNumber);
            if (subParts != 1 && parts - subParts != 1) {
                return "(" + left + ")" + OPERATORS[random.nextInt(OPERATORS.length)] + "(" + right + ")";
            } else {
                return subParts == 1 ? left + OPERATORS[random.nextInt(OPERATORS.length)] + "(" + right + ")" : "(" + left + ")" + OPERATORS[random.nextInt(OPERATORS.length)] + right;
            }
        }
    }

    private static int generateOperand(int MAX_OPERAND) {
        return 1 + random.nextInt(MAX_OPERAND - 1 + 1);
    }

    public static String generateTrueFraction(int MAX_OPERAND) {
        int denominator = random.nextInt(9) + 1;

        int numerator;
        for(numerator = random.nextInt(9) + 1; numerator >= denominator; denominator = random.nextInt(9) + 1) {
            numerator = random.nextInt(9) + 1;
        }

        int wholePart = random.nextInt(MAX_OPERAND);
        return wholePart == 0 ? numerator + "/" + denominator : wholePart + "'" + numerator + "/" + denominator;
    }
}
package main;

import java.util.Random;

public class GenerateExpression {
    private static final String[] OPERATORS = new String[]{"+", "-", "×", "÷"};
    private static final int MIN_OPERAND = 1;
    private static final int MIN_EXPRESSION_PARTS = 2;
    private static final int MAX_RECURSION_DEPTH = 3;
    private static final Random random = new Random();

    public GenerateExpression() {
    }

    public static String generateExpression(int minParts, int maxParts, int MaxNumber) {
        if (minParts <= maxParts && minParts >= 2) {
            int parts = minParts + random.nextInt(maxParts - minParts + 1);
            return parts < 3 ? generateSimpleExpression(parts, MaxNumber) : generateComplexExpression(parts, MaxNumber);
        } else {
            throw new IllegalArgumentException("Invalid range for expression parts");
        }
    }

    private static String generateSimpleExpression(int parts, int MaxNumber) {
        StringBuilder sb = new StringBuilder();
        if (parts == 1) {
            if (random.nextBoolean()) {
                sb.append(generateTrueFraction(MaxNumber));
            } else {
                sb.append(generateOperand(MaxNumber));
            }

            sb.append(' ');
            return sb.toString();
        } else {
            int flat = 0;

            for(int i = 0; i < parts - 1; ++i) {
                if (random.nextInt(5) == 1) {
                    sb.append(generateTrueFraction(MaxNumber));
                } else {
                    sb.append(generateOperand(MaxNumber));
                }

                sb.append(' ');
                String Op = OPERATORS[random.nextInt(OPERATORS.length)];
                if (Op == "-") {
                    ++flat;
                }

                sb.append(Op);
                sb.append(' ');
            }

            if (random.nextInt(5) == 1) {
                sb.append(generateTrueFraction(MaxNumber));
            } else {
                sb.append(generateOperand(MaxNumber));
            }

            return sb.toString();
        }
    }

    private static String generateComplexExpression(int parts, int MaxNumber) {
        if (random.nextBoolean()) {
            return generateSimpleExpression(parts, MaxNumber);
        } else {
            int subParts = random.nextInt(parts - 1) + 1;
            String left = generateSimpleExpression(subParts, MaxNumber);
            String right = generateSimpleExpression(parts - subParts, MaxNumber);
            if (subParts != 1 && parts - subParts != 1) {
                return "(" + left + ")" + OPERATORS[random.nextInt(OPERATORS.length)] + "(" + right + ")";
            } else {
                return subParts == 1 ? left + OPERATORS[random.nextInt(OPERATORS.length)] + "(" + right + ")" : "(" + left + ")" + OPERATORS[random.nextInt(OPERATORS.length)] + right;
            }
        }
    }

    private static int generateOperand(int MAX_OPERAND) {
        return 1 + random.nextInt(MAX_OPERAND - 1 + 1);
    }

    public static String generateTrueFraction(int MAX_OPERAND) {
        int denominator = random.nextInt(9) + 1;

        int numerator;
        for(numerator = random.nextInt(9) + 1; numerator >= denominator; denominator = random.nextInt(9) + 1) {
            numerator = random.nextInt(9) + 1;
        }

        int wholePart = random.nextInt(MAX_OPERAND);
        return wholePart == 0 ? numerator + "/" + denominator : wholePart + "'" + numerator + "/" + denominator;
    }
}
public static void main(String[] args) throws IOException {
        int numberOfQuestions = -1;
        int maxNumber = -1;
        String filename1 = "";
        String filename2 = "";
        if (args.length < 2) {
            System.err.println("输入参数至少两个");
        } else {
            String[] var5 = args;
            int QuestionNumber = args.length;

            for(int var7 = 0; var7 < QuestionNumber; ++var7) {
                String arg = var5[var7];
                if (arg.startsWith("-n")) {
                    try {
                        numberOfQuestions = Integer.parseInt(arg.substring(2));
                    } catch (NumberFormatException var13) {
                        System.err.println("Invalid argument after -n: " + arg);
                        System.exit(1);
                    }

                    System.out.println(numberOfQuestions);
                }

                if (arg.startsWith("-r")) {
                    try {
                        maxNumber = Integer.parseInt(arg.substring(2));
                    } catch (NumberFormatException var12) {
                        System.err.println("Invalid argument after -r: " + arg);
                        System.exit(1);
                    }

                    System.out.println(maxNumber);
                }

                if (arg.startsWith("-e")) {
                    try {
                        filename1 = arg.substring(2);
                    } catch (NumberFormatException var11) {
                        System.err.println("Invalid argument after -e: " + arg);
                        System.exit(1);
                    }

                    System.out.println(filename1);
                }

                if (arg.startsWith("-a")) {
                    try {
                        filename2 = arg.substring(2);
                    } catch (NumberFormatException var10) {
                        System.err.println("Invalid argument after -a: " + arg);
                        System.exit(1);
                    }

                    System.out.println(filename2);
                }
            }

            if (numberOfQuestions != -1 && maxNumber != -1 || !filename1.equals("") && !filename2.equals("")) {
                if (numberOfQuestions != -1 && maxNumber != -1) {
                    GenerateQuestion.generateQuestions(numberOfQuestions, maxNumber);
                }

                if (!filename1.equals("") && !filename2.equals("")) {
                    BufferedReader exercise = new BufferedReader(new InputStreamReader(new FileInputStream(filename1)));
                    QuestionNumber = 0;
                    String Expression = "";
                    ArrayList questions = new ArrayList();

                    while((Expression = exercise.readLine()) != null) {
                        ++QuestionNumber;
                        String answer = ExpressionParsing.evaluate(Expression).toString();
                        questions.add(new Question(QuestionNumber, Expression, answer));
                    }

                    Save.saveAnswerToFile(questions, "D:\\SyAnswer.txt");
                    CorrectExpression.check(filename2, "D:\\SyAnswer.txt");
                }
            } else {
                System.err.println("参数输入方式不对");
            }

        }
    }
}

结果展示



PSP分析

PSP2.1 Person Software Process Stages 预估耗时(min) 实际耗时(min)
Planning 计划 60 60
.Estimate 预估这个任务需要多少时间 60 120
.Development 开发 600 1040
· Analysis 需求分析(包括学习新技术 60 90
· Design Spec 生成设计文档 30 30
· Design Review 设计复审 20 15
· Coding Standard 代码规范(为目前的开发制定合适的规范) 30 20
· Design 具体设计 30 30
· Coding 具体编码 120 240
· Code Review 代码复审 30 40
· Test 测试(自我测试、修改代码、提交修改) 30 40
Reporting 报告 30 40
· Test Report 测试报告 20 20
· Size Measurement 计算工作量 10 10
· Postmortem & Process Improvement Plan 事后总结,并提出过程改进计划 30 30
Total 合计 1180 1755

总结

由于java是初学,而且这次作业是独立完成的,所以有不少功能未能实现,希望能够吸取这次的教训,下次将作业完成的更好。

标签:结对,random,return,String,项目,int,denominator,numerator
From: https://www.cnblogs.com/jjz1475983697/p/18438425

相关文章

  • 结对项目——四则运算
    结对项目——四则运算这个作业属于哪个课程软工22级计科12班这个作业的要求在哪里作业要求这个作业的目标实现四则运算的结对编程项目成员姓名学号GitHub链接分工谭立业3122004365github项目功能的基本实现,博客的编写罗锴佳3122001905gith......
  • 结对项目
    成员姓名学号李心怡3222004336郑梦瀚3222004340作业这个作业属于哪个课程班级链接这个作业要求在哪里作业链接这个作业的目标二人合作实现一个自动生成小学四则运算题目的命令行程序Github链接:点击此处PSP表格PSP2.1PersonalSoftwar......
  • 结对项目
    这个作业属于哪个课程计科22级12班这个作业要求在哪里作业要求这个作业的目标实现一个自动生成小学四则运算题目的命令行程序姓名&学号:姓名学号董雯霖3122004780陈金星3122004774GitHub链接:https://github.com/1534063091/Arithmometer一......
  • 结对项目
    这个作业属于哪个课程计科2班这个作业要求在哪里作业要求这个作业的目标<要求实现一个自动生成小学四则运算题目的命令行程序>[作业地址](https://github.com/cr1017/3122004813)一、时间预估及实际花费时间PSP表格PSP2.1PersonalSoftwareProcessS......
  • 结对项目
    这个作业属于哪个课程课程这个作业的要求在哪里结对项目这个作业的目标实现一个自动生成小学四则运算题目的命令行程序一、合作成员项目成员学号github仓库地址黄锐3222004335github王伊若3222004382PSP2.1PersonalSoftwareProcess......
  • 结对项目
    这个作业属于哪个课程https://edu.cnblogs.com/campus/gdgy/CSGrade22-34这个作业要求在哪里https://edu.cnblogs.com/campus/gdgy/CSGrade22-34/homework/13230这个作业的目标组队编程实现四则运算的项目组队成员阿依古再丽(3222004721)、张汉洁(3222004598)这......
  • 结对项目
    姓名学号Github项目地址薛秋昊3122004369https://github.com/0818XR/0818XR/tree/main/3122004369/GenerateArithmeticProblems曾俊涛3122004373一.PSP表格PSP2.1PersonalSoftwareProcessStages预估耗时(分钟实际耗时(分钟)Planning计划1010......
  • 结对项目
    这个作业属于哪个课程 软件工程课程这个作业要求在哪里 结对项目这个作业的目标 按照要求写一个四则运算生成器成员一 罗祖文3122004537成员二 郑志涛312004547GitHub地址:https://github.com/Lzw1322/3122004537PSP2.1PersonalSoftwareProcessStages预估耗时(分钟......
  • 结对项目
    姓名学号陈祥意3122004821肖商3122004837GitHub地址https://github.com/Csneer/auto_generate_formulaPSP表格PSP2.1PersonalSoftwareProcessStages预估耗时(分钟)实际耗时(分钟)Planning计划4050Estimate估计这个任务需要多少时间1460......
  • 结对项目
    这个作业属于哪个课程https://edu.cnblogs.com/campus/gdgy/CSGrade22-34/这个作业要求在哪里https://edu.cnblogs.com/campus/gdgy/CSGrade22-34/homework/13230这个作业的目标学习合作编程开发项目,继续熟悉运用性能分析工具和PSP表格管理时间,养成良好编程风格......