計算器(棧實現)

通過棧的數據結構實現一個計算器

詳解見註釋

package stack;

public class Calculator
{

	public static void main(String[] args)
	{
		//表達式
		String expression = "3+20*6-20";
		//創建兩個棧,一個符號棧,一個數棧
		ArrayStack2 numStack = new ArrayStack2(10);
		ArrayStack2 operStack = new ArrayStack2(10);
		//定義需要的相關變量
		int index = 0;//用於掃描
		int num1 = 0;
		int num2 = 0;
		int oper = 0;
		int res = 0;
		char ch = ' '; //每次掃描到的char保存到ch
		String keepNum = ""; //多位數
		//開始while循環的
		while(true)
		{
			//依次得到expression的每一個字符
			ch = expression.substring(index, index+1).charAt(0);
			//判斷ch是什麼,然後再做處理
			if(operStack.isOper(ch)) //如果是運算符
			{
				if(!operStack.isEmpty())//判斷當前棧是否爲空
				{
					if(operStack.priority(ch) <= operStack.priority(operStack.peek()))
					{
						num1 = numStack.pop();
						num2 = numStack.pop();
						oper= operStack.pop();
						res = numStack.cal(num1, num2, oper);
						//運算結果入棧
						numStack.push(res);
						operStack.push(ch);
					}
					else
					{
						//如果當前的操作符的優先級大於棧中的操作符,就直接入符號棧
						operStack.push(ch);
					}
				}
				else
				{
					//如果爲空,直接入符號棧
					operStack.push(ch);
				}
			}
			else
			{
//				//如果是數,直接入數棧
//				numStack.push(ch - 48); //'1' + '3' = >1    (阿斯克碼)  
				
				//處理多位數
				keepNum = keepNum + ch;
				
				//如果ch已經是expression的最後一位,就直接入棧
				if(index == expression.length() - 1)
				{
					numStack.push(Integer.parseInt(keepNum));
				}
				else
				{
					
					//判斷下一個是不是數字,如果是數字,就繼續掃描,運算符啊,則入棧
					if(operStack.isOper(expression.substring(index+1, index+2).charAt(0)))
					{
						numStack.push(Integer.parseInt(keepNum));
						keepNum = "";  //置空
					}
				}

			}
			
			//讓index+1,並判斷是否掃描到expression最後
			index++;
			if(index >= expression.length())
			{
				break;
			}
		}
		
		//當表達式掃描完畢,就順序的從數棧 + 符號棧中pop相應的數和符號,並運行
		while(true)
		{
			if(operStack.isEmpty())
			{
				break;
			}
			//如果符號棧爲空,則計算到最後結果
			num1 = numStack.pop();
			num2 = numStack.pop();
			oper = operStack.pop();
			res = numStack.cal(num1, num2, oper);
			numStack.push(res); //入棧
		}
		//將數棧的最後數,pop;
		
		System.out.printf("表達式%s = %d", expression, numStack.pop());
	}
}

//定義一個ArrayStack表示棧
class ArrayStack2
{
	private int maxSize;  //最大容量
	private int[] stack;  //數組模擬棧,數據就放在該數組中
	private int top = -1; //top表示棧頂,初始化爲-1
	
	//構造器
	public ArrayStack2(int maxSize)
	{
		this.maxSize = maxSize;
		stack = new int[this.maxSize];
	}
	
	//增加一個方法,可以返回當前棧頂的值,但不是真正的pop
	public int peek()
	{
		return stack[top];
	}
	
	//判斷是否棧滿
	public boolean isFull()
	{
		return top == maxSize-1;
	}
	
	//判斷是否棧空
	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())
			throw new RuntimeException("棧已空,無數據~");
		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 = num2 + num1;
				break;
			case '-':
				res = num2 - num1;
				break;
			case '*':
				res = num2 * num1;
				break;
			case '/':
				res = num2 / num1;
				break;
			default:
					break;
		}
		return res;
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章