用Java设计一个小学算术运算测试软件

2025-01-03 11:55:47
推荐回答(2个)
回答1:

你的想法是好的,但是我仔细构思了一遍,太难了。
首先难点就是怎么生成题目。也是最难得,因为有太多不确定,弄不好就会出现负数结果的题目。
然后是题目的解答,这个难点就不说了,这个我在几年前就写好了。一直没用上,现在却用上了。
还有,你要窗体,可是我不会用swing

还有你要的四则混合运算是多长的,我总不能写一个有1000多个数字写成的式子吧。
题目中是否涉及到小数

下面是我的计算代码:
package compute;
public class CaculateString {
private String str, s;
private int p1, p2;
public CaculateString(String str) {
this.str = str;
this.clear();
this.check();
}
public String caculate() {
while (isEnd(str)) {
p2 = str.indexOf(")");
if (p2 != -1) {
s = str.substring(0, p2);
p1 = s.lastIndexOf("(");
s = str.substring(p1 + 1, p2);
if (!s.equals("")) {
s = this.caculateNumber(s);
if (p2 == (str.length() - 1))
str = str.substring(0, p1) + s;
else
str = str.substring(0, p1) + s + str.substring(p2 + 1);
} else {
str = str.substring(0, p1) + str.substring(p2 + 1);
}
} else
str = this.caculateNumber(str);
}
return str;
}
private String caculateNumber(String ss) {
int p3 = 0, p4 = 0, p5 = 0;
char b = ' ', c = ' ';
String s1 = null, s2 = null;
double d1 = 0.0, d2 = 0.0, d3 = 0.0;
if (ss.charAt(0) == '+') {
ss = ss.substring(1);
if (ss.indexOf("+") == -1 && ss.indexOf("-") == -1
&& ss.indexOf("*") == -1 && ss.indexOf("/") == -1)
return ss;
} else if (ss.charAt(0) == '-') {
if (ss.indexOf("+", 1) == -1 && ss.indexOf("-", 1) == -1
&& ss.indexOf("*", 1) == -1 && ss.indexOf("/", 1) == -1)
return ss;
}
while (isEnd2(ss)) {
p3 = 0;
p4 = 0;
p5 = 0;
if (ss.indexOf("*") == -1 && ss.indexOf("/") == -1) {
for (int i = 0; i < ss.length(); i++) {
b = ss.charAt(i);
if (b == '+' || b == '-') {
if (p3 == 0 && i != 0) {
p3 = i;
c = b;
} else if (p3 != 0 && i != (p3 + 1)) {
p4 = i;
break;
}
}
}
if (p4 == 0) {
s1 = ss.substring(0, p3);
s2 = ss.substring(p3 + 1);
} else {
s1 = ss.substring(0, p3);
s2 = ss.substring(p3 + 1, p4);
}
d1 = Double.parseDouble(s1);
d2 = Double.parseDouble(s2);
if (c == '+')
d3 = d1 + d2;
else if (c == '-')
d3 = d1 - d2;
if (p4 == 0)
ss = new Double(d3).toString();
else
ss = new Double(d3).toString() + ss.substring(p4);
} else {
if (ss.indexOf("*") == -1) {
c = '/';
p4 = ss.indexOf("/");
} else if (ss.indexOf("/") == -1) {
c = '*';
p4 = ss.indexOf("*");
} else if (ss.indexOf("*") > ss.indexOf("/")) {
c = '/';
p4 = ss.indexOf("/");
} else if (ss.indexOf("*") < ss.indexOf("/")) {
c = '*';
p4 = ss.indexOf("*");
}
for (int i = p4 - 1; i >= 0; i--) {
b = ss.charAt(i);
if (b == '+' || b == '-') {
if ((i - 1) != 0 && ss.charAt(i - 1) != '+'
&& ss.charAt(i - 1) != '-') {
p3 = i;
break;
} else if ((i - 1) != 0
&& (ss.charAt(i - 1) == '+' || ss.charAt(i - 1) == '-')) {
p3 = i - 1;
break;
}
}
}
for (int i = p4 + 1; i < ss.length(); i++) {
b = ss.charAt(i);
if ((b == '+' || b == '-' || b == '*' || b == '/')
&& (i - 1) != p4) {
p5 = i;
break;
}
}
if (p3 == 0)
s1 = ss.substring(0, p4);
else
s1 = ss.substring(p3 + 1, p4);
if (p5 == 0)
s2 = ss.substring(p4 + 1);
else
s2 = ss.substring(p4 + 1, p5);
d1 = Double.parseDouble(s1);
d2 = Double.parseDouble(s2);
if (c == '*')
d3 = d1 * d2;
else if (c == '/')
d3 = d1 / d2;
if (p3 == 0 && p5 == 0)
ss = new Double(d3).toString();
else if (p3 == 0 && p5 != 0)
ss = new Double(d3).toString() + ss.substring(p5);
else if (p3 != 0 && p5 == 0)
ss = ss.substring(0, p3 + 1) + new Double(d3).toString();
else if (p3 != 0 && p5 != 0)
ss = ss.substring(0, p3 + 1) + new Double(d3).toString()
+ ss.substring(p5);
}
}
return ss;
}
private boolean isEnd(String str) {
if (str.indexOf("(") != -1 || str.indexOf(")") != -1
|| str.indexOf("+", 1) != -1 || str.indexOf("-", 1) != -1
|| str.indexOf("*", 1) != -1 || str.indexOf("/", 1) != -1)
return true;
return false;
}
private boolean isEnd2(String ss) {
if (ss.indexOf("+", 1) != -1 || ss.indexOf("-", 1) != -1
|| ss.indexOf("*", 1) != -1 || ss.indexOf("/", 1) != -1)
return true;
return false;
}
private void clear() {
str = str.replaceAll(" ", "");
}
private void check() {
int right = 0, left = 0;
for (int i = 0; i < str.length(); i++) {
char a = str.charAt(i);
if (a != '0' && a != '1' && a != '2' && a != '3' && a != '4'
&& a != '5' && a != '6' && a != '7' && a != '8' && a != '9'
&& a != '+' && a != '-' && a != '*' && a != '/' && a != '('
&& a != ')' && a != '.') {
System.exit(0);
}
if (a == '(')
left++;
else if (a == ')')
right++;
}
if (left != right) {
System.exit(0);
}
}
public static void main(String args[]) {
System.out.println(new CaculateString("((1-(0/3))*7)+8").caculate());
}
}

