java利用栈编写计算器(中缀表达式直接求值)

处理多位数字

	//考虑多位数字,如果是数字
				//numArrayStackk.push(ch-48);
				KeepNum+=ch;
				
				//如果ch已经是表达式最后一位
				if(index == expression.length()-1){
					numArrayStackk.push(Integer.parseInt(KeepNum));
				}else{
				if(operArrayStackk.isOper(expression.substring(index+1,index+2).charAt(0))){
					numArrayStackk.push(Integer.parseInt(KeepNum));
					KeepNum="";
				}
				}

判断优先级

//返回符号优先级吗,使用数字表示,数字越大,优先级越高
	public int priority(int oper) {
		if(oper == '*'||oper == '/'){
			return 1;
		}
		else if(oper =='+'||oper == '-'){
			return 0;
		}else{
			return -1;
		}
	}
package stack;

public class Calculator {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String expression="70+10*3-30";
		ArrayStack numArrayStackk=new ArrayStack(10);
		ArrayStack operArrayStackk=new ArrayStack(10);
		int index=0;
		int num1=0;
		int num2=0;
		int oper=0;
		int res=0;
		char ch= ' ';
		String KeepNum = "" ;
		while(true){
			ch=expression.substring(index,index+1).charAt(0);
			System.out.println("ch:"+ch);
			if(operArrayStackk.isOper(ch)){//如果是运算符
				//判断符号站是否为空
				if(!operArrayStackk.isEmpty()){
					//如果不为空,则比较 扫描到得符号和符号栈里的符号优先级大小
					if(operArrayStackk.priority(ch)<=operArrayStackk.priority(operArrayStackk.peek())){
						num1=numArrayStackk.pop();
						System.out.println("值1:"+num1);
						num2=numArrayStackk.pop();
						System.out.println("值2:"+num1);
						oper=operArrayStackk.pop();
						res=numArrayStackk.cal(num1, num2, oper);
						numArrayStackk.push(res);
						operArrayStackk.push(ch);
					}else{
						operArrayStackk.push(ch);
					}
				}else{
					operArrayStackk.push(ch);
					System.out.println("符号ch:"+ch);
				}
			}
			//如果是数字
			else{
				//考虑多位数字,如果是数字
				//numArrayStackk.push(ch-48);
				KeepNum+=ch;
				
				//如果ch已经是表达式最后一位
				if(index == expression.length()-1){
					numArrayStackk.push(Integer.parseInt(KeepNum));
				}else{
				if(operArrayStackk.isOper(expression.substring(index+1,index+2).charAt(0))){
					numArrayStackk.push(Integer.parseInt(KeepNum));
					KeepNum="";
				}
				}
			}
			index++;
			if(index>=expression.length()){
				break;
			}
			
		}
		
		while(true){
			if(operArrayStackk.isEmpty()){
				break;
			}
			num1=numArrayStackk.pop();
			num2=numArrayStackk.pop();
			oper=operArrayStackk.pop();
			res=numArrayStackk.cal(num1, num2, oper);
			numArrayStackk.push(res);
		}
		int res2=numArrayStackk.pop();
		System.out.printf("表达式%s=%d",expression,res2);
	}

}

class ArrayStack{
	private int maxSize;
	private int[] stack;
	private int top=-1;//栈顶
	//构造器
	public ArrayStack(int maxSize) {
		this.maxSize = maxSize;
		stack=new int[this.maxSize];
	}
	
	//栈满
	public boolean isFull() {
		return top == maxSize-1;
	}
	//返回栈顶元素
	public char peek() {
		return (char)stack[top];
	}
	//栈空
	public boolean isEmpty() {
		return top == -1;
	}
	//入栈
	public void push(int value) {
		if(isFull()){
			System.out.println("栈满");
			return ;
		}
		top++;
		stack[top] =value;
	}
	public int pop() {
		if(isEmpty()){
			System.out.println("栈空");		
		}
		int value=stack[top];
		top--;
		return value;
	}
	
	public void list(){
		if(isEmpty()){
			System.out.println("栈空,没有数据");
			return;
		}for(int i=top;i>=0;i--){
			System.out.printf("stack[%d]=%d\n",i,stack[i]);
		}
	}
	
	//返回符号优先级吗,使用数字表示,数字越大,优先级越高
	public int priority(int oper) {
		if(oper == '*'||oper == '/'){
			return 1;
		}
		else if(oper =='+'||oper == '-'){
			return 0;
		}else{
			return -1;
		}
	}
	
	//判断符号是不是运算符号
	public boolean isOper(char val){
		return val=='+'||val=='-' || val=='*' ||val=='/';
	}
	
	//计算方法
	public int  cal(int num1,int num2,int oper) {
		int res= 0;//存放计算结果
		switch(oper){
		case'+': res=num1 +num2;break;
		case'-': res=num2 -num1;break;
		case'*': res=num2 * num1;break;
		case'/': res=num2 / num1;break;
		default: break;                                                                      
		}
		return res;
	}
}
发布了58 篇原创文章 · 获赞 20 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_40709110/article/details/101159592