华为机试题:字符串的简单加减乘运算

时间:2021-02-14 18:52:11

最近做了几套华为的机试题,今天有时间,把之前写的几套代码全都贴出来。题目都只记得个大概,将就着看吧,不过代码都是完整的,自认为写的还行。

题目描述

大概意思是:实现一个加减乘的计算。给定的样式是一个字符串,且该字符串为正确的字符串;需要运算的数字均为正数,且每个数字前只有一个运算符。

Java代码

初始思路比较笨,对字符串从左到右一个一个的做判断。分割出数字和运算符号后,先算乘法,再算加减。写完后回头看代码,都有点郁闷了,听上去这么简单的功能,怎么不小心,代码就这么长了呢:

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

public class Main2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String line = scanner.nextLine();
        Stack<String> splitItems = getSplitItems(line);
        int res = getCalculationResult(splitItems);
        scanner.close();
        System.out.println(res);
    }

    public static Stack<String> getSplitItems(String line) {
        Stack<String> stackS = new Stack<String>();
        char[] charS = line.toCharArray();
        int accu = 0;
        boolean isNumReady = false;
        for(char c:charS) {
            if(c>='0' && c<='9') {
                accu = accu*10 + (int)(c-'0');
                isNumReady = true;
            }else {
                if(isNumReady) {
                    stackS.add(String.valueOf(accu));
                }
                if(c=='+'||c=='-'||c=='*'){
                    stackS.add(String.valueOf(c));
                }else {
                    return null;
                }
                isNumReady = false;
                accu = 0;
            }
        }
        if(isNumReady) {
            stackS.add(String.valueOf(accu));
        }
        return stackS;
    }

    public static int getCalculationResult(Stack<String> stack) {
        if(stack==null) {
            return -1;
        }
        Stack<String> helpStack = new Stack<String>();
        while(!stack.isEmpty()) {
            String item = stack.pop();
            if(item.equals("*")) {
                int mult = Integer.parseInt(stack.pop()) * Integer.parseInt(helpStack.pop());
                helpStack.push(String.valueOf(mult));
            }else {
                helpStack.push(item);
            }
        }
        int resNum = Integer.valueOf(helpStack.pop());
        while(!helpStack.isEmpty()) {
            String s = helpStack.pop();
            if(s.equals("+")) {
                resNum += Integer.parseInt(helpStack.pop());
            }else if(s.equals("-")) {
                resNum -= Integer.parseInt(helpStack.pop());
            }
        }
        return resNum;
    }
}

刚突然灵光一闪,我为什么不能用符号“+-*”来直接分隔出数字,用数字分隔出我需要的符号呢,这样分别获得存储数字的栈和存储符号的栈,计算起来岂不是更Easy?以下是我实现的代码:

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

public class Main3 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String line = scanner.nextLine();
        Stack<Integer> splitNums = getSplitNums(line);
        Stack<String> splitStrs = getSplitStrs(line); 
        int res = getCalculationResult(splitNums, splitStrs);
        scanner.close();
        System.out.println(res);
    }

    public static Stack<Integer> getSplitNums(String line) {
        Stack<Integer> stackS = new Stack<>();
        String[] strArray = line.split("\\+|-|\\*");
        for(String s:strArray) {
            stackS.push(Integer.parseInt(s));
        }
        return stackS;
    }

    public static Stack<String> getSplitStrs(String line){
        Stack<String> stackS = new Stack<>();
        String[] strArray = line.split("[0-9]+");
        for(String s:strArray) {
            if(!s.equals("")){
                stackS.push(s);
            }
        }
        return stackS;
    }

    public static int getCalculationResult(Stack<Integer> nums, Stack<String> symbles) {
        Stack<Integer> numsStack = new Stack<>();
        Stack<String> symStack = new Stack<>();
        numsStack.push(nums.pop());
        while(!symbles.isEmpty()) {
            String symble = symbles.pop();
            if(symble.equals("*")) {
                numsStack.push(nums.pop() * numsStack.pop());
            }else {
                symStack.push(symble);
                numsStack.push(nums.pop());
            }
        }

        while(!symStack.isEmpty()) {
            int num = numsStack.pop();
            String symble = symStack.pop();
            if(symble.equals("+")){
                numsStack.push(num + numsStack.pop());
            }else if(symble.equals("-")) {
                numsStack.push(num - numsStack.pop());
            }
        }
        return numsStack.pop();
    }
}