标签:str and 减法 优先级 res let red alc ons
项目要求:
实现一个自动生成小学四则运算题目的命令行程序.
使用 -n 参数控制生成题目的个数
使用 -r 参数控制题目中数值的范围, 。该参数可以设置为1或其他自然数。
生成的题目中计算过程不能产生负数
生成的题目中如果存在形如e1 ÷ e2的子表达式,那么其结果应是真分数。
程序一次运行生成的题目不能重复,生成的题目存入执行程序的当前目录下的Exercises.txt文件
每道题目中出现的运算符个数不超过3个
在生成题目的同时,计算出所有题目的答案,并存入执行程序的当前目录下的Answers.txt文件
程序应能支持一万道题目的生成。
程序支持对给定的题目文件和答案文件,判定答案中的对错并进行数量统计。
设计:
分为两个部分,第一是生成题目,生成表达式及答案后写入当前目录下的文件,第二是判断答案是否正确,读取文件后判断,再将结果写入当前目录下的文件。
主函数
public static void main(String[] args){
System.out.println("请选择功能:");
System.out.println(" 1. 四则运算生成器");
System.out.println(" 2. 答案对比");
System.out.print("请输入你的选择[1/2]:");
int choose = new Scanner(System.in).nextInt();
switch (choose){
case 1:
ProducerController producerController = new ProducerController();
producerController.ConstructProblem();break;
case 2:
JudgeAnswerController judgeAnswerController = new JudgeAnswerController();
judgeAnswerController.start();break;
default:
System.out.println("输入不正确,请输入1或2");main(args);break;
}
}
整数生成器与真分数生成器函数
public String[] createProblem(int range){
Random random = new Random();
int operatorCount = 1 + random.nextInt(3); //随机操作符的个数(1-3个)
int operand[] = new int[operatorCount + 1]; //操作数个数
int[] operatorIndex = index(operatorCount, 4, random);
for(int i = 0; i < operatorCount + 1; i++){
operand[i] = random.nextInt(range);
}
String formula = stitchingFormula(operatorCount, operand, operatorIndex);
//计算结果
Calculator calculator = new Calculator();
int res = calculator.algorithm(formula);
String formulaRes[] = new String[2];
if (res > 0){
formulaRes[0] = formula;
formulaRes[1] = String.valueOf(res);
}else {
return createProblem(range);
}
return formulaRes;
}
public String[] createProblem(int range){
Random random = new Random();
int operatorCount = 1 + random.nextInt(3); //操作符的个数1-3
CreateInteger create = new CreateInteger();
int[] operatorIndex = create.index(operatorCount,2, random); //操作符的下标
//生成第一个操作数
int[] coprimeNumber1 = createCoprimeNumbers(range, random);
int x = coprimeNumber1[0];
int y = coprimeNumber1[1];
String s = shamToProperFraction(x, y);
for(int i=0; i < operatorCount; i++){
//生成剩下的操作数
int[] coprimeNumber = createCoprimeNumbers(range, random);
int numx = coprimeNumber[0];
int numy = coprimeNumber[1];
String currentOpreator = OPERATOR[operatorIndex[i]];
if(currentOpreator.equals("+")){ //加法
x = x * numy + y * numx;
y = y * numy;
}else { //减法
int count = 0;
while(x * numy - y * numx < 0){ //差为负数
coprimeNumber = createCoprimeNumbers(range, random);
numx = coprimeNumber[0];
numy = coprimeNumber[1];
count++;
if (count >= 5){
numx = x - 1;
numy = y;
}
}
x = x * numy - y * numx;
y = y * numy;
}
String num = shamToProperFraction(numx, numy);
s += currentOpreator + num;
}
int greatFactor = greatFactor(x,y);
x /= greatFactor; //最终结果化简
y /= greatFactor;
String res = shamToProperFraction(x, y);
s += "=";
String formulaRes[] = {s, res};
return formulaRes;
}
/**
* 假分数转化为真分数
* @param x 分子
* @param y 分母
* @return
*/
public String shamToProperFraction(int x, int y){
if (x > y){
int n = x / y;
x = (x - n * y);
if (x == 0){
return String.valueOf(n);
}
return n + "‘" + x + "/" + y;
}else if (x == y){
return "1";
}else if (y == 1){
return String.valueOf(x);
}else if (x == 0){
return "0";
}
return x + "/" + y;
}
}
计算结果函数
public int algorithm(String s) {
Stack<Integer> numStack = new Stack<>(); //放数字
Stack<String> operatorStack = new Stack<>(); //放操作符
HashMap<String, Integer> hashMap = new HashMap<>(); //存放运算符优先级
hashMap.put("(", 0);
hashMap.put("+", 1);
hashMap.put("-", 1);
hashMap.put("*", 2);
hashMap.put("÷", 2);
String formula = s.replaceAll(" ", "");
for (int i = 0; i < formula.length();) {
StringBuilder digit = new StringBuilder(); //StringBuilder类中的方法主要偏重于对于字符串的变化,例如追加、插入和删除等,这个也是StringBuffer和String类的主要区别。
char c = formula.charAt(i); //将式子字符串切割为c字符
while (Character.isDigit(c)) { //判断字符是否为10进制数字,将一个数加入digit
digit.append(c);
i++;
if (i < formula.length()){
c = formula.charAt(i);
}else {
break;
}
}
if (digit.length() == 0){ //当前digit里面已经无数字,即当前处理符号
switch (c) {
case ‘(‘: {
operatorStack.push(String.valueOf(c));//如果是( 转化为字符串压入字符栈
break;
}
case ‘)‘: { //遇到右括号了计算,因为(的优先级最高
String stmp = operatorStack.pop(); //如果是),将符号栈栈顶元素取到
while (!operatorStack.isEmpty() && !stmp.equals("(")) { //当前符号栈里面还有+ - * /
int a = numStack.pop(); //取操作数a,b
int b = numStack.pop();
int result = calculate(b, a, stmp); //计算
if(result < 0)
return -1;
numStack.push(result); //将结果压入栈
stmp = operatorStack.pop(); //符号指向下一个计算符号
}
break;
}
case ‘=‘: { //遇到等号了计算
String stmp;
while (!operatorStack.isEmpty()) { //当前符号栈里面还有+ - * /,即还没有算完
stmp = operatorStack.pop();
int a = numStack.pop();
int b = numStack.pop();
int result = calculate(b, a, stmp);
if(result < 0)
return -1;
numStack.push(result);
}
break;
}
default: { //不满足之前的任何情况
String stmp;
while (!operatorStack.isEmpty()) { //如果符号栈有符号
stmp = operatorStack.pop(); //当前符号栈,栈顶元素
if (hashMap.get(stmp) >= hashMap.get(String.valueOf(c))) { //比较优先级
int a = numStack.pop();
int b = numStack.pop();
int result =calculate (b, a, stmp);
if(result < 0)
return -1;
numStack.push(result);
}
else {
operatorStack.push(stmp);
break;
}
}
operatorStack.push(String.valueOf(c)); //将符号压入符号栈
break;
}
}
}
else { //处理数字,直接压栈
numStack.push(Integer.valueOf(digit.toString())); //Integer.valueof()返回的是Integer对象,而Integer.parseInt()返回的是int型
continue; //结束本次循环,回到for语句进行下一次循环,即不执行i++(因为此时i已经指向符号了)
}
i++;
}
return numStack.peek(); //返回栈底数字即等式的答案。
}
判断结果函数
public void start(){
System.out.print("请输入待验证答案路径:");
Scanner scanner = new Scanner(System.in);
String exerciseFilePath = scanner.next();
System.out.print("请输入程序生成答案文件路径:");
String answerFilePath = scanner.next();
try {
List<String> exerciseAnswers = exerciseFileReader(exerciseFilePath);
List<String> answers = answerReader(answerFilePath);
List<String> correct = new ArrayList<>();
List<String> wrong = new ArrayList<>();
int max = Math.max(exerciseAnswers.size(), answers.size());
int num = 1;
for (int i = 0; i < max; i++){
if (exerciseAnswers.get(i).equals(answers.get(i))){
correct.add(String.valueOf(num++));
}else {
wrong.add(String.valueOf(num++));
}
}
File grade = new File("Grade.txt");
if (grade.exists()){
grade.delete();
}
if (grade.createNewFile()){
FileOutputStream gradeOutput = new FileOutputStream(grade);
PrintStream gradePrintStream = new PrintStream(gradeOutput);
String corrects = String.join(",", correct);
gradePrintStream.println("Correct:" + correct.size() +
" (" + corrects + ")");
String wrongs = String.join(",", wrong);
gradePrintStream.println("Wrong:" + wrong.size() +
" (" + wrongs + ")");
}
System.out.println("判定完成");
} catch (FileNotFoundException e) {
System.out.println("文件不存在");
} catch (IOException e) {
System.out.println("文件读入异常");
}
}
public List<String> exerciseFileReader(String path) throws IOException {
BufferedReader exerciseReader = new BufferedReader(new FileReader(path));
String exerciseAnswer = "";
List<String> exerciseAnswers = new ArrayList<>();
while ((exerciseAnswer = exerciseReader.readLine()) != null){
String[] split = exerciseAnswer.split("=");
if (split[1] != null){
exerciseAnswers.add(split[1]);
}else {
exerciseAnswers.add(" ");
}
}
return exerciseAnswers;
}
总结
在这一次的作业中,我从伙伴身上学习到了很多,在这个过程中不断的完善代码。两个人一起处理问题,互相汲取对方好的想法,有些细节没有考虑到的,另一个人可以帮忙补充,这样使得效率也大大提高。
标签:str and 减法 优先级 res let red alc ons
原文地址:https://www.cnblogs.com/xjbb/p/14832668.html