本次使用java语言,实现了四则运算习题的生成。
一、主要功能:
(1)算式个数
(2)是否有乘除法
(3)结果集数值范围
(4)加减法有无负数
(5)除法有无余数
(6)除法出现小数是否支持分数显示
(7)选择生成算式导入的文件
(8)输出打印每行个数
二、代码实现:
(1)IFormulaGeneration
package cn.zhl.software;
import java.util.Map;
public interface IFormulaGeneration {
//加法生成接口,结果范围,加法有无负数,生成个数
public Map<String,Integer> Add(int resultRange,boolean is,int num);
//减法生成接口,结果范围,减法有无负数,生成个数
public Map<String,Integer> Sub(int resultRange,boolean is,int num);
//乘法生成接口,生成个数
public Map<String,Integer> Mul(int resultRange,int num);
//除法生成接口,结果范围,除法有无余数,是否支持分数,生成个数
public Map<String,String> Div(int resultRange,boolean is,boolean is2,int num);
//检测算式是否存在重复,如果存在返回true
public boolean Repeat(Map<?,?> map,String formula);
//生成特定范围的数值
public int nextInt(int min, int max);
//化简小数
public String fracReduction(int numerator, int denominator);
//算法生成器:算式个数,是否有乘除法,数值范围,加减法有无负数,除法又无余数,是否支持分数,打印每行个数
public Map<String,?> FormulaCustom(int formulaNum,boolean if_MulDiv,int range,
boolean ifNeg_AddSub,boolean ifRem_Div,
boolean ifRed_Div,int lineNum);
}
(2)FormulaRealization
package cn.zhl.software;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Set;
public class FormulaRealization implements IFormulaGeneration {
Random random = new Random();
@Override
public Map<String, Integer> Add(int resultRange, boolean is, int num) {
if (resultRange < 0) {
resultRange = -resultRange;
}
Map<String, Integer> addMap = new HashMap<>();
int r1 = 0, r2 = 0, n = 0;
String formula = "";
for (; n < num; ) {
r1 = nextInt(-resultRange, resultRange);
if (is) {//加法允许出现负数
r2 = nextInt(-resultRange - r1, resultRange - r1);
} else {
r2 = nextInt(-r1, resultRange - r1);
}
formula = (r1 < 0 ? "(" + r1 + ")" : r1) + "+" + (r2 < 0 ? "(" + r2 + ")" : r2);
if (Repeat(addMap, formula)) {
addMap.put(formula, r1 + r2);
n++;
}
}
return addMap;
}
@Override
public Map<String, Integer> Sub(int resultRange, boolean is, int num) {
if (resultRange < 0) {
resultRange = -resultRange;
}
Map<String, Integer> subMap = new HashMap<>();
int r1 = 0, r2 = 0, n = 0;
String formula = "";
for (; n < num; ) {
r1 = nextInt(-resultRange, resultRange);
if (is) {//加法允许出现负数
r2 = nextInt(-resultRange + r1, resultRange + r1);
} else {
r2 = nextInt(r1, resultRange + r1);
}
formula = (r1 < 0 ? "(" + r1 + ")" : r1) + "-" + (r2 < 0 ? "(" + r2 + ")" : r2);
if (Repeat(subMap, formula)) {
subMap.put(formula, r1 - r2);
n++;
}
}
return subMap;
}
@Override
public Map<String, Integer> Mul(int resultRange, int num) {
if (resultRange == 0) {
resultRange = 1;
}
if (resultRange < 0) {
resultRange = -resultRange;
}
Map<String, Integer> mulMap = new HashMap<>();
int r1 = 0, r2 = 0, n = 0;
String formula = "";
for (; n < num; ) {
while (r1 == 0) {
r1 = nextInt(-resultRange, resultRange);
}
r2 = nextInt(-(int) (resultRange / Math.abs(r1)), (int) (resultRange / Math.abs(r1)));
formula = (r1 < 0 ? "(" + r1 + ")" : r1) + "*" + (r2 < 0 ? "(" + r2 + ")" : r2);
if (Repeat(mulMap, formula)) {
mulMap.put(formula, r1 * r2);
n++;
r1 = nextInt(-resultRange, resultRange);
}
}
return mulMap;
}
@Override
public Map<String, String> Div(int resultRange, boolean is, boolean is2, int num) {
if (resultRange == 0) {
resultRange = 1;
}
if (resultRange < 0) {
resultRange = -resultRange;
}
Map<String, String> divMap = new HashMap<>();
int r1 = 0, r2 = 0, n = 0;
String formula = "";
for (; n < num; ) {
//r1 = nextInt(-resultRange, resultRange);
while (r2 == 0) {
r2 = nextInt(-resultRange, resultRange);
}
if (!is) {//除法没有余数
r1 = r2 * nextInt(-(resultRange), resultRange);
} else {//有余数
r1 = nextInt(-resultRange, resultRange);
}
formula = (r1 < 0 ? "(" + r1 + ")" : r1) + "http://www.likecs.com/" + (r2 < 0 ? "(" + r2 + ")" : r2);
if (Repeat(divMap, formula)) {
String result = "";
if (is && is2) {//有余数且化为分数
if(r1*r2<0){
result = "-"+fracReduction(Math.abs(r1), Math.abs(r2));
}else {
result = fracReduction(Math.abs(r1), Math.abs(r2));
}
if(r1%r2==0){
result = ((double) r1 / r2) + "";
}
} else {
if (!is) {//无余数
result = ((double) r1 / r2) + "";
}
if (is) {
result = ((double) r1 / r2) + "";
}
}
if(r1==0){
result=0.0+"";
}
if (r1==-r2){
result="-1.0";
}
divMap.put(formula, result);
n++;
}
}
return divMap;
}
@Override
public boolean Repeat(Map<?, ?> map, String formula) {
if (map.isEmpty()) {
return true;
} else {
Set<String> strings = (Set<String>) map.keySet();
for (String string : strings) {
//如果当前算式与前面算式重复返回false
if (string.equals(formula)) {
return false;
}
}
//如果当前算式与前面算式不存在重复返回true
return true;
}
}
@Override
public int nextInt(int min, int max) {
if (min == max) {
return max;
}
return random.nextInt(max - min + 1) + min;
}
@Override
public String fracReduction(int numerator, int denominator) {
//找到最大公约数,然后分别处以最大公约数
int m = numerator;
int n = denominator;
int r;
while (numerator > 0) {
r = denominator % numerator;
denominator = numerator;
numerator = r;
}
// if ((m / denominator)==-(n / denominator)){
// return "-1.0";
// }
return m / denominator + "http://www.likecs.com/" + n / denominator;
}
@Override
//算法生成器:算式个数,是否有乘除法,数值范围,加减法有无负数,除法又无余数,是否支持分数,打印每行个数
public Map<String, ?> FormulaCustom(int formulaNum, boolean if_MulDiv, int range, boolean ifNeg_AddSub, boolean ifRem_Div, boolean ifRed_Div, int lineNum) {
int add = 0, sub = 0, mul = 0, div = 0;
add = nextInt(formulaNum/5,formulaNum/3);
sub = nextInt((formulaNum - add)/4,(formulaNum - add)/2);
mul = nextInt((formulaNum - add - sub)/3,(formulaNum - add - sub)/1);
div = formulaNum - add - sub - mul;
Map map = new HashMap();
if (if_MulDiv) {//如果存在乘除法将算式总数分为四份
Map<String, Integer> addMap = Add(range, ifNeg_AddSub, add);
Map<String, Integer> subMap = Sub(range, ifNeg_AddSub, sub);
Map<String, Integer> mulMap = Mul(range, mul);
Map<String, String> divMap = Div(range, ifRem_Div, ifRed_Div, div);
map.putAll(addMap);
map.putAll(subMap);
map.putAll(mulMap);
map.putAll(divMap);
} else {//不存在则分为两份
Map<String, Integer> addMap = Add(range, ifNeg_AddSub, add);
Map<String, Integer> subMap = Sub(range, ifNeg_AddSub, sub);
map.putAll(addMap);
map.putAll(subMap);
}
return map;
}
}
(3)FormulaRealizationTest
package cn.zhl.test;
import cn.zhl.software.FormulaRealization;
import org.junit.Test;
import java.util.Map;
import java.util.Set;
public class FormulaRealizationTest {
FormulaRealization formulaRealization = new FormulaRealization();
@Test
public void testAdd() {
Map<String, Integer> add = formulaRealization.Add(100, false, 5);
for (String s : add.keySet()) {
System.out.print(s+"="+add.get(s)+" ");
}
}
@Test
public void testSub() {
Map<String, Integer> sub = formulaRealization.Sub(100, true, 5);
for (String s : sub.keySet()) {
System.out.print(s+"="+sub.get(s)+" ");
}
}
@Test
public void testMul() {
Map<String, Integer> mul = formulaRealization.Mul(100, 5);
for (String s : mul.keySet()) {
System.out.print(s+"="+mul.get(s)+" ");
}
}
@Test
public void testDiv() {
Map<String, String> div = formulaRealization.Div(100, true, true, 5);
for (String s : div.keySet()) {
System.out.print(s+"="+div.get(s)+" ");
}
}
@Test
public void test1() {
String div = formulaRealization.fracReduction(25,5);
System.out.print(div);
}
}
(4)IFileGeneration
package cn.zhl.fileCreate;
import java.util.Map;
public interface IFileGeneration {
//用于将产生的算式写入到文件中
public void fileShow(Map<String,?> stringMap,int lineNum,String fileName);
}
(5)FileRealization
package cn.zhl.fileCreate;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
public class FileRealization implements IFileGeneration {
@Override
public void fileShow(Map<String,?> stringMap, int lineNum, String fileName) {
int n=0;
String answerName=fileName+"_Answer.txt";
fileName=fileName+".txt";
/*File file1 = new File("\\cn\\zhl\\formulaFile\\" + fileName);
File file2 = new File("\\cn\\zhl\\formulaFile\\" + answerName);*/
File file1 = new File("Arithmetic_question_generation_system\\src\\cn\\zhl\\formulaFile\\"+fileName);
File file2 = new File("Arithmetic_question_generation_system\\src\\cn\\zhl\\formulaFile\\"+answerName);
for (String s : stringMap.keySet()) {
n++;
try(FileWriter formulaWriter = new FileWriter(file1,true); FileWriter answerWriter = new FileWriter(file2,true)) {
formulaWriter.write(s+"= ");
answerWriter.write(s+"="+stringMap.get(s)+" ");
if(n%lineNum==0){
formulaWriter.write("\n");
answerWriter.write("\n");
}
} catch (IOException e) {
e.printStackTrace();