Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import scala.io.StdIn._
- import scala.collection.mutable._
- object Calculator {
- object Type extends scala.Enumeration {
- type Type = Value
- val NUMBER, OPERATION, OPEN_BR, CLOSE_BR, NONE = Value
- }
- import Type._
- val tokens = Array(NONE -> " +",
- NUMBER -> "\\d+.?\\d*",
- OPEN_BR -> "[(]",
- CLOSE_BR -> "[)]",
- OPERATION -> "[+-*\\/]")
- def toPolish(input : String) : Array[(Calculator.Type.Value ,Double)] = {
- val result = new ArrayBuffer[(Calculator.Type.Value,Double)]
- var pos = 0
- while(pos < input.length) {
- var isFound = false
- for(tok <- tokens){
- val mat = tok._2.r findFirstMatchIn input.substring(pos)
- if(mat!=None){
- isFound = true
- pos+=mat.get.end
- tok._1 match {
- case NUMBER => result+=(tok._1 -> tok._2.toDouble)
- case OPEN_BR =>
- case CLOSE_BR=>
- case OPERATION=>result+=(tok._1 -> 0)
- }
- }
- }
- if(!isFound)pos=input.length
- }
- result.toArray
- }
- }
- object MyApp extends App {
- val str = readLine
- val array = Calculator.toPolish(str)
- println(array.mkString(","))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement