/* Copyright (C) 2015 by Alexandru Cojocaru */ /* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ // try: // echo '19 2.14 + 4.5 2 4.3 / - *' | go run 1.go package main import ( "bufio" "fmt" "log" "os" "strconv" ) var stack []float64 func push(n float64) { stack = append(stack, n) } func pop() float64 { n := stack[len(stack)-1] stack = stack[:len(stack)-1] return n } func main() { scanner := bufio.NewScanner(os.Stdin) scanner.Split(bufio.ScanWords) for scanner.Scan() { str := scanner.Text() switch str { case "+": push(pop() + pop()) case "-": push(-pop() + pop()) case "*": push(pop() * pop()) case "/": push(1 / pop() * pop()) default: n, err := strconv.ParseFloat(str, 64) if err != nil { log.Fatal(err) } push(n) } } if err := scanner.Err(); err != nil { log.Fatal(err) } fmt.Printf("%v\n", stack) }