04-使用栈实现简单计算器

package 使用栈实现计算器;

public class Calculator {
public static void main(String[] args) {
//需要计算的表达式
String expression=“30+2*6-200”;
//数字栈
ArrayStack1 numstack=new ArrayStack1(10);
// 符号栈
ArrayStack1 operstack= new ArrayStack1(10);
//定义需要的相关变量
int index=0;//用于扫描
int num1=0;
int num2=0;
int oper=0;
int res=0;
char ch=’ ';//将每次扫描得到的char保存到ch
String keepnum="";//用于拼接多位数
//开始扫描,依次得到expression的每一个字符,判断ch类型,并处理
while(true){
// charAt(int index) :返回指定索引处的字符。
//str.substring(int beginIndex,int endIndex);截取str中从beginIndex开始至endIndex结束时的字符串;
//补充: beginIndex =< 截取str的值 < endIndex(左闭右开)
ch= expression.substring(index,index+1).charAt(0);//得到expression的一个字符
//判断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);//,如果是单个数,直接入栈,char为char,ASCII对应转换减去48变数
//当处理多位数时,不需要发现是一个数就立即入栈,需要向expression的表达式后index后在看一位
//如果是数就进行扫描,是符号才入栈,定义变量字符串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))) {//看后一位,不是index++
//如果后一位是运算符,则入栈keepnum=”1“或者”123“
numstack.push(Integer.parseInt(keepnum));
keepnum = “”;//清空
}
}
}
//判断index是否扫描到expression到最后
index++;
if(index>=expression.length()){
break;
}

    }
    //当表达式扫描完毕,就顺序从数栈和符号栈中取出相应符号和数,并计算
    while (true){
        //如果符号栈为空,则计算到最后结果,数栈中只有一个数字,即为结果
        if(operstack.isEmpty()){
            break;
        }
        num1=numstack.pop();
        num2=numstack.pop();
        oper=operstack.pop();
        res=numstack.cal(num1,num2,oper);
        numstack.push(res);//入栈

    }
    //输出最后结果
    System.out.println("表达式"+expression+"="+numstack.pop());
}

}
class ArrayStack1{
int top=-1;
int maxsize;
int[] stack;
ArrayStack1(int maxsize){
this.maxsize=maxsize;
stack=new int[maxsize];
}
//是否满
public boolean isFull(){
return top==maxsize-1;
}
//是否为空
public boolean isEmpty(){

        return top==-1;
    }
//入
public void push(int value){
    isFull();
    top++;
    stack[top]=value;
}
//出
public int pop(){
    isEmpty();
    int val=stack[top];
    top--;
    return val;
}
//遍历
public void show(){
    isFull();
    while(top!=-1){
        System.out.println(stack[top]);
        top--;
    }
}
//返回栈顶元素
public int peek(){
    return stack[top];
}
//判断是不是运算符
public boolean isOper(char val){
    return val=='*'||val=='+'||val=='-'||val=='/';
}
//返回运算符优先级,程序员决定,优先级用数字表示;
//数字越大,优先级越高
public int priority(int oper){
    if(oper=='*'||oper=='/'){
        return 1;
    }else if(oper=='+'||oper=='-'){
        return 0;
    }else {//即目前运算符仅有加减乘除
        return -1;
    }
}
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=num1*num2;
            break;
        case '/':
            res=num2/num1;
            break;
       default:
           break;
    }
    return  res;
}

}

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值