用Java求解10以内的整数的四则运算表达式

一、思路
求解10以内的整数的四则运算表达式,主要是利用栈进行计算,需要用两个栈,数字栈和符号栈,用数组存放表达式,用两个栈分别遍历数字和符号,利用符号优先关系,判断当前符号是应该入栈还是出栈进行运算。
二、代码

//栈描述
class SeqStack<T>{
    // 存储栈的元素的数组
    private T[] stack;
    // top表示栈顶的位置
    private int top;

    public SeqStack(){
        this(10);
    }

    public SeqStack(int size){
        this.stack = (T[])new Object[size];
        this.top = 0;
    }

    public void push(T val){
        if(full()){
            // 栈如果满了,要进行内存2倍扩容
            this.stack = Arrays.copyOf(this.stack,
                    this.stack.length*2);
        }
        this.stack[this.top] = val;
        this.top++;
    }

    public void pop(){
        if(empty())
            return;
        this.top--;
        if(this.top < this.stack.length/2){
            this.stack = Arrays.copyOf(this.stack, this.stack.length/2);
        }
    }

    public T to
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PTA(Programming Test and Assessment)平台上有很多简单的四则运算表达式题目,可以练习基本的算术运算和表达式求值。下面是一个例子: 题目描述: 给定一个只包含加、减、乘、除四种基本运算的表达式,求表达式的值。 输入格式: 第一行包含一个整数 T,表示共有 T 组测试数据。 每组数据占一行,包含一个长度不超过 50 的只包含数字与加、减、乘、除四种运算符的表达式。 输出格式: 对于每组数据,输出表达式的值,保留两位小数。 样例输入: 2 1+2*3-4/5 3+5*8-6/2 样例输出: 6.20 43.00 代码示例(C++): ```cpp #include <iostream> #include <stack> #include <string> #include <iomanip> using namespace std; int main() { int T; cin >> T; while (T--) { string s; cin >> s; stack<double> nums; stack<char> ops; for (int i = 0; i < s.size(); i++) { if (isdigit(s[i])) { double num = s[i] - '0'; while (i + 1 < s.size() && isdigit(s[i + 1])) { num = num * 10 + s[i + 1] - '0'; i++; } nums.push(num); } else if (s[i] == '(') { ops.push('('); } else if (s[i] == ')') { while (ops.top() != '(') { double b = nums.top(); nums.pop(); double a = nums.top(); nums.pop(); char op = ops.top(); ops.pop(); if (op == '+') nums.push(a + b); else if (op == '-') nums.push(a - b); else if (op == '*') nums.push(a * b); else if (op == '/') nums.push(a / b); } ops.pop(); } else if (s[i] == '+' || s[i] == '-') { while (!ops.empty() && (ops.top() == '+' || ops.top() == '-' || ops.top() == '*' || ops.top() == '/')) { double b = nums.top(); nums.pop(); double a = nums.top(); nums.pop(); char op = ops.top(); ops.pop(); if (op == '+') nums.push(a + b); else if (op == '-') nums.push(a - b); else if (op == '*') nums.push(a * b); else if (op == '/') nums.push(a / b); } ops.push(s[i]); } else if (s[i] == '*' || s[i] == '/') { while (!ops.empty() && (ops.top() == '*' || ops.top() == '/')) { double b = nums.top(); nums.pop(); double a = nums.top(); nums.pop(); char op = ops.top(); ops.pop(); if (op == '+') nums.push(a + b); else if (op == '-') nums.push(a - b); else if (op == '*') nums.push(a * b); else if (op == '/') nums.push(a / b); } ops.push(s[i]); } } while (!ops.empty()) { double b = nums.top(); nums.pop(); double a = nums.top(); nums.pop(); char op = ops.top(); ops.pop(); if (op == '+') nums.push(a + b); else if (op == '-') nums.push(a - b); else if (op == '*') nums.push(a * b); else if (op == '/') nums.push(a / b); } cout << fixed << setprecision(2) << nums.top() << endl; } return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值