四則運算計速器算法

//用雙棧模擬 有點low
public class MyStack{
private int top=-1; //模擬棧底,指向-1
private int maxSize=15; //模擬棧的容量爲5
private String[] stack=new String [100];//用來數組來模擬,想數組 內填入棧的內容
// private char a=1.2;
//判斷是否是運算符
public boolean isOper(char ch){
if(ch==’+’||ch==’-‘||ch==’*’||ch==’/’){
return true;
}else{
return false;
}
}
//判斷棧是否爲空
public boolean isEmpty(){
if(this.top==-1){
return true;
}else{
return false;
}
}
//判斷符號的優先級
public int PRI(char ch){
if(ch==’*’||ch==’/’){
return 1;
}else if(ch==’+’||ch==’-‘){
return 0;
}
return -1;
}
//獲得棧頂的值
public String getTop(){
return this.stack[this.top];
}
//計算數值
public int getResult(int num1,int num2,char ope){
int res=0;
switch(ope){
case ‘+’:
res=num1+num2;
break;
case ‘-‘:
res=num2-num1;
break;
case ‘*’:
res=num1*num2;
break;
case ‘/’:
res=num2/num1;
break;
//erqiqu huibohuayuan 155171111 86
}
return res;
}
//入棧操作
public void push(String val){
///先判斷棧是否已滿
if(this.top==this.maxSize-1){
// echo ‘棧滿,不能添加‘;
return; //棧滿 返回
}
//先top上移,然後填充棧內容
this.top++;
this.stack[this.top]=val;
}
//出棧
public String pop(){
if(this.top==-1){
// echo ‘棧111空‘;
return null; //空棧,無數據,返回
}
//取出棧頂的數據,同時把該數據返回,別忘了把top指針下移
String topValue=this.stack[this.top];
this.top–;
return topValue;
}
//顯示棧的所有信息
public void showStack(){
if(this.top==-1){
// echo ‘棧空!‘;
return;//空棧,無數據,返回
}
//結合堆棧的數據結構,是後進先出類型的,因此從棧頂開始,依次往下讀出棧的內容
//for(i=this.top;i>-1;i–){
//echo ‘Stack[‘.i.’]=’.this.stack[i].’‘;
// }
}
}

 //測試
public class Test {
public static void main(String[] args) {

    String exp = "-3*9-2-1";
    MyStack numStack = new MyStack();
    MyStack opeStack = new MyStack();

    int index = 0;
    String keepNum = "";
    while (true) {
        String ch = exp.substring(index, index + 1);
        // ch=substr(exp,index,1);
        // 判斷是否是字符
        if (opeStack.isOper(ch.charAt(0))) {
            // 是運算符
            // 是運算符
            /**
             * 3.如果發現是運算符 3.1 如果符號棧爲空,就直接入符號棧
             * 
             * 3.2. 如何符號棧,不爲空,就判斷
             * 如果當前運算符的優先級小於等於符號棧頂的這個運算符的優先級,就計算,並把計算結果入數棧.然後把當前符號入棧 3.3
             * 如何符號棧,不爲空,就判斷 如果當前運算符的優先級大於符號棧頂的這個運算符的優先級,就入棧.
             * 
             */
            // 如何符號棧,不爲空,
            // 如何符號棧,不爲空,
            if (opeStack.isEmpty() == true) {

                if ("-".equals(ch)) {// 解決第一字符時操作符‘-’的問題
                    numStack.push("0");
                    opeStack.push(ch);
                } else {

                    opeStack.push(ch);
                }

            } else {
                // 解決當符號位的優先級最終一致時 比如:3-4*6-2。第二個‘-’能先在3*5 計算完畢之後入棧
                while (!opeStack.isEmpty()
                        && opeStack.PRI(ch.charAt(0)) <= opeStack.PRI(opeStack.getTop().charAt(0))) {

                    int num1 = Integer.parseInt(numStack.pop());
                    int num2 = Integer.parseInt(numStack.pop());
                    String ope = opeStack.pop();
                    int res = opeStack.getResult(num1, num2, ope.charAt(0));
                    numStack.push(res + "");

                }
                opeStack.push(ch);

            } // if結束
        } else {
            keepNum += ch;// 解決數字是多位數的問題
            // 先判斷是否到最後,如果到最後就入棧
            if (index == exp.length() - 1) {
                numStack.push(keepNum);
            } else {

                if (opeStack.isOper(exp.substring(index + 1, index + 2).charAt(0))) {
                    numStack.push(keepNum);
                    keepNum = "";
                }
            }

        }

        ++index;
        if (index == exp.length()) {
            break;

        }

    }

    while (!opeStack.isEmpty()) {

        int num1 = Integer.parseInt(numStack.pop());
        int num2 = Integer.parseInt(numStack.pop());
        String ope = opeStack.pop();
        int res = opeStack.getResult(num1, num2, ope.charAt(0));
        numStack.push(res + "");
    }
    System.out.println(numStack.getTop());
}

}

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