回答2:

package com.amixuse.calcdemo;

import java.util.ArrayList;
import java.util.Random;

public class Calc {

public static void main(String[] args) {
ComputeString computeString = new ComputeString();
for (int i = 0; i < 100; i++) {
System.out.print(computeString.getComputeString()
+ computeString.getComputeResult() + "\t");
if ((i + 11) % 10 == 0) {
System.out.println();
}
}
computeString.setMaxNumber(100);
for (int i = 0; i < 100; i++) {
System.out.print(computeString.getComputeString()
+ computeString.getComputeResult() + "\t");
if ((i + 11) % 10 == 0) {
System.out.println();
}
}

}
}

class ComputeString {
private char flag = '+';
private int maxNumber = 10;
private int num1 = 0;
private int num2 = 0;
public static Random random = new Random(System.currentTimeMillis());
private char[] flags = new char[] { '+', '-', '*', '/' };

public int getMaxNumber() {
return maxNumber;
}

public void setMaxNumber(int maxNumber) {
this.maxNumber = Math.abs(maxNumber);
}

public char getFlag() {
return flag;
}

public void setFlag(char flag) {
this.flag = flag;
}

public int getNum1() {
return num1;
}

public void setNum1(int num1) {
this.num1 = Math.abs(num1);
}

public int getNum2() {
return num2;
}

public void setNum2(int num2) {
this.num2 = Math.abs(num2);
}

/**
* 根据指定的运算符号生成指定的算术表达式
* */
public String getComputeString(char flag) {
setFlag(flag);
setNum1(Math.abs(random.nextInt(maxNumber)));
StringBuffer result = new StringBuffer();
result.append(num1).append(flag);
switch (flag) {
case '+':
if (getNum1() == maxNumber - 1) {
setNum2(0);
} else {
setNum2(random.nextInt(maxNumber - num1 - 1));
}
break;
case '-':
if (getNum1() == 0) {
setNum2(0);
} else {
setNum2(random.nextInt(getNum1()));
}
break;
case '*':
if (getNum1() == 0) {
setNum2(random.nextInt(maxNumber));
} else {
setNum2(maxNumber / getNum1());
}
break;
case '/':
int tmp = Integer.MAX_VALUE;
if (getNum1() == 0) {
while (true) {
tmp = Math.abs(maxNumber);
if (tmp != 0) {
setNum1(tmp);
break;
}
}
result.setLength(0);
result.append(num1).append(flag);
}
setNum2(getGongyuesu(getNum1()));
break;
}
result.append(num2).append("=");
return result.toString();
}

/**
* 随机生成运算符表达式
* */
public String getComputeString() {
setFlag(flags[random.nextInt(flags.length - 1)]);
return getComputeString(getFlag());
}

/**
* 产生最大公约数
*/
private int getGongyuesu(int number) {
ArrayList result = new ArrayList<>();
int length = (int) Math.sqrt(number);
for (int i = 1; i <= length; i++) {
if (number % i == 0) {
result.add(new Integer(i));
}
}
int position = Math.abs(result.size());
position = position == result.size() ? position - 1 : position;
position = position <= 0 ? 0 : position;
return result.get(position);
}

/**
* 取得运算符结果
* */
public int getComputeResult() {
switch (flag) {
case '+':
return num1 + num2;
case '-':
return num1 - num2;
case '*':
return num1 * num2;
case '/':
return num1 / num2;
default:
return 0;
}
}
}
图形界面暂时没有弄。