golang 反向波兰表示计算器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了golang 反向波兰表示计算器相关的知识,希望对你有一定的参考价值。
package main
import (
"fmt"
"strconv"
"unicode"
"unicode/utf8"
)
func skipSpaces(s []byte) []byte {
c, w := utf8.DecodeRune(s)
for w > 0 && unicode.IsSpace(c) {
s = s[w:]
c, w = utf8.DecodeRune(s)
}
return s
}
func readDigits(s []byte) (numStr, remain []byte) {
numStr = s
totalW := 0
c, w := utf8.DecodeRune(s)
for w > 0 && unicode.IsDigit(c) {
s = s[w:]
totalW += w
c, w = utf8.DecodeRune(s)
}
return numStr[:totalW], s
}
func pop(stack []int) (int, []int) {
return stack[len(stack)-1], stack[:len(stack)-1]
}
func eval(s []byte) {
stack := make([]int, 0)
var a, b int
var token []byte
fmt.Println("eval:", string(s))
s = skipSpaces(s)
for len(s) > 0 {
c, w := utf8.DecodeRune(s)
switch {
case unicode.IsDigit(c):
token, s = readDigits(s)
num, err := strconv.Atoi(string(token))
if err != nil {
fmt.Println(err)
} else {
stack = append(stack, num)
}
case c == '+':
b, stack = pop(stack)
a, stack = pop(stack)
stack = append(stack, a+b)
s = s[w:]
case c == '-':
b, stack = pop(stack)
a, stack = pop(stack)
stack = append(stack, a-b)
s = s[w:]
case c == '*':
b, stack = pop(stack)
a, stack = pop(stack)
stack = append(stack, a*b)
s = s[w:]
case c == '/':
b, stack = pop(stack)
a, stack = pop(stack)
stack = append(stack, a/b)
s = s[w:]
case c == '%':
b, stack = pop(stack)
a, stack = pop(stack)
stack = append(stack, a%b)
s = s[w:]
default:
fmt.Println("unknown character:", c)
s = s[w:]
}
s = skipSpaces(s)
}
fmt.Println("stack:", stack)
}
func main() {
// 2 * 21 - 30 = 12
eval([]byte("2 21 * 30-"))
// 1 + ... + 10 = 55
eval([]byte("1 2 3 4 5 6 7 8 9 10+++++++++"))
}
java 150.评估反向波兰表示法(#)
public class Solution {
public int evalRPN(String[] tokens) {
// 栈,用于遍历初始字符串数组
Deque<Integer> stack = new ArrayDeque<Integer>();
int a, b;// 临时存放栈中弹出两个元素
/**
* 遍历初始字符串数组,
* 若当前字符为 运算符 ,则从栈中弹出两个元素,并用该运算符对它们进行运算,然后再将运算结果压入栈
* 若读到的是数字,则直接将其压入栈,不作其他操作
*/
for (int i = 0; i < tokens.length; i++) {
String temp = tokens[i];
switch (temp) {
case "+":
a = stack.pop();
b = stack.pop();
stack.push((b + a));
break;
case "-":
a = stack.pop();
b = stack.pop();
stack.push(b - a);
break;
case "*":
a = stack.pop();
b = stack.pop();
stack.push(b * a);
break;
case "/":
a = stack.pop();
b = stack.pop();
if (a == 0) {
return -1;
}
stack.push(b / a);
break;
default:
stack.push(Integer.parseInt(temp));
}
}
int result = stack.peek();
return result;
}
}
public class Solution {
int index;
public int evalRPN(String[] tokens) {
index = tokens.length - 1;
return recurse(tokens);
}
private int recurse(String[] tokens) {
String word = tokens[index--];
int a, b;
switch (word) {
case "+":
b = recurse(tokens);
a = recurse(tokens);
return a + b;
case "-":
b = recurse(tokens);
a = recurse(tokens);
return a - b;
case "*":
b = recurse(tokens);
a = recurse(tokens);
return a * b;
case "/":
b = recurse(tokens);
a = recurse(tokens);
return a / b;
default:
return Integer.parseInt(word);
}
}
}
public class Solution {
private int calculate (int a, int b, String op) {
switch(op) {
case "+" : return a + b;
case "-" : return a - b;
case "*" : return a * b;
case "/" : return a / b;
default : return 0;
}
}
public int evalRPN(String[] tokens) {
if (tokens == null || tokens.length < 1) return 0;
String operations = "+-*/";
Stack<Integer> stack = new Stack<>();
for (String s : tokens) {
if (operations.contains(s)) {
int second = stack.pop();
int first = stack.pop();
stack.push(calculate(first, second, s));
} else {
stack.push(Integer.valueOf(s));
}
}
return stack.pop();
}
}
以上是关于golang 反向波兰表示计算器的主要内容,如果未能解决你的问题,请参考以下文章