Advertisement
veraklim

Untitled

Sep 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.88 KB | None | 0 0
  1. import java.util.Stack
  2.  
  3. object Postfix {
  4.  
  5.   var current = new Stack[Int]()
  6.  
  7.   def calculate(post: List[String]): Int = {
  8.     if (post.isEmpty) current.pop()
  9.     else {
  10.       post.head match {
  11.         case "+" => {
  12.           val top = current.pop()
  13.           val next = current.pop()
  14.           current.push(top + next)
  15.         }
  16.         case "-" => {
  17.           val top = current.pop()
  18.           val next = current.pop()
  19.           current.push(next - top)
  20.         }
  21.         case "*" => {
  22.           val top = current.pop()
  23.           val next = current.pop()
  24.           current.push(top * next)
  25.         }
  26.         case i => current.push(i.toInt)
  27.       }
  28.       calculate(post.tail)
  29.     }
  30.   }
  31. }
  32.  
  33. object Main extends App {
  34.  
  35.   val post = scala.io.StdIn.readLine()
  36.   //println(post.toString().split(" ").toList)
  37.   println(Postfix.calculate(post.toString().split(" ").toList))
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement