小学生算术题生成器
项目相关要求
1.实现一个自动生成小学四则运算题目的命令行程序(也可以用图像界面,具有相似功能)。
1.项目Github地址
我们的项目Github地址:
https://github.com/HQingshan/ArithmeticQuestionGenerator
作者:黄清山3118005094
,钟俊豪3118005122
2.遇到的困难及解决方法
困难:
由于两个使用的电脑操作系统不一样,导致无法使用两人熟悉的语言,因此选择使用的是java语言。因为两个java刚刚入门,算是边学边做。困难巨大。编程语言是不熟悉,导致浪费大量时间。不过确实体会到了java强大的跨平台性。
解决办法:
两人学不一样的模块,一个人做图形界面代码,一个人做逻辑方面的代码。
3.关键代码
主要函数
public static void GenerateQuestion (int range,int number) throws IOException {
File file1=new File("Exercise.txt");
File file2=new File("Answer.txt");
try {
if (!file1.exists()){
file1.createNewFile();
}
if (!file2.exists()){
file2.createNewFile();
}
FileWriter fw1=new FileWriter(file1);
FileWriter fw2=new FileWriter(file2);
BufferedWriter bw1=new BufferedWriter(fw1);
BufferedWriter bw2=new BufferedWriter(fw2);
for (int i=1;i<=number;) {
int a = (int) (Math.random() * range);
int b = (int) (Math.random() * range);
int c = (int) (Math.random() * range);
int d = (int)(Math.random()*range)+1;
int e = (int)(Math.random()*range)+1;
int f = (int)(Math.random()*range)+1;
int fuhao1 = (int) (Math.random() * 4);
int fuhao2 = (int) (Math.random() * 4);
String str;
String str1;
String str2;
if (fuhao1==0){
if (fuhao2==0){
str=i+". "+tozhenfenshu(a,d)+" + "+tozhenfenshu(b,e)+" + "+tozhenfenshu(c,f)+" = ";
String x1=tofenshu(a,d);
String x2=tofenshu(b,e);
String x3=tofenshu(c,f);
str1=add(add(x1,x2),x3);
str2=i+"."+fenshutozhenfenshu(add(add(x1,x2),x3));
bw1.write(str);
bw2.write(str2);
bw1.newLine();
bw2.newLine();
bw1.flush();
bw2.flush();
i++;
}
else if (fuhao2==1){
str=i+". "+tozhenfenshu(a,d)+" + "+tozhenfenshu(b,e)+" - "+tozhenfenshu(c,f)+" = ";
String x1=tofenshu(a,d);
String x2=tofenshu(b,e);
String x3=tofenshu(c,f);
//str1=fenshutozhenfenshu(minus(add(x1,x2),x3));
str1=minus(add(x1,x2),x3);
str2=i+"."+fenshutozhenfenshu(minus(add(x1,x2),x3));
double panduan=getResult(str1);
if (panduan>=0){
bw1.write(str);
bw2.write(str2);
bw1.newLine();
bw2.newLine();
bw1.flush();
bw2.flush();
i++;
}
}
else if (fuhao2==2){
str=i+". "+tozhenfenshu(a,d)+" + "+tozhenfenshu(b,e)+" * "+tozhenfenshu(c,f)+" = ";
String x1=tofenshu(a,d);
String x2=tofenshu(b,e);
String x3=tofenshu(c,f);
str1=add(x1,multiply(x2,x3));
str2=i+"."+fenshutozhenfenshu(add(x1,multiply(x2,x3)));
bw1.write(str);
bw2.write(str2);
bw1.newLine();
bw2.newLine();
bw1.flush();
bw2.flush();
i++;
}
else if (fuhao2==3){
str=i+". " +tozhenfenshu(a,d)+" + "+tozhenfenshu(b,e)+" / "+tozhenfenshu(c,f)+" = ";
String x1=tofenshu(a,d);
String x2=tofenshu(b,e);
String x3=tofenshu(c,f);
if(c!=0) {
str1=add(x1, divide(x2, x3));
str2 = i+"."+fenshutozhenfenshu(add(x1, divide(x2, x3)));
bw1.write(str);
bw2.write(str2);
bw1.newLine();
bw2.newLine();
bw1.flush();
bw2.flush();
i++;
}
}
}
else if (fuhao1==1){
if (fuhao2==0){
str=i+". "+tozhenfenshu(a,d)+" - "+tozhenfenshu(b,e)+" + "+tozhenfenshu(c,f)+" = ";
String x1=tofenshu(a,d);
String x2=tofenshu(b,e);
String x3=tofenshu(c,f);
str1=add(minus(x1,x2),x3);
str2=i+"."+fenshutozhenfenshu(add(minus(x1,x2),x3));
double panduan=getResult(str1);
if (panduan>=0){
bw1.write(str);
bw2.write(str2);
bw1.newLine();
bw2.newLine();
bw1.flush();
bw2.flush();
//System.out.println(str+" "+str1);
i++;
}
}
else if (fuhao2==1){
//str=i+"."+tozhenfenshu(a,d)+"-"+tozhenfenshu(b,e)+"-"+tozhenfenshu(c,f)+"=";
str=i+". "+tozhenfenshu(a,d)+" - "+tozhenfenshu(b,e)+" - "+tozhenfenshu(c,f)+" = ";
String x1=tofenshu(a,d);
String x2=tofenshu(b,e);
String x3=tofenshu(c,f);
str1=minus(minus(x1,x2),x3);
str2=i+"."+fenshutozhenfenshu(minus(minus(x1,x2),x3));
double panduan=getResult(str1);
if (panduan>=0){
bw1.write(str);
bw2.write(str2);
bw1.newLine();
bw2.newLine();
bw1.flush();
bw2.flush();
i++;
}
}
else if (fuhao2==2){
str=i+". "+tozhenfenshu(a,d)+" - "+tozhenfenshu(b,e)+" * "+tozhenfenshu(c,f)+" = ";
String x1=tofenshu(a,d);
String x2=tofenshu(b,e);
String x3=tofenshu(c,f);
str1=minus(x1,multiply(x2,x3));
str2=i+"."+fenshutozhenfenshu(minus(x1,multiply(x2,x3)));
double panduan=getResult(str1);
if (panduan>=0){
bw1.write(str);
bw2.write(str2);
bw1.newLine();
bw2.newLine();
bw1.flush();
bw2.flush();
i++;
}
}
else if (fuhao2==3){
str=i+". "+tozhenfenshu(a,d)+" - "+tozhenfenshu(b,e)+" / "+tozhenfenshu(c,f)+" = ";
String x1=tofenshu(a,d);
String x2=tofenshu(b,e);
String x3=tofenshu(c,f);
if (c!=0) {
str1=minus(x1,divide(x2,x3));
str2=i+"."+fenshutozhenfenshu(minus(x1,divide(x2,x3)));
double panduan = getResult(str1);
if (panduan >= 0) {
bw1.write(str);
bw2.write(str2);
bw1.newLine();
bw2.newLine();
bw1.flush();
bw2.flush();
i++;
//System.out.println(str+" "+str1);
}
}
}
}
else if (fuhao1==2){
if (fuhao2==0){
str=i+". "+tozhenfenshu(a,d)+" * "+tozhenfenshu(b,e)+" + "+tozhenfenshu(c,f)+" = ";
String x1=tofenshu(a,d);
String x2=tofenshu(b,e);
String x3=tofenshu(c,f);
str1=add(multiply(x1,x2),x3);
str2=i+"."+fenshutozhenfenshu(add(multiply(x1,x2),x3));
bw1.write(str);
bw2.write(str2);
bw1.newLine();
bw2.newLine();
bw1.flush();
bw2.flush();
i++;
}
else if (fuhao2==1){
str=i+". "+tozhenfenshu(a,d)+" * "+tozhenfenshu(b,e)+" - "+tozhenfenshu(c,f)+" = ";
String x1=tofenshu(a,d);
String x2=tofenshu(b,e);
String x3=tofenshu(c,f);
str1=minus(multiply(x1,x2),x3);
str2=i+"."+fenshutozhenfenshu(minus(multiply(x1,x2),x3));
double panduan=getResult(str1);
if (panduan>=0){
bw1.write(str);
bw2.write(str2);
bw1.newLine();
bw2.newLine();
bw1.flush();
bw2.flush();
i++;
//System.out.println(str+" "+str1);
}
}
else if (fuhao2==2){
str=i+". "+tozhenfenshu(a,d)+" * "+tozhenfenshu(b,e)+" * "+tozhenfenshu(c,f)+" = ";
String x1=tofenshu(a,d);
String x2=tofenshu(b,e);
String x3=tofenshu(c,f);
str1=multiply(multiply(x1,x2),x3);
str2=i+"."+fenshutozhenfenshu(multiply(multiply(x1,x2),x3));
bw1.write(str);
bw2.write(str2);
bw1.newLine();
bw2.newLine();
bw1.flush();
bw2.flush();
i++;
}
else if (fuhao2==3) {
str=i+". "+tozhenfenshu(a,d)+" * "+tozhenfenshu(b,e)+" / "+tozhenfenshu(c,f)+" = ";
String x1 = tofenshu(a, d);
String x2 = tofenshu(b, e);
String x3 = tofenshu(c, f);
if (c!=0) {
str1=divide(multiply(x1,x2),x3);
str2=i+"."+fenshutozhenfenshu(divide(multiply(x1,x2),x3));
bw1.write(str);
bw2.write(str2);
bw1.newLine();
bw2.newLine();
bw1.flush();
bw2.flush();
i++;
}
}
}
else if (fuhao1==3) {
if (b != 0) {
if (fuhao2 == 0) {
str=i+". "+tozhenfenshu(a,d)+" / "+tozhenfenshu(b,e)+" + "+tozhenfenshu(c,f)+" = ";
String x1 = tofenshu(a, d);
String x2 = tofenshu(b, e);
String x3 = tofenshu(c, f);
str1=add(divide(x1,x2),x3);
str2=i+"."+fenshutozhenfenshu(add(divide(x1,x2),x3));
bw1.write(str);
bw2.write(str2);
bw1.newLine();
bw2.newLine();
bw1.flush();
bw2.flush();
i++;
} else if (fuhao2 == 1) {
str=i+". "+tozhenfenshu(a,d)+" / "+tozhenfenshu(b,e)+" - "+tozhenfenshu(c,f)+" = ";
String x1 = tofenshu(a, d);
String x2 = tofenshu(b, e);
String x3 = tofenshu(c, f);
str1=minus(divide(x1,x2),x3);
str2=i+"."+fenshutozhenfenshu(minus(divide(x1,x2),x3));
double panduan = getResult(str1);
if (panduan >= 0) {
bw1.write(str);
bw2.write(str2);
bw1.newLine();
bw2.newLine();
bw1.flush();
bw2.flush();
i++;
}
}
else if (fuhao2 == 2) {
str=i+". "+tozhenfenshu(a,d)+" / "+tozhenfenshu(b,e)+" * "+tozhenfenshu(c,f)+" = ";
String x1 = tofenshu(a, d);
String x2 = tofenshu(b, e);
String x3 = tofenshu(c, f);
str1=multiply(divide(x1,x2),x3);
str2=i+"."+fenshutozhenfenshu(multiply(divide(x1,x2),x3));
bw1.write(str);
bw2.write(str2);
bw1.newLine();
bw2.newLine();
bw1.flush();
bw2.flush();
i++;
} else if (fuhao2 == 3) {
str=i+". "+ tozhenfenshu(a,d)+" / "+tozhenfenshu(b,e)+" / "+tozhenfenshu(c,f)+" = ";
String x1 = tofenshu(a, d);
String x2 = tofenshu(b, e);
String x3 = tofenshu(c, f);
if (c!=0) {
str1=divide(divide(x1,x2),x3);
str2=i+"."+fenshutozhenfenshu(divide(divide(x1,x2),x3));
bw1.write(str);
bw2.write(str2);
bw1.newLine();
bw2.newLine();
bw1.flush();
bw2.flush();
i++;
//System.out.println(str+" "+str1);
}
}
}
}
}
System.out.println("成功写入数据!");
}catch (IOException e){
e.printStackTrace();
}
}
四则运算的函数
public static String add(String str1,String str2){ //加法
int a[]=new int[4];
//String b[]=new String[4];
java.util.List<String> c=new ArrayList<>();
//ArrayList<Integer> c = new ArrayList<Integer>();
Matcher m = Pattern.compile("\\d+").matcher(str1);
while(m.find()) {
c.add(m.group());
}
Matcher m1=Pattern.compile("\\d+").matcher(str2);
while (m1.find()){
c.add(m1.group());
}
for (int i=0;i<4;i++) {
a[i] = Integer.parseInt(c.get(i));
}
int fenzi=a[0]*a[3]+a[2]*a[1];
int fenmu=a[1]*a[3];
int x=fenzi;
int y=fenmu;
int mod=fenzi%fenmu;
while (mod>0){
x=y;
y=mod;
mod=x%y;
}
fenzi=fenzi/y;
fenmu=fenmu/y;
return fenzi+"/"+fenmu;
}
public static String minus(String str1,String str2){ //减法
int a[]=new int[4];
java.util.List<String> c=new ArrayList<>();
Matcher m=Pattern.compile("\\d+").matcher(str1);
while (m.find()){
c.add(m.group());
}
Matcher m1=Pattern.compile("\\d+").matcher(str2);
while (m1.find()){
c.add(m1.group());
}
for (int i=0;i<4;i++) {
a[i] = Integer.parseInt(c.get(i));
}
int fenzi=a[0]*a[3]-a[1]*a[2];
int fenmu=a[1]*a[3];
int x=fenzi;
int y=fenmu;
int mod=fenzi%fenmu;
while (mod>0){
x=y;
y=mod;
mod=x%y;
}
if (fenzi>=fenmu) {
fenzi = fenzi / y;
fenmu = fenmu / y;
}
return fenzi+"/"+fenmu;
}
public static String multiply(String str1,String str2){ //乘法
int a[]=new int[4];
java.util.List<String> c=new ArrayList<>();
Matcher m=Pattern.compile("\\d+").matcher(str1);
while (m.find()){
c.add(m.group());
}
Matcher m1=Pattern.compile("\\d+").matcher(str2);
while (m1.find()){
c.add(m1.group());
}
for (int i=0;i<4;i++) {
a[i] = Integer.parseInt(c.get(i));
}
int fenzi=a[0]*a[2];
int fenmu=a[1]*a[3];
int x=fenzi;
int y=fenmu;
int mod=fenzi%fenmu;
while (mod>0){
x=y;
y=mod;
mod=x%y;
}
fenzi=fenzi/y;
fenmu=fenmu/y;
return fenzi+"/"+fenmu;
}
public static String divide(String str1,String str2){ //除法
int a[]=new int[4];
String b[]=new String[4];
java.util.List<String> c=new ArrayList<>();
Matcher m=Pattern.compile("\\d+").matcher(str1);
while (m.find()){
c.add(m.group());
}
Matcher m1=Pattern.compile("\\d+").matcher(str2);
while (m1.find()){
c.add(m1.group());
}
for (int i=0;i<4;i++) {
a[i] = Integer.parseInt(c.get(i));
}
int fenzi=a[0]*a[3];
int fenmu=a[1]*a[2];
int x=fenzi;
int y=fenmu;
int mod=fenzi%fenmu;
if (mod!=0) {
while (mod > 0) {
x = y;
y = mod;
mod = x % y;
}
fenzi = fenzi / y;
fenmu = fenmu / y;
}
return fenzi+"/"+fenmu;
}
关于假分数和真分数的处理
public static String tofenshu(int a,int b){ //把两数相除化为假分数
return a+"/"+b;
}
public static String fenshutozhenfenshu(String str){ //把假分数化为真分数
int []a=new int[2];
List<String> c=new ArrayList<>();
Matcher m=Pattern.compile("\\d+").matcher(str);
while (m.find()){
c.add(m.group());
}
for (int i=0;i<2;i++){
a[i]=Integer.parseInt(c.get(i));
}
return tozhenfenshu(a[0],a[1]);
}
3.1测试
运行程序-生成算术题
运行结果
判断答案是否正确如下
结果如下
4.PSP
PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
---|---|---|---|
Planning | 计划 | 60 | 60 |
· Estimate | · 估计这个任务需要多少时间 | 600 | 600 |
Development | 开发 | 600 | 60 |
· Analysis | · 需求分析 (包括学习新技术) | 30 | 30 |
· Design Spec | · 生成设计文档 | 20 | 20 |
· Design Review | · 设计复审 (和同事审核设计文档) | 60 | 60 |
· Coding Standard | · 代码规范 (为目前的开发制定合适的规范) | 30 | 30 |
· Design | · 具体设计 | 30 | 30 |
· Coding | · 具体编码 | 30 | 30 |
· Code Review | · 代码复审 | 30 | 30 |
· Test | · 测试(自我测试,修改代码,提交修改) | 30 | 30 |
Reporting | 报告 | 35 | 35 |
· Test Report | · 测试报告 | 10 | 10 |
· Size Measurement | · 计算工作量 | 0 | 0 |
· Postmortem & Process Improvement Plan | · 事后总结, 并提出过程改进计划 | 0 | 0 |
合计 | 900 | 900 |
学习进度条
第N周 | 新增代码(行) | 累计代码(行) | 本周学习耗时(小时) | 累计学习耗时(小时) | 重要成长 |
---|---|---|---|---|---|
1 | 86 | 86 | 1 | 1 | 熟悉markdown还有git使用 |
2 | 610 | 610 | 15 | 15 | 熟悉java |
... | ... | ... | ... | ... | ... |