Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Stack
- object Postfix {
- var current = new Stack[Int]()
- def calculate(post: List[String]): Int = {
- if (post.isEmpty) current.pop()
- else {
- post.head match {
- case "+" => {
- val top = current.pop()
- val next = current.pop()
- current.push(top + next)
- }
- case "-" => {
- val top = current.pop()
- val next = current.pop()
- current.push(next - top)
- }
- case "*" => {
- val top = current.pop()
- val next = current.pop()
- current.push(top * next)
- }
- case i => current.push(i.toInt)
- }
- calculate(post.tail)
- }
- }
- }
- object Main extends App {
- val post = scala.io.StdIn.readLine()
- //println(post.toString().split(" ").toList)
- println(Postfix.calculate(post.toString().split(" ").toList))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement