-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathequationevaluation.go
155 lines (140 loc) · 2.7 KB
/
equationevaluation.go
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"fmt"
"strconv"
"unicode"
)
type stack []string
func (s *stack) isempty() bool {
return len(*s) == 0
}
func (s *stack) push(str string) {
*s = append(*s, str)
}
func (s *stack) pop() bool {
if s.isempty() {
return false
} else {
index := len(*s) - 1
*s = (*s)[:index]
return true
}
}
func (s *stack) top() string {
if s.isempty() {
return ""
} else {
index := len(*s) - 1
item := (*s)[index]
return item
}
}
func prec(s string) int {
if s == "^" {
return 3
} else if (s == "/") || (s == "*") {
return 2
} else if (s == "+") || (s == "-") {
return 1
} else {
return -1
}
}
func infixtopostfix(expression string) string {
var sta stack
var postfix string
for i := 0; i < len(expression); i++ {
s := expression[i]
if s == ' ' {
continue
} else if unicode.IsDigit(rune(s)) {
var temp string
for unicode.IsDigit(rune(expression[i])) {
temp = temp + string(expression[i])
i++
if i < len(expression) {
s = expression[i]
} else {
break
}
}
i--
postfix = postfix + temp + ","
} else if s == '(' {
sta.push(string(s))
} else if expression[i] == ')' {
for sta.top() != "(" {
postfix = postfix + sta.top() + ","
sta.pop()
}
sta.pop()
} else {
for !sta.isempty() && prec(string(s)) <= prec(sta.top()) {
postfix = postfix + sta.top() + ","
sta.pop()
}
sta.push(string(s))
}
}
for !sta.isempty() {
postfix = postfix + sta.top() + ","
sta.pop()
}
return postfix
}
func postfixevaluation(expression string) string {
var sta stack
for i := 0; i < len(expression); i++ {
if expression[i] == ',' {
continue
} else if unicode.IsDigit(rune(expression[i])) {
var temp string
if i < len(expression)-1 {
for unicode.IsDigit(rune(expression[i])) {
temp = temp + string(expression[i])
i++
if i >= len(expression) {
break
}
}
i--
} else {
temp = string(expression[i])
}
sta.push(temp)
} else {
val1 := sta.top()
sta.pop()
val2 := sta.top()
sta.pop()
a, err1 := strconv.Atoi(val1)
b, err2 := strconv.Atoi(val2)
if err1 == err2 {
fmt.Print()
}
if expression[i] == '+' {
res := b + a
sta.push(strconv.Itoa(res))
} else if expression[i] == '-' {
res := b - a
sta.push(strconv.Itoa(res))
} else if expression[i] == '*' {
res := b * a
sta.push(strconv.Itoa(res))
} else if expression[i] == '/' {
res := b / a
sta.push(strconv.Itoa(res))
}
}
}
res := sta.top()
return res
}
func main() {
//infix := "100 * ( 2 + 12 ) / 14"
var infix string
fmt.Scanln(&infix)
postfix := infixtopostfix(infix)
evaluation := postfixevaluation(postfix)
fmt.Println(evaluation)
}