1. 程式人生 > >華為機試題:字串的簡單加減乘運算

華為機試題:字串的簡單加減乘運算

最近做了幾套華為的機試題,今天有時間,把之前寫的幾套程式碼全都貼出來。題目都只記得個大概,將就著看吧,不過程式碼都是完整的,自認為寫的還行。

題目描述

大概意思是:實現一個加減乘的計算。給定的樣式是一個字串,且該字串為正確的字串;需要運算的數字均為正數,且每個數字前只有一個運算子。

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();
    }
}