四则运算

计应192 第五组 马冰霞:

一、主要功能:

(1)算式个数

(2)是否有乘除法

(3)结果集数值范围

(4)加减法有无负数

(5)除法有无余数

(6)除法出现小数是否支持分数显示

(7)选择生成算式导入的文件

(8)输出打印每行个数

二、代码实现:

(1)IFormulaGeneration

package cn.zhl.software;

import java.util.Map;

public interface IFormulaGeneration {
//加法生成接口,结果范围,加法有无负数,生成个数

public Map Add(int resultRange,boolean is,int num);

//减法生成接口,结果范围,减法有无负数,生成个数

public Map Sub(int resultRange,boolean is,int num);

//乘法生成接口,生成个数

public Map Mul(int resultRange,int num);

//除法生成接口,结果范围,除法有无余数,是否支持分数,生成个数

public Map 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 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 Add(int resultRange, boolean is, int num) {
if (resultRange < 0) {
resultRange = -resultRange;

}

Map 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 Sub(int resultRange, boolean is, int num) {
if (resultRange < 0) {
resultRange = -resultRange;

}

Map 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 Mul(int resultRange, int num) {
if (resultRange == 0) {
resultRange = 1;

}

if (resultRange < 0) {
resultRange = -resultRange;

}

Map 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 Div(int resultRange, boolean is, boolean is2, int num) {
if (resultRange == 0) {
resultRange = 1;

}

if (resultRange 

resultRange = -resultRange;

}

Map divMap = new HashMap<>();

int r1 = 0, r2 = 0, n = 0;

String formula = "";

for (; n 

//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 

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 strings = (Set) 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 + "/" + n / denominator;

}

@Override

//算法生成器:算式个数,是否有乘除法,数值范围,加减法有无负数,除法又无余数,是否支持分数,打印每行个数

public Map 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 addMap = Add(range, ifNeg_AddSub, add);

Map subMap = Sub(range, ifNeg_AddSub, sub);

Map mulMap = Mul(range, mul);

Map divMap = Div(range, ifRem_Div, ifRed_Div, div);

map.putAll(addMap);

map.putAll(subMap);

map.putAll(mulMap);

map.putAll(divMap);

} else {//不存在则分为两份

Map addMap = Add(range, ifNeg_AddSub, add);

Map 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 add = formulaRealization.Add(100, false, 5);

for (String s : add.keySet()) {
System.out.print(s+"="+add.get(s)+"   ");

}

}

@Test

public void testSub() {
Map sub = formulaRealization.Sub(100, true, 5);

for (String s : sub.keySet()) {
System.out.print(s+"="+sub.get(s)+"   ");

}

}

@Test

public void testMul() {
Map mul = formulaRealization.Mul(100, 5);

for (String s : mul.keySet()) {
System.out.print(s+"="+mul.get(s)+"   ");

}

}

@Test

public void testDiv() {
Map 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 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 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();

System.out.println("未成功将算式保存到"+fileName+"中,答案保存到"+answerName+"中,请联系开发人员!");

}

}

System.out.println("已成功将算式保存到"+fileName+"中,答案保存到"+answerName+"中,欢迎您的下次使用!");

}

}

(6)FormulaCustomTest

package cn.zhl.software;

import cn.zhl.fileCreate.FileRealization;

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Map;

import java.util.Scanner;

public class FormulaCustomTest {
public static void main(String[] args) {
int formulaNum;

boolean if_MulDiv;int range;

boolean ifNeg_AddSub;boolean ifRem_Div;

boolean ifRed_Div;int lineNum;

Scanner scanner = new Scanner(System.in);

System.out.println("请输入需要算式个数:");

formulaNum=scanner.nextInt();

System.out.println("请输入需要乘除法(Y/N):");

if(scanner.next().equals("y")||scanner.next().equals("Y")){
if_MulDiv=true;

}else {
if_MulDiv=false;

}

System.out.println("请输入需要结果集范围(默认-n~n):");

range=scanner.nextInt();

System.out.println("请输入允许加减法出现负数(Y/N):");

if(scanner.next().equals("y")||scanner.next().equals("Y")){
ifNeg_AddSub=true;

}else {
ifNeg_AddSub=false;

}

System.out.println("请输入允许除法出现小数(Y/N):");

if(scanner.next().equals("y")||scanner.next().equals("Y")){
ifRem_Div=true;

}else {
ifRem_Div=false;

}

System.out.println("请输入允许除法结果换算成分数(Y/N):");

if(scanner.next().equals("y")||scanner.next().equals("Y")){
ifRed_Div=true;

}else {
ifRed_Div=false;

}

System.out.println("请输入算式每行个数:");

lineNum=scanner.nextInt();

//文件名

String fileName="";

System.out.println("请输入算式需要导入的文件名:");

fileName=scanner.next();

System.out.println("定制完成,正在随机生成算式。。。。");

long l = System.currentTimeMillis();

//算法生成器:算式个数,是否有乘除法,数值范围,加减法有无负数,除法又无余数,是否支持分数,打印每行个数

FormulaRealization formulaRealization = new FormulaRealization();

Map stringMap = formulaRealization.FormulaCustom(formulaNum,if_MulDiv,range,

ifNeg_AddSub,ifRem_Div,

ifRed_Div,lineNum);

long l2 = System.currentTimeMillis();

System.out.println("算式生成成功,耗时"+(l2-l)+"毫秒!");

FileRealization fileRealization = new FileRealization();

fileRealization.fileShow(stringMap,lineNum,fileName);

}

}

package demo;

import java.util.Random;

public class math {
public static void main(String[] args) {
String[] operate=new String[]{"+","-","×","÷"};

int[] numbers=new int[1000];

for(int i=1;i<=1000;i++){
numbers[i-1]=i;

}

Random r=new Random();

for(int i=0;i<300;i++){
System.out.println(numbers[r.nextInt(1000)]+operate[r.nextInt(4)]+numbers[r.nextInt(1000)]+"=");

}

}

}

 

 

 

 

 

 

预计耗时(分钟)

实际耗时(分钟)

Planning

计划

30

20

Estimate

估计这个任务需要多少时间

30

20

Development

开发

170

330

Analysis

需求分析

30

60

Design Spec

生成设计文档

20

30

Design Review

设计复审(与同事审核设计文档)

/

/

Coding Standerd

代码规范(为目前的开发制定合适的规范)

/

/

Design

具体设计

120

240

Coding

具体编码

60

60

Code Review

代码复审

60

60

Text

测试(自测,修改代码,提交修改)

30

30

Reporting

报告

60

60

Text Report

测试报告

55

55

Size MeaSurement

计算工作量

5

5

Postmortem & Process Improvement Plan

事后总结,并提出过程改进计划

/

/

Sum

合计

410

560

posted @ 2021-04-11 22:30  陈十十  阅读(321)  评论(0编辑  收藏  举报