Java接口四个类四则运算_java实现四则运算

Github项目链接:https://github.com/zzhzzhzhangzihan/myapp

一、项目相关要求

1. 使用 -n 参数控制生成题目的个数(实现)

2.使用 -r 参数控制题目中数值(自然数、真分数和真分数分母)的范围(实现)

3.生成的题目中计算过程不能产生负数,也就是说算术表达式中如果存在形如e1 − e2的子表达式,那么e1 ≥ e2(实现)

4.生成的题目中如果存在形如e1 ÷ e2的子表达式,那么其结果应是真分数(实现)

5.每道题目中出现的运算符个数不超过3个。(实现)

6. 程序一次运行生成的题目不能重复(实现)

7.在生成题目的同时,计算出所有题目的答案,并存入执行程序的当前目录下的Answers.txt文件(未实现)

8. 程序应能支持一万道题目的生成。(实现)

9. 程序支持对给定的题目文件和答案文件(未实现),判定答案中的对错并进行数量统计(未实现)

二、PSP

PSP2.1

Personal Software Process Stages

预估耗时(分钟)

实际耗时(分钟)

Planning

计划

60

40

· Estimate

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

1200

1500

Development

开发

1400

1550

· Analysis

· 需求分析 (包括学习新技术)

90

60

· Design Spec

· 生成设计文档

100

120

· Design Review

· 设计复审 (和同事审核设计文档)

60

40

· Coding Standard

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

30

20

· Design

· 具体设计

60

60

· Coding

· 具体编码

500

500

· Code Review

· 代码复审

30

30

· Test

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

250

300

Reporting

报告

90

90

· Test Report

· 测试报告

50

50

· Size Measurement

· 计算工作量

30

20

· Postmortem & Process Improvement Plan

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

90

90

合计

1600

1860

三、代码

291a02904bd7f7afb7056e3d695f110c.png

7106d530c009473317d6724c3483b9d1.png

f08626baed9ad3a2f5c1f4f11fd537e2.png

d6a5c0f07aeff18b54e73557c98bde2e.png

58acb1a3eab55a3ccb19801ae55aae62.png

41421968bbbe253bea356a199f82e233.png

3040265ea64e47aa98f808057f21e2ca.png

317fde1f18dfcca0c0784af123ffad76.png

676b5f61bd098fb7b561463c280f7082.png

package myapp;

import java.util.Random;

public class Expression {

Random random=new Random();

Calc c=new Calc();

public Fraction fraction_create(int r) {

int choose=random.nextInt(2)+1;

int denominator=1;

int numerator=0;

if(choose==1) {

numerator=random.nextInt(r);

denominator=1;

}else {

denominator=random.nextInt(r)+1;

numerator=random.nextInt(r*r+1);

while(numerator/denominator>=r) {

denominator=random.nextInt(r)+1;

numerator=random.nextInt(r+1);}

}

return new Fraction( numerator , denominator );

}

public char operator_create() {

int oper=random.nextInt(4);

char sign;

switch (oper) {

case 0:

sign='+';

break;

case 1:

sign='-';

break;

case 2:

sign='×';

break;

case 3:

sign='÷';

break;

default:

sign='+';

}

return sign;

}

public String getexp(int r) {

String expression="";

int ran=random.nextInt(3);

switch (ran) {

case 0:

expression=oneopexp(r);

break;

case 1:

expression=twoopexp(r);

break;

case 2:

expression=threeopexp(r);

break;

}

return expression;

}

public String oneopexp(int r) {

Fraction f1=fraction_create(r);

Fraction f2=fraction_create(r);

char op=operator_create();

String exp ="";

switch (op) {

case '+':

exp= f1+" + "+f2;

break;

case '-':

if(!f1.isgreaterthan2(f2)) {

Fraction temp;

temp=f1;f1=f2;f2=temp;

}

exp= f1+" - "+f2;

break;

case '×':

exp= f1+" × "+f2;

break;

case '÷':

while(f2.isZero()) {

f2=fraction_create(r);

}

exp= f1+" ÷ "+f2;

break;

}

return exp;

}

public String twoopexp(int r){

Fraction f1=fraction_create(r);

Fraction f2=fraction_create(r);

Fraction f3=fraction_create(r);

char op1=operator_create();

char op2=operator_create();

String exp ="";

String exp1="";

switch (op1) {

case '+':

exp= f1+" + "+f2;

break;

case '-':

if(!f1.isgreaterthan2(f2)) {

Fraction temp;

temp=f1;f1=f2;f2=temp;

}

exp= f1+" - "+f2;

break;

case '×':

exp= f1+" × "+f2;

break;

case '÷':

while(f2.isZero()) {

f2=fraction_create(r);

}

exp= f1+" ÷ "+f2;

break;

}

switch (op2) {

case '+':

exp1=exp+" + "+f3;

break;

case '-':

if(!c.calculate(exp).isgreaterthan2(f3)) {

exp1= f3+" - "+"("+exp+")";

}else {

exp1= exp+" - "+f3;}

break;

case '×':

exp1= "("+exp+")"+" × "+f3;

break;

case '÷':

while(f3.isZero()) {

f3=fraction_create(r);

}

exp1="("+exp+")"+" ÷ "+f3;

break;

}

return exp1;

}

public String threeopexp(int r){

Fraction f1=fraction_create(r);

Fraction f2=fraction_create(r);

Fraction f3=fraction_create(r);

Fraction f4=fraction_create(r);

char op1=operator_create();

char op2=operator_create();

char op3=operator_create();

String exp ="";

String exp1="";

String exp2="";

switch (op1) {

case '+':

exp= f1+" + "+f2;

break;

case '-':

if(!f1.isgreaterthan2(f2)) {

Fraction temp;

temp=f1;f1=f2;f2=temp;

}

exp= f1+" - "+f2;

break;

case '×':

exp= f1+" × "+f2;

break;

case '÷':

while(f2.isZero()) {

f2=fraction_create(r);

}

exp= f1+" ÷ "+f2;

break;

}

switch (op2) {

case '+':

exp1=exp+" + "+f3;

break;

case '-':

if(!c.calculate(exp).isgreaterthan2(f3)) {

exp1= f3+" - "+"("+exp+")";

}else {

exp1= exp+" - "+f3;}

break;

case '×':

exp1= "("+exp+")"+" × "+f3;

break;

case '÷':

while(f3.isZero()) {

f3=fraction_create(r);

}

exp1="("+exp+")"+" ÷ "+f3;

break;

}

switch (op3) {

case '+':

exp2=exp1+" + "+f4;

break;

case '-':

if(!c.calculate(exp1).isgreaterthan2(f4)) {

exp2= f4+" - "+"("+exp1+")";

}else {

exp2= exp1+" - "+f4;}

break;

case '×':

exp2= "("+exp1+")"+" × "+f4;

break;

case '÷':

while(f4.isZero()) {

f4=fraction_create(r);

}

exp2="("+exp1+")"+" ÷ "+f4;

break;

}

return exp2;

}

}

package myapp;

public class Fraction {

private int a;

private int b;

public Fraction(int a, int b) {

this.a = a;

if (b == 0) {

throw new ArithmeticException("分母不能为零");

} else {

this.b = b;

}

simple();

}

private Fraction simple() {

int gcd = this.gcd(this.a, this.b);

this.a /= gcd;

this.b /= gcd;

return this;

}

private int gcd(int a, int b) {

int mod = a % b;

if (mod == 0) {

return b;

} else {

return gcd(b, mod);

}

}

public Fraction add(Fraction second) {//加法

return new Fraction(this.a * second.b + second.a * this.b,

this.b * second.b);

}

public Fraction sub(Fraction second) {//减法

return new Fraction(this.a * second.b - second.a * this.b,

this.b * second.b);

}

public Fraction mult(Fraction second) {//乘法

return new Fraction(this.a*second.a,

this.b * second.b);

}

public Fraction div(Fraction second) {//除法

return new Fraction(this.a*second.b,

this.b * second.a);

}

public String toString() {

if (this.b==1) {

return String.valueOf(a);

}

else if(this.a>this.b) {

int c=0;

c=this.a/this.b;

this.a=this.a%this.b;

return String.valueOf(c)+"'"+String.valueOf(a)+"/"+String.valueOf(b);

}

else{

return String.valueOf(a)+"/"+String.valueOf(b);}

}

public static Fraction tofraction(String str) {

str=str.replaceAll(" ", "");

int a=1;

int b=1;

int s1=str.indexOf("'");

int s2=str.indexOf("/");

if(s1!=-1) {

int c=Integer.valueOf(str.substring(0,s1));

b=Integer.valueOf(str.substring(s2+1));

a=c*b+Integer.valueOf(str.substring(s1+1,s2));

} else if(s2!=-1) {

b=Integer.valueOf(str.substring(s2+1));

a=Integer.valueOf(str.substring(0,s2));

} else {

a=Integer.valueOf(str);

b=1;

}

return new Fraction(a,b);

}

public boolean isgreaterthan2(Fraction f) {

int z=this.a * f.b - f.a * this.b;

if(z>0) {return true;}

else return false;

}

public boolean isZero() {

return a == 0;

}

}

fdfdabf476eca20f8f6994a15d52d01c.png

4822f51d8742c98dcd9faaee8255d0ef.png

四、运行结果

0246d42bdc117f08434ba9902cf97fa3.png

7572f19f61f4866d5aeb6275a42f65ca.png

6a27ef1e8d101cb1110ff8bfbbcd7e70.png

9b6e6ccef6e19e32cf8802d0de16ecb7.png

9bb233da6872c7ea02dfa30ae39b1d67.png

f88f54413bcf310b5cdd10fe6d51c210.png

ad003c09b8c0f6f001141715c43b24da.png

55b2b89c664a0dc5fa8a84cdf57e75f6.png

五、总结

这次任务要求是两个人完成,但是由于忘了找结对对象,再加上自己基础比较差,就自己一个人作了作,一开始完全没思路,还是借鉴了很多网上和先做完的同学的博客,才找到了思路,并且只完成了一些很基础的东西。但是在完成作业的过程中,还是回忆起了不少知识的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值