-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy path727.go
More file actions
73 lines (64 loc) · 961 Bytes
/
727.go
File metadata and controls
73 lines (64 loc) · 961 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// UVa 727 - Equation
package main
import (
"fmt"
"io"
"os"
)
const max = 50
var (
count int
stack [max]string
out io.WriteCloser
)
func pop() {
count--
fmt.Fprintf(out, "%s", stack[count])
}
func push(s string) {
stack[count] = s
count++
}
func solve(s string) {
switch s {
case "+", "-":
for count > 0 && stack[count-1] != "(" {
pop()
}
push(s)
case "*", "/":
for count > 0 && stack[count-1] != "(" && stack[count-1] != "+" && stack[count-1] != "-" {
pop()
}
push(s)
case "(":
push(s)
case ")":
for stack[count-1] != "(" {
pop()
}
count--
default:
fmt.Fprint(out, s)
}
}
func main() {
in, _ := os.Open("727.in")
defer in.Close()
out, _ = os.Create("727.out")
defer out.Close()
var n int
for fmt.Fscanf(in, "%d\n\n", &n); n > 0; n-- {
for {
var s string
if fmt.Fscanf(in, "%s\n", &s); len(s) == 0 {
break
}
solve(s)
}
for count != 0 {
pop()
}
fmt.Fprintln(out)
}
}