java 练习

public class Tree{
	
	   public static void main(String[] args) {
			String exp1="2+3*5";
			String exp2="((1+4)*2+10)-(1+5)";
			int result1=Count(exp1);
			int result2=Count(exp2);
			System.out.println(exp1+" = "+result1);
			System.out.println(exp2+" = "+result2);
		}
	
	public Node root;
	public Tree(Node root){
		this.root=root;
	}
	public Tree(){}
	
//判断一个字符串是否是数字				
	private static boolean isNum(String s){
		char[] a=s.toCharArray();
		for(int i=0;i<a.length;i++){
			if(a[i]<'0'||a[i]>'9'){
				return false;
			}
		}
		return true;
	}
//加入元素方法,可以加入数字字符串,符号字符串,ch是加入的字符串变量,preNode是上一个节点		
	public  Node addElement(String ch,Node preNode){
		Node node=new Node(ch);
		if(isNum(ch)){
			if(root==null){					
				this.root=node;
			}else{					
				preNode.rightNode=node;										
			}	
		}else if(ch.equals("+")||ch.equals("-")){
			
			node.leftNode=this.root;
			this.root=node;
		}else if(ch.equals("*")||ch.equals("/")){
//这里是成为上一个节点的父节点	的操作,由于子节点还没有提供方法指向父节点,所以这里采用了调换节点中存储的text方式来实现		
			node.text=preNode.text;
			preNode.text=ch;
			preNode.leftNode=node;
			return preNode;
			
		}
		
	   return node;
	}
	
//递归的方式进行计算,和树的后序遍历方法一致		
	public int count(Node node){					
			if("+".equals(node.text)){
				return count(node.leftNode)+count(node.rightNode);
				
			}else if("-".equals(node.text)){
				return count(node.leftNode)-count(node.rightNode);
				
			}else if("*".equals(node.text)){
				return count(node.leftNode)*count(node.rightNode);
				
			}else if("/".equals(node.text)){
				return count(node.leftNode)/count(node.rightNode);				
			}else{
				
				return Integer.parseInt(node.text);
			}
	
	}
//静态方法,读取算式的文本信息,逐个元素加入到树中,最后根据构建好的树进行四则运算		
	public static int countZ(String exp){
		Tree tree=new Tree();				
		
		Node pre=null;			
		int i=1;			
		StringBuilder numS=new StringBuilder();
		//flag用于标记截取的字符串是否是数字
		int flag=0;
		while(i<=exp.length()){
			String s=exp.substring(i-1,i);
			if(isNum(s)){
			//判断截取字符是数字需要进一步判断下一个字符
				numS.append(s);
				flag=1;
				if(i==exp.length()){
					tree.addElement(numS.toString(),pre);																														
				}
			}else{
			    //上一个字符是数字,此字符是运算符
				if(flag==1){						
					Node node=tree.addElement(numS.toString(),pre);						
					Node nodeC=tree.addElement(s,node);
					pre=nodeC;
					flag=0;
					numS.delete(0,numS.length());
				}else{
				//上一个字符不是数字,直接加入树中
					Node nodeC=tree.addElement(s,pre);
					pre=nodeC;
				}
				
			}
			i++;
		}
		return tree.count(tree.root);		
	}
	
	public static int Count(String exp){
        //括号起点
		int start=exp.indexOf("(");
		//与上面"("对应的")"位置
		int end=GetEnd(exp);
		//没有括号后跳出
		if(start<0){
			return Tree.countZ(exp);
		}
		//截取到子字符串表达式
		String subExp=exp.substring(start+1,end);
		//将子字符串表达式运算结果替换原来的子表达式,且去括号
		String newExp=exp.substring(0,start)+Count(subExp)+exp.substring(end+1);
		return Count(newExp);
	}
	
	
	public static int GetEnd(String exp){
		int i=0;
		int j=exp.length();
		while(i<exp.length()-1){
		    //出现第一个")"
			if(exp.substring(i,i+1).equals(")")){
				j=i;
			}
			if(i>j){
			    //再出现”)“之后第一次出现”(“
				if(exp.substring(i,i+1).equals("(")){
					break;
				}
			}
			i++;			
		}
		i++;
		//截取字符串,找到此时最后一个”)“
		int end=exp.substring(0,i).lastIndexOf(")");
		
		return end;	
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值