码迷,mamicode.com
首页 > 编程语言 > 详细

java实现四则运算

时间:2018-10-01 00:13:31      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:opera   网上   ice   oop   return   review   evel   较差   substring   

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

 

三、代码

技术分享图片

技术分享图片

技术分享图片

技术分享图片

技术分享图片

技术分享图片

技术分享图片

技术分享图片

技术分享图片

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;
}
}

技术分享图片

技术分享图片

四、运行结果

技术分享图片

技术分享图片

技术分享图片

技术分享图片技术分享图片

技术分享图片

 

技术分享图片技术分享图片

 

 

 五、总结

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

 

java实现四则运算

标签:opera   网上   ice   oop   return   review   evel   较差   substring   

原文地址:https://www.cnblogs.com/zzh81/p/9733678.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!