四则运算5.2-5.3

开发环境:eclipse

结对同伴 :32 马志磊

同伴博客:http://www.cnblogs.com/mac54321/

程序介绍:

第二阶段目标 - 通过测试程序和API 接口测试其简单的加减乘除功能。

比如:

  • 最多4 个运算符

  • 数值范围是 -1000 到 1000

  • 精度是小数点后两位

第三阶段目标 - 通过测试程序和API 接口测试对于各种参数的支持。

  • 定义要增加什么功能 - 例如:支持 “运算式子格式错误” 异常

  • 写好测试用例,传进去一个错误的式子,期望能捕获这个 异常。 如果没有,那测试就报错。

  • 在 Core 模块中实现这个功能

源代码:

Core.java

import java.text.DecimalFormat;
import java.math.RoundingMode;
public class Core {
	private int MAXNUM=10;//操作数
	private int OP=10;	//运算符
	private int MAXrange=1000;//数值范围 
	private int MINrange=-1000;//数值范围 
	private int JD=2;//计算精度
	public Core(int MAXNUM,int OP,int MAXrange,int MINrange,int JD){
		this.MAXNUM=MAXNUM;
		this.OP=OP;
		this.MAXrange=MAXrange;
		this.MINrange=MINrange;
		this.JD=JD;
	}
	public Core(){
		this.MAXNUM=10;
		this.OP=10;	
		this.MAXrange=1000;
		this.MINrange=-1000;
		this.JD=2;
	}
	public void setMAXNUM(int MAXNUM){
		this.MAXNUM=MAXNUM;
	}
	public void setOP(int OP){
		this.OP=OP;
	}
	public void setMAXrange(int MAXrange){
		this.MAXrange=MAXrange;
	}
	public void setMINrange(int MINrange){
		this.MINrange=MINrange;
	}
	public void setJD(int JD){
		this.JD=JD;
	}
	
	public String jisuan(String str) throws cuowu  {
		String num[] =new String[MAXNUM];
		int number[] =new int[MAXNUM];
		char operator[]=new char [OP];
		int brackets=0,endbrackets=0;
		int posbra=0,endposbra=0;
		int j;int k;
		double result=0;
		boolean flag;
		for(int i=0;i<MAXNUM;i++)
		{
			num[i]="";
			number[i]=0;
		}
		j=0;k=0;flag=false;
		for(int i=0;str.charAt(i)!='=';i++)
		{
			if(str.charAt(i)>='0' && str.charAt(i)<='9' ||  str.charAt(i)=='.')
			{
				flag=false;
				num[j]=num[j]+ str.charAt(i);
			}
			else if(str.charAt(i)=='+' || str.charAt(i)=='-' || str.charAt(i)=='*' || str.charAt(i)=='/')
			{
				if(flag==true) throw new cuowu("公式错误!不能连续输入运算符");
				flag=true;
				j++;
				if(j>MAXNUM-1)
					throw new cuowu("操作数不能超过"+MAXNUM+"个!");
				if(k>OP-1)
					throw new cuowu("运算符不能超过"+OP+"个!");
				operator[k]=str.charAt(i);
				k++;
			}
			else if(str.charAt(i)=='(')
			{
				brackets++;
				posbra=i;
			}else if(str.charAt(i)==')'){
				endbrackets++;
			}
		}	
		if(brackets!=endbrackets)
			throw new cuowu("括号不对称!");

		for(int i=0;i<=j;i++)
			if(Double.parseDouble(num[i])>MAXrange || Double.parseDouble(num[i])<MINrange)
				 throw new cuowu("数值范围出错!");
		 DecimalFormat formater = new DecimalFormat();
		 formater.setMaximumFractionDigits(JD);
		 formater.setGroupingSize(0);
		 formater.setRoundingMode(RoundingMode.FLOOR);
		if(j==0)
			return String.valueOf(formater.format(Double.parseDouble(num[0])));
		else
		{
			int pos=-1;
			String str2=new String("");
			for(int i=0;i<=k;i++)
			{
				if(operator[i]=='*' || operator[i]=='/')
					pos=i;
				if(operator[i]=='/')
					if(Double.parseDouble(num[i+1])==0)
						 throw new cuowu("公式错误!除数不能为0");
			}
			if(pos==-1)
				pos=0;
			for(int i=0;i<pos;i++)
				str2=str2+num[i]+String.valueOf(operator[i]);
			str2=str2+op(Double.parseDouble(num[pos]),Double.parseDouble(num[pos+1]),operator[pos])+String.valueOf(operator[pos+1]);
			for(int i=pos+2;i<=j;i++)
				str2=str2+num[i]+String.valueOf(operator[i]);
			//System.out.println(str2);	
			//System.out.println(op(Double.parseDouble(num[0]),Double.parseDouble(num[1]),operator[0])+String.valueOf(operator[1])+str2+"=");
		//	System.out.println("返回答案"+op(Double.parseDouble(num[0]),Double.parseDouble(num[1]),operator[0]));
			return jisuan(str2+"=");
		}
		}
	public double op(double a,double b,char operator)
	{
		switch(operator)
		{
		case '+':
			return a+b;
		case '-':
			return a-b;		
		case '*':
			return a*b;	
		case '/':
			return a/b;	
		}
	return 0;
	}
	
}
class cuowu extends Exception 
{ 
    public cuowu(String msg) 
    { 
        super(msg); 
    } 
}  

Calculator.java

public class Calculator {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String exp=new String("10.5222+10.222=");
		Core c=new Core(10,10,500,-500,4);
		try{
			String result=c.jisuan(exp);
			System.out.print(result);
		}
		catch(cuowu e){
			e.printStackTrace();
		}
	}
}

  我们在原来5.1的基础上增加了自定义异常 和各种参数设置。例如,最多几个运算符,数据范围是多少,还要设置计算的精度(保留几位小数)

测试结果

(1)计算的精度(保留几位小数)

(2)测试数值范围 超过500 就会出错 抛出异常

 (3)测试运算符 超过4个就会出错 抛出异常

(4)测试操作数 超过5个就会出错 抛出异常

出现的问题及解决方法:

  我们在小数点精度不知道如何进行做,然后我们通过网上学习,发现了DecimalFormat()类可以实现这个问题,我们通过学习与消化,把小数精度问题顺利解决了!

原文地址:https://www.cnblogs.com/383237360q/p/4487923.html