-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.go
175 lines (164 loc) · 4.05 KB
/
calculator.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
//Jared McDonald
//CSC 442
package main
import "fmt"
import "bufio"
import "os"
import "strconv"
import "strings"
import "bytes"
import "unicode"
import "math"
type DStack struct{
top *DElement;
size int;
}
type DElement struct {
value float64
next *DElement
}
func (s *DStack) push(value float64) {
if value > 0{
s.top = &DElement{value, s.top}
s.size++
}
}
func (s *DStack) isEmpty() bool {
return s.size == 0
}
func (s *DStack) pop() (value float64) {
if s.size > 0 {
value, s.top = s.top.value, s.top.next
s.size--
return
}
return
}
type SStack struct{
top *SElement;
size int;
}
type SElement struct {
value string
next *SElement
}
func (s *SStack) push(value string) {
s.top = &SElement{value, s.top}
s.size++
}
func (s *SStack) isEmpty() bool {
return s.size == 0
}
func (s *SStack) pop() (value string) {
if s.size > 0 {
value, s.top = s.top.value, s.top.next
s.size--
return
}
return
}
func main() {
reader := bufio.NewScanner(os.Stdin)
fmt.Printf("You can terminate the program by entering = instead of an equation.\n")
var finished int = 0
for finished < 1{
fmt.Printf("Please enter an equation: ")
reader.Scan()
if reader.Text() != "="{
var line string = reader.Text()
var r float64 = calc(line)
if r != -99.99{
fmt.Printf("Answer: ")
fmt.Printf("%g\n",r)
}else{
finished = 1
}
} else if reader.Text() == "="{
finished = 1
fmt.Println("Program terminated")
os.Exit(0)
}
}
}
func calc(line string) float64{
if line == "="{
return -99.99
}
var result float64 = 0.0;
line =strings.Replace(line,"sqrt ","|",-1)
line =strings.Replace(line," ","",-1)
if n, err := strconv.ParseFloat(line, 64); err == nil {
result = n
return result
} else{
var buffer bytes.Buffer
numstack := new(DStack)
opstack := new(SStack)
temp := strings.Split(line,"=")
line = temp[0]
for _, r := range line{
if unicode.IsDigit(rune(r)) || unicode.IsLetter(rune(r)) || string(r) == "."{
buffer.WriteString(string(r))
} else if string(r) == "+" || string(r) == "-" || string(r) == "/" || string(r) == "*" || string(r) == "^" || string(r) == "|"{
if buffer.String() != "<nil>"{
if n, err := strconv.ParseFloat(buffer.String(), 64); err == nil {
numstack.push(n)
buffer.Reset()
}
}
if opstack.isEmpty(){
opstack.push(string(r))
} else{
if !opstack.isEmpty(){
var currentop string= opstack.pop()
var operand float64 = numstack.pop()
if currentop != "|"{
var operandt float64 = numstack.pop()
result = operate(operand,operandt,currentop)
} else{
result = operate(operand,0.5,currentop)
}
numstack.push(result)
}
opstack.push(string(r))
}
}
numstack.push(result)
}
if buffer.String() != "<nil>"{
if n, err := strconv.ParseFloat(buffer.String(), 64); err == nil {
numstack.push(n)
buffer.Reset()
}
}
for !opstack.isEmpty(){
var currentop string= opstack.pop()
var operand float64 = numstack.pop()
if currentop != "|"{
var operandt float64 = numstack.pop()
result = operate(operand,operandt,currentop)
} else{
result = operate(operand,0.5,currentop)
}
numstack.push(result)
}
return result
}
}
func operate(x float64, y float64, op string) float64{
var r float64 = 0.0
if op == "+"{
r = y+x
} else if op == "-"{
r = (y-x)
} else if op == "/"{
r = (y/x)
} else if op == "*"{
r = (y*x)
} else if op == "^"{
r = math.Pow(y,x)
} else if op == "|"{
r = math.Pow(x,y)
}
return r;
}