lotus

贵有恒何必三更眠五更起 最无益只怕一日曝十日寒

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1. 题目

读题

HJ50 四则运算 

 

考查点

 

2. 解法

思路

 

代码逻辑

 

具体实现

 

import java.util.Scanner;
import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        // 输入
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        // 中括号、大括号全部转为小括号,方便处理
        str = str.replace('[', '(');
        str = str.replace('{', '(');
        str = str.replace(']', ')');
        str = str.replace('}', ')');
        // 为了统一计算逻辑,在最外面的表达式也放括号
        if (str.charAt(0) != '(') {
            str = '(' + str + ')';
        }
        // 输出
        System.out.println(solve(str));
        // 关闭
        in.close();
    }

    private static int solve(String str) {
        char[] charArray = str.toCharArray();
        int length = charArray.length;
        // 用于存放数字。
        Stack<Integer> stack = new Stack<>();
        // 纪录数字
        int number = 0;
        // 纪录上个操作符
        char opr = '+';
        for (int i = 0; i < length; i++) {
            char ch = charArray[i];
            // 一直入栈
            // 遇到右括号就出栈,直到左括号出现为止
            // 括号内包裹的表达式进行计算
            // 如果当前字符是小括号
            if (ch == '(') {
                // 移到小括号后一位字符
                int j = i + 1;
                // 统计括号的数量
                int count = 1;
                while (count > 0) {
                    // 遇到右括号,括号数-1
                    if (charArray[j] == ')') {
                        count--;
                    }
                    // 遇到左括号,括号数+1
                    if (charArray[j] == '(') {
                        count++;
                    }
                    j++;
                }
                // 递归,解小括号中的表达式
                number = solve(str.substring(i + 1, j - 1));
                i = j - 1;
            } else if (Character.isDigit(ch)) {
                // 多为数字的处理,ch-'0'是转为整数
                number = number * 10 + ch - '0';
            }
            if (!Character.isDigit(ch) || i == length - 1) {
                // 遇到符号,将数字处理后放进栈
                // 如果是'+',直接入栈
                if (opr == '+') {
                    stack.push(number);
                }
                // 如果是'-',数字取相反数在入栈
                else if (opr == '-') {
                    stack.push(-1 * number);
                }
                // 如果是'*',弹出一个数字乘后放入栈
                else if (opr == '*') {
                    stack.push(stack.pop() * number);
                }
                // 如果是'/',弹出一个数字/后放入栈
                else if (opr == '/') {
                    stack.push(stack.pop() / number);
                }
                // 更新符号
                opr = ch;
                // 刷新数字
                number = 0;
            }
        }
        // 栈中数字求和得到结果
        int sum = 0;
        while (!stack.isEmpty()) {
            sum += stack.pop();
        }
        return sum;
    }
}

自行实现

public class HJ050 {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
str = str.replace('[', '(');
str = str.replace('{', '(');
str = str.replace(']', ')');
str = str.replace('}', ')');
if (str.charAt(0) != '(') {
str = "(" + str + ")";
}
System.out.println(solve(str));
sc.close();
}

public static int solve(String str) {

char[] chars = str.toCharArray();
int n = chars.length;
Stack<Integer> stack = new Stack<>();
int num = 0;
char opr = '+';

for (int i = 0; i < n; i++) {
char c = chars[i];
if (c == '(') {
int j = i + 1;
int count = 1;
while (count > 0) {
if (chars[j] == ')') {
count--;
}
if (chars[j] == '(') {
count++;
}
j++;
}
num = solve(str.substring(i + 1, j - 1));
i = j - 1;
} else if (Character.isDigit(c)) {
num = num * 10 + (c - '0');
} else if (!Character.isDigit(c) || i == n - 1) {
if (opr == '-') {
stack.push(-1 * num);
} else if (opr == '+') {
stack.push(num);
} else if (opr == '*') {
stack.push(stack.pop() * num);
} else if (opr == '/') {
stack.push(stack.pop() / num);
}
opr = c;
num = 0;
}
}

int sum = 0;
while (!stack.isEmpty()) {
sum += stack.pop();
}
return sum;
}
}

3. 总结

posted on 2023-07-21 17:58  白露~  阅读(32)  评论(0编辑  收藏  举报