-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcal.go
175 lines (145 loc) · 2.58 KB
/
cal.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"fmt"
"strconv"
"strings"
"unicode"
)
type Fraction struct {
n int
d int
}
type Stack struct {
arry [20]Fraction
N int
}
func calculate(in_string string)(Fraction) {
var stack Stack
var i int
var answer Fraction
stack.N = 0
s := strings.Split(in_string, " ")
for i = 0; i < len(s); i++ {
if is_num(s[i]) {
var input_fra Fraction
input_fra = strt2fra(s[i])
stack.push(input_fra)
} else {
var ina Fraction
var inb Fraction
ina, inb = stack.pop()
if s[i] == "+" {
answer = add(inb, ina)
} else if s[i] == "-" {
answer = sub(inb, ina)
} else if s[i] == "*" {
answer = multiple(inb, ina)
} else if s[i] == "/" {
answer = fraction(inb, ina)
}
var f int
f = gcd(answer.n, answer.d)
answer.n /= f
answer.d /= f
stack.push(answer)
}
}
answer = stack.GetTop()
// if answer.d == 1 {
// fmt.Println("answer is ", answer.n)
// } else if answer.d < 0 {
// fmt.Println("answer is ", -answer.n, "/", -answer.d)
// } else {
// fmt.Println("answer is ", answer.n, "/", answer.d)
// }
return answer
}
func (s *Stack) GetTop() Fraction {
return s.arry[s.N]
}
func (s *Stack) pop() (Fraction, Fraction) {
var top Fraction
var topNext Fraction
top = s.arry[s.N]
s.N = s.N - 1
topNext = s.arry[s.N]
s.N = s.N - 1
return top, topNext
}
func (s *Stack) push(value Fraction) {
s.N++
s.arry[s.N] = value
}
func (s *Stack) printStack() {
var i int
for i = 0; i < s.N; i++ {
fmt.Println(s.arry[i])
}
}
//Fraction operation
func gcd(a, b int) int {
if b == 0 {
return a
}
return gcd(b, a%b)
}
func lcm(a, b int) int {
var hcf int
var temp int
hcf = a
temp = b
for hcf != temp {
if hcf > temp {
hcf -= temp
} else {
temp -= hcf
}
}
return (a * b) / hcf
}
func add(a, b Fraction) Fraction {
var new_d int
var c Fraction
new_d = lcm(a.d, b.d)
a.n = a.n * new_d / a.d
b.n = b.n * new_d / b.d
c.d = new_d
c.n = a.n + b.n
return c
}
func sub(a, b Fraction) Fraction {
//a > b
var new_d int
var c Fraction
new_d = lcm(a.d, b.d)
a.n = a.n * new_d / a.d
b.n = b.n * new_d / b.d
c.d = new_d
c.n = a.n - b.n
return c
}
func multiple(a, b Fraction) Fraction {
var y Fraction
y.n = a.n * b.n
y.d = a.d * b.d
return y
}
func fraction(a, b Fraction) Fraction {
var y Fraction
y.n = a.n * b.d
y.d = a.d * b.n
return y
}
func is_num(a string) bool {
return unicode.IsDigit(rune(a[0]))
}
func strt2fra(in string) Fraction {
var fra Fraction
input_num, err := strconv.Atoi(in)
if err != nil {
fmt.Println("turn int err")
}
fra.n = input_num
fra.d = 1
return fra
}