You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

67 lines
1.0 KiB

package main
import (
"fmt"
"os"
"strconv"
"strings"
)
func deleteExtraSpaces(a []string) (res []string) {
for _, v := range a {
if v != "" {
res = append(res, v)
}
}
return
}
func main() {
if len(os.Args) != 2 {
fmt.Println("Error")
return
}
var values []int
op := strings.Split(os.Args[1], " ")
op = deleteExtraSpaces(op)
for _, v := range op {
val, err := strconv.Atoi(v)
if err == nil {
values = append(values, val)
continue
}
n := len(values)
if n < 2 {
fmt.Println("Error")
return
}
switch v {
case "+":
values[n-2] += values[n-1]
values = values[:n-1]
case "-":
values[n-2] -= values[n-1]
values = values[:n-1]
case "*":
values[n-2] *= values[n-1]
values = values[:n-1]
case "/":
values[n-2] /= values[n-1]
values = values[:n-1]
case "%":
values[n-2] %= values[n-1]
values = values[:n-1]
default:
fmt.Println("Error")
return
}
}
if len(values) == 1 {
fmt.Println(values[0])
} else {
fmt.Println("Error")
}
}