Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let GetExpression() =
- let priority operator =
- match operator with
- | "+" -> 1
- | "-" -> 1
- | "*" -> 2
- | "/" -> 2
- | "%" -> 2
- | "^" -> 3
- | _ -> 0
- let isOperator (a : string) = (a.Length = 1 && priority(a) > 0)
- use inputStream = new StreamReader("test.in")
- use outputStream = new StreamWriter("test.out")
- let InStr = inputStream.ReadToEnd()
- let stack = new Stack<string>()
- for i = 0 to InStr.Length - 1 do
- let c = InStr.[i]
- if System.Char.IsDigit(c) then outputStream.WriteLine(c.ToString())
- else
- match c with
- | '(' ->
- stack.push(c.ToString())
- | ')' ->
- while stack.top() <> "(" && (not stack.isEmpty) do
- outputStream.WriteLine(stack.pop())
- ignore(stack.pop())
- | _ ->
- while not stack.isEmpty
- && (priority(stack.top()) >= priority(c.ToString()) && priority(c.ToString()) < 3
- || (priority(stack.top()) > priority(c.ToString()) && priority(c.ToString()) = 3))
- do
- outputStream.WriteLine(stack.pop())
- stack.push(c.ToString())
- while not stack.isEmpty do
- outputStream.WriteLine(stack.pop())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement