Advertisement
Guest User

calc

a guest
Apr 4th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.29 KB | None | 0 0
  1. let GetExpression() =
  2.    let priority operator =
  3.       match operator with
  4.       | "+" -> 1
  5.       | "-" -> 1
  6.       | "*" -> 2
  7.       | "/" -> 2
  8.       | "%" -> 2
  9.       | "^" -> 3
  10.       |  _  -> 0
  11.  
  12.    let isOperator (a : string) = (a.Length = 1 && priority(a) > 0)  
  13.  
  14.    use inputStream = new StreamReader("test.in")
  15.    use outputStream = new StreamWriter("test.out")
  16.    let InStr = inputStream.ReadToEnd()
  17.  
  18.    let stack = new Stack<string>()
  19.  
  20.    for i = 0 to InStr.Length - 1 do
  21.       let c = InStr.[i]
  22.       if System.Char.IsDigit(c) then outputStream.WriteLine(c.ToString())
  23.       else
  24.          match c with
  25.          | '(' ->
  26.             stack.push(c.ToString())
  27.          | ')' ->
  28.             while stack.top() <> "(" && (not stack.isEmpty) do
  29.                outputStream.WriteLine(stack.pop())
  30.             ignore(stack.pop())
  31.          | _   ->
  32.             while not stack.isEmpty
  33.                && (priority(stack.top()) >= priority(c.ToString()) && priority(c.ToString()) < 3
  34.                || (priority(stack.top()) >  priority(c.ToString()) && priority(c.ToString()) = 3))
  35.                   do
  36.                      outputStream.WriteLine(stack.pop())
  37.             stack.push(c.ToString())
  38.    while not stack.isEmpty do
  39.       outputStream.WriteLine(stack.pop())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement