个人项目四则混合运算
一、需求分析
(1)程序可接收一个输入参数n,然后随机产生n道加减乘除(分别使用符号±÷来表示)练习题,每个数字在 0 和 100 之间,运算符在3个到5个之间。
(2)每个练习题至少要包含2种运算符。同时,由于小学生没有分数与负数的概念,你所出的练习题在运算过程中不得出现负数与非整数,比如不能出 3÷5+2=2.6,2-5+10=7等算式。
(3)练习题生成好后,将你的学号与生成的n道练习题及其对应的正确答案输出到文件“result.txt”中,不要输出额外信息,文件目录与程序目录一致。
(4)当程序接收的参数为4时,以下为一个输出文件示例。
2018010203
13+17-1=29
1115-5=160
3+10+4-16=1
15÷5+3-2=4
二、功能设计
1、基本功能:实现四则运算的出题和计算
2、扩展功能(目前未实现):
(1)支持有括号的运算式,包括出题与求解正确答案。注意,算式中存在的括号数必须大于2对,且不得超过运算符的个数。
(2)扩展程序功能支持真分数的出题与运算(只需要涵盖加减法即可),例如:1/6 + 1/8 + 2/3= 23/24。注意在实现本功能时,需支持运算时分数的自动化简,比如 1/2+1/6=2/3,而非4/6,且计算过程中与结果都须为真分数。
三、代码展示
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Main {
private static String calculation() {
Random rand = new Random();
String[] operator = new String[] { "+", "-", "*", "÷" };
int number = rand.nextInt(3) + 4;
int figure = rand.nextInt(100) + 1;
String[] str = new String[number * 2 + 1];
str[0] = String.valueOf(figure);
for (int i = 1; i < number * 2 - 2; i += 2) {
int ope = rand.nextInt(4);
if (ope == 3) {
str[i] = operator[ope];
str[i + 1] = String.valueOf(exact(Integer.parseInt(str[i - 1]), rand.nextInt(100) + 1));
} else if (ope == 1) {
str[i] = operator[ope];
str[i + 1] = String.valueOf(positive(Integer.parseInt(str[i - 1]), rand.nextInt(100) + 1));
} else {
str[i] = operator[ope];
str[i + 1] = String.valueOf(rand.nextInt(100) + 1);
}
}
str[str.length - 2] = "=";
String[] str2 = new String[number * 2 + 1];
for (int i = 0; i < str.length - 1; i++) {
str2[i] = str[i];
}
int result = 0;
for (int i = 1; i < str.length; i++) {
if (str[i] == "*" || str[i] == "÷") {
if (str[i] == "*") {
result = Integer.parseInt(str[i - 1]) * Integer.parseInt(str[i + 1]);
str[i - 1] = String.valueOf(result);
str[i + 1] = String.valueOf(result);
} else {
result = Integer.parseInt(str[i - 1]) / Integer.parseInt(str[i + 1]);
str[i - 1] = String.valueOf(result);
str[i + 1] = String.valueOf(result);
}
}
}
for (int i = 1; i < str.length; i++) {
if (str[i] == "+" || str[i] == "-") {
if (str[i] == "+") {
result = Integer.parseInt(str[i - 1]) + Integer.parseInt(str[i + 1]);
str[i - 1] = String.valueOf(result);
str[i + 1] = String.valueOf(result);
} else {
result = Integer.parseInt(str[i - 1]) - Integer.parseInt(str[i + 1]);
str[i - 1] = String.valueOf(result);
str[i + 1] = String.valueOf(result);
}
}
}
if (result > 100 || result < 0) {
return null;
} else {
for (int i = 0; i < str2.length - 1; i++) {
}
str2[str.length - 1] = String.valueOf(result);
String str3 = str2[0];
for (int i = 1; i < str2.length; i++) {
str3 += str2[i];
}
return str3;
}
}
public static int exact(int a, int b) {
if (a % b != 0) {
b = new Random().nextInt(100) + 1;
return exact(a, b);
}
return b;
}
public static int positive(int a, int b) {
if (a - b < 0) {
b = new Random().nextInt(100) + 1;
return positive(a, b);
}
return b;
}
public static void main(String[] args) throws IOException {
System.out.println("请输入你要生成的四则远算式个数且数字在1-100之间:");
Scanner num = new Scanner(System.in);
int operation = num.nextInt();
if (operation > 0 && operation <= 100) {
ArrayList<String> list = new ArrayList<String>();
list.add("2017012312");
for (int i = 0; i < operation;) {
String str = calculation();
if (str == null) {
continue;
} else {
list.add(str);
i++;
}
}
BufferedWriter cal = new BufferedWriter(new FileWriter("result.txt"));
for (String src : list) {
cal.write(src);
System.out.println(src);
cal.newLine();
}
cal.close();
} else {
System.out.println("对不起,您输入的数字有误错误,请重新输入1-100间的数字~");
}
}
}
四、总结
1、在项目方面还有扩展功能未实现,后期会继续努力补上。
2、在做项目时,开始想的不够全面就开始做,结果不尽人意,后来做不下去了,才又一次进行好好的规划,做了差不多三十几个小时,但是后来调试时,发现了好多bug,然后又一直改代码,又花去好多时间。通过这次自己亲自做项目,了解了做项目不只是需要知识,更多需要是耐心以及经验,同样我也认识到了知识上的不足,此后我会努力学习,充实自己。