四则运算
Posted 曾哥啊
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了四则运算相关的知识,希望对你有一定的参考价值。
不经常写代码,只能随便写点了。分数还是做不出来,代码也有点bug,由于疏忽大意忘记了作业的提交时间实在惭愧。先把代码贴上来,之后再进行修改。。import java.util.Random;
public class CreateExpression {
public static final char[] operator = { ‘+‘, ‘-‘, ‘*‘, ‘/‘ };
public static String createExpression(int range) {
int num = new Random().nextInt(2) + 1;// 算符个数
String[] number = new String[num + 1];
char[] op = new char[num];
int length = number.length;
String expression = "";
for (int i = 0; i < num; i++) {
op[i] = operator[(int) (Math.random() * 4)];// 生成算符
}
for (int i = 0; i < num + 1; i++) {// 生成算数
number[i] = (int) (Math.random() * range) + "";// 整数
}
for (int i = 0; i < length; i++) {
expression += number[i];
if (i <= length - 2)
if (op[i] != ‘/‘ || (op[i] == ‘/‘ && Integer.parseInt(number[i + 1]) != 0))//下一个运算符不为“/”或者下一个除数不为0
expression += op[i];
else {
expression = "";
return null;
}
}
return expression;
}
}public Object calculate(String expression) {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");//调用js引擎
Object result = null;
try {
result = engine.eval(expression);//调用eval方法
if (Double.parseDouble(String.valueOf(result)) < 0) {
return null;
}
} catch (ScriptException e) {
e.printStackTrace();
}
return result;
}
public void judge(String[] s1, String[] s2) {
int correctnum = 0;
int length = s1.length;
int[] correct = new int[length];
int[] wrong = new int[length];
for (int i = 0; i < length; i++) {
if (s1[i].equals(s2[i])) {
correct[correctnum] = i + 1;//记录正确结果题目号
correctnum++;//记录正确结果个数
} else {
wrong[i - correctnum] = i + 1;//错误结果题目号
}
}
//打印结果
System.out.print("Correct:" + correctnum);
for (int i = 0; i < correctnum; i++) {
System.out.print(correct[i] + " 、 ");
}
System.out.print("Wrong:" + (length - correctnum));
for (int i = 0; i < length - correctnum; i++) {
System.out.print(wrong[i] + " 、 ");
}
}
public static void start() throws IOException {
Scanner sc = new Scanner(System.in
);
int loopnum = 0, range;
System.out.println("输入题目文件名称");
File questionFile = new File(sc.next());
FileWriter q_writer = new FileWriter(questionFile, true);
System.out.println("输入文件答案名称");
File answerFile = new File(sc.next());
FileWriter a_writer = new FileWriter(answerFile, true);
System.out.println("输入题目个数");
loopnum = sc.nextInt();
System.out.println("输入数值范围");
range = sc.nextInt();
String[] i_anwser = new String[loopnum];
String[] q_anwser = new String[loopnum];
// 开始生成
for (int i = 0; i < loopnum; i++) {
CreateExpression cte = new CreateExpression();
String expression = cte.createExpression(range);
Object result = null;
CalculateExpression cce = new CalculateExpression();
result = cce.calculate(expression);
// 判断结果是否可用
q_writer.write(i + "." + expression);
a_writer.write(i + "." + result);
q_anwser[i] = result;
}
System.out.println("输入答案");
for (int i = 0; i < loopnum; i++) {
i_anwser[i] = sc.next();
}
juds.judge(q_anwser, i_anwser);
}
很多边界条件和判断语句不知道写。。
public class CreateExpression {
public static final char[] operator = { ‘+‘, ‘-‘, ‘*‘, ‘/‘ };
public static String createExpression(int range) {
int num = new Random().nextInt(2) + 1;// 算符个数
String[] number = new String[num + 1];
char[] op = new char[num];
int length = number.length;
String expression = "";
for (int i = 0; i < num; i++) {
op[i] = operator[(int) (Math.random() * 4)];// 生成算符
}
for (int i = 0; i < num + 1; i++) {// 生成算数
number[i] = (int) (Math.random() * range) + "";// 整数
}
for (int i = 0; i < length; i++) {
expression += number[i];
if (i <= length - 2)
if (op[i] != ‘/‘ || (op[i] == ‘/‘ && Integer.parseInt(number[i + 1]) != 0))//下一个运算符不为“/”或者下一个除数不为0
expression += op[i];
else {
expression = "";
return null;
}
}
return expression;
}
}public Object calculate(String expression) {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");//调用js引擎
Object result = null;
try {
result = engine.eval(expression);//调用eval方法
if (Double.parseDouble(String.valueOf(result)) < 0) {
return null;
}
} catch (ScriptException e) {
e.printStackTrace();
}
return result;
}
public void judge(String[] s1, String[] s2) {
int correctnum = 0;
int length = s1.length;
int[] correct = new int[length];
int[] wrong = new int[length];
for (int i = 0; i < length; i++) {
if (s1[i].equals(s2[i])) {
correct[correctnum] = i + 1;//记录正确结果题目号
correctnum++;//记录正确结果个数
} else {
wrong[i - correctnum] = i + 1;//错误结果题目号
}
}
//打印结果
System.out.print("Correct:" + correctnum);
for (int i = 0; i < correctnum; i++) {
System.out.print(correct[i] + " 、 ");
}
System.out.print("Wrong:" + (length - correctnum));
for (int i = 0; i < length - correctnum; i++) {
System.out.print(wrong[i] + " 、 ");
}
}
public static void start() throws IOException {
Scanner sc = new Scanner(System.in
);
int loopnum = 0, range;
System.out.println("输入题目文件名称");
File questionFile = new File(sc.next());
FileWriter q_writer = new FileWriter(questionFile, true);
System.out.println("输入文件答案名称");
File answerFile = new File(sc.next());
FileWriter a_writer = new FileWriter(answerFile, true);
System.out.println("输入题目个数");
loopnum = sc.nextInt();
System.out.println("输入数值范围");
range = sc.nextInt();
String[] i_anwser = new String[loopnum];
String[] q_anwser = new String[loopnum];
// 开始生成
for (int i = 0; i < loopnum; i++) {
CreateExpression cte = new CreateExpression();
String expression = cte.createExpression(range);
Object result = null;
CalculateExpression cce = new CalculateExpression();
result = cce.calculate(expression);
// 判断结果是否可用
q_writer.write(i + "." + expression);
a_writer.write(i + "." + result);
q_anwser[i] = result;
}
System.out.println("输入答案");
for (int i = 0; i < loopnum; i++) {
i_anwser[i] = sc.next();
}
juds.judge(q_anwser, i_anwser);
}
很多边界条件和判断语句不知道写。。
以上是关于四则运算的主要内容,如果未能解决你的问题,请参考以下文章