栈的应用计算器(中缀表达式)练习

创建与运算符有关的类

public class Operation {
    private static int ADD = 1;
    private static int SUB = 1;
    private static int MUL = 2;
    private static int DIV = 2;
    //写一个方法,返回对应的优先级数字
    public static int getValue(char operation){
        int result = 0;
        switch (operation){
            case '+':
                result = ADD;
                break;
            case '-':
                result = SUB;
                break;
            case '*':
                result = MUL;
                break;
            case '/':
                result = DIV;
                break;
            default:
                break;
        }
        return result;
    }
    //判断是不是运算符
    public static boolean isOperation(char ch){
        return ch == '+' || ch == '-' || ch == '/' || ch == '*';
    }
    //计算
    public static int calculate(int num1, int num2, char operation){
        int result = 0;
        switch (operation){
            case '+':
                result = num1 + num2;
                break;
            case '-':
                result = num1 - num2;
                break;
            case '*':
                result = num1 * num2;
                break;
            case '/':
                result = num1 / num2;
            default:
                break;
        }
        return result;
    }
}

计算器

计算器需要使用两个栈,一个数字栈存放数字,一个符号栈存放运算符
在这里插入图片描述
从左往右扫描,第一个为3,是数字就压入数字栈,接下来扫描第二个,为运算符+,但是此时符号栈为空,所以直接将运算符压入栈中
在这里插入图片描述
继续扫描遇到数字3,直接压入数字栈,继续扫描,遇到运算符*,这个运算符的优先级高于符号栈栈顶符号+运算符,所以也直接压入符号栈,继续扫描
在这里插入图片描述
此时遇到数字4,继续扫描,遇到运算符-,优先级小于此时符号栈栈顶元素*,应该将数字栈的栈顶和栈顶前一个数字出栈,符号栈的栈顶符号进行运算,将计算的结果加入数字栈中,同时将当前扫描的运算符压入符号栈中
在这里插入图片描述

继续扫描,扫描到最后一个数字7,压入数字栈中,扫描结束,最后进行计算
在这里插入图片描述
每次计算都是将数字栈的栈顶两个数字出栈,符号栈的栈顶运算符出栈进行计算,计算结果都继续压入数字栈,直到符号栈中没有了运算符,数字栈中只剩一个数字就是最后的计算结果
在这里插入图片描述

public static void calculator(String expression){
        Stack<Integer> numStack = new Stack<>();   //用来存放数字的栈
        Stack<Character> operStack = new Stack<>();  //用来存放运算符的栈
        int index = 0;   //索引,指针
        int num1 = 0;
        int num2 = 0;
        int result = 0;
        char operator = 0;
        char ch = ' ';  //将扫描的字符存在这里
        String keepNum = "";   //用来拼接数字
        while(true){
            ch = expression.charAt(index);
            if(Operation.isOperation(ch)){   //如果是运算符
                if(operStack.isEmpty()){     //如果栈此时为空,直接入栈
                    operStack.push(ch);
                }else{
                    if(Operation.getValue(ch) <= Operation.getValue(operStack.peek())){   //如果当前运算符的优先级小于或者等于符号栈栈顶运算符
                        num2 = numStack.pop();  //弹出栈顶两个元素,进行运算
                        num1 = numStack.pop();
                        operator = operStack.pop();
                        result = Operation.calculate(num1, num2, operator);
                        //把运算结果入栈
                        numStack.push(result);
                        //当前运算符入栈
                        operStack.push(ch);
                    }else{ //如果当前运算符的优先级大于符号栈栈顶符号优先级
                        //直接入栈
                        operStack.push(ch);
                    }
                }
            }else{   //如果是数,继续入栈
                keepNum += ch;
                //如果ch是最后一位,直接入栈
                if(index == expression.length() - 1){
                    numStack.push(Integer.valueOf(keepNum));
                }else{
                    //判断下一位是不是数字,如果是,继续扫描,不是则直接入栈
                    if(Operation.isOperation(expression.charAt(index + 1))){
                        numStack.push(Integer.valueOf(keepNum));
                        keepNum = "";   //注意!这里必须要把拼接数字的辅助串置空
                    }
                }
            }
            index++;
            //结束条件,如果index是字符串的长度时,表示已经扫描完毕了
            if(index == expression.length()){
                break;
            }
        }
//             * 4. 当表达式扫描完毕,就顺序的从 数栈和符号栈中pop出相应的数和符号,并运行.
//             * 5. 最后在数栈只有一个数字,就是表达式的结果

        while(true){
            if(operStack.isEmpty()){
                break;
            }else{
                num2 = numStack.pop();
                num1 = numStack.pop();
                operator = operStack.pop();
                result = Operation.calculate(num1, num2, operator);
                numStack.push(result);
            }
        }
        System.out.println(expression + " = " + numStack.pop());
    }
}
是实现计算器的核心数据结构,可以用来存储运算符和操作数。下面是使用实现中缀表达式计算器的C语言代码: ```c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <ctype.h> #define MAX_SIZE 50 typedef struct { int top; int data[MAX_SIZE]; } Stack; void push(Stack *s, int value) { if (s->top == MAX_SIZE - 1) { printf("Stack Overflow\n"); return; } s->data[++(s->top)] = value; } int pop(Stack *s) { if (s->top == -1) { printf("Stack Underflow\n"); exit(1); } return s->data[(s->top)--]; } bool is_operator(char ch) { return ch == '+' || ch == '-' || ch == '*' || ch == '/'; } int get_priority(char op) { switch (op) { case '(': return 0; case '+': case '-': return 1; case '*': case '/': return 2; default: return -1; } } int calculate(int op1, int op2, char operator) { switch (operator) { case '+': return op1 + op2; case '-': return op1 - op2; case '*': return op1 * op2; case '/': return op1 / op2; default: return -1; } } int evaluate_expression(char *expression) { Stack operand_stack; Stack operator_stack; operand_stack.top = -1; operator_stack.top = -1; int i = 0; while (expression[i] != '\0') { if (isdigit(expression[i])) { int operand = 0; while (isdigit(expression[i])) { operand = operand * 10 + (expression[i] - '0'); i++; } push(&operand_stack, operand); } else if (is_operator(expression[i])) { while (operator_stack.top >= 0 && get_priority(expression[i]) <= get_priority(operator_stack.data[operator_stack.top])) { int op2 = pop(&operand_stack); int op1 = pop(&operand_stack); char op = pop(&operator_stack); int result = calculate(op1, op2, op); push(&operand_stack, result); } push(&operator_stack, expression[i]); i++; } else if (expression[i] == '(') { push(&operator_stack, expression[i]); i++; } else if (expression[i] == ')') { while (operator_stack.data[operator_stack.top] != '(') { int op2 = pop(&operand_stack); int op1 = pop(&operand_stack); char op = pop(&operator_stack); int result = calculate(op1, op2, op); push(&operand_stack, result); } pop(&operator_stack); i++; } else { i++; } } while (operator_stack.top >= 0) { int op2 = pop(&operand_stack); int op1 = pop(&operand_stack); char op = pop(&operator_stack); int result = calculate(op1, op2, op); push(&operand_stack, result); } return pop(&operand_stack); } int main() { char str[MAX_SIZE]; printf("Enter expression: "); scanf("%s", str); int result = evaluate_expression(str); printf("Result: %d\n", result); return 0; } ``` 这个程序支持加、减、乘、除和括号,可以计算包括负数在内的表达式。你可以在控制台中输入一个表达式,程序会输出计算结果。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ht巷子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值