四则运算计速器算法

//用双栈模拟 有点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());
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值