Skip to content

Commit fed102b

Browse files
committed
calculator in go
1 parent 6b076a4 commit fed102b

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed

src/hard/Calculator.go

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
)
7+
8+
func Calculator(str string) int {
9+
tokens := tokenize(str)
10+
result, _ := evaluate(tokens)
11+
return result
12+
}
13+
14+
func tokenize(str string) []string {
15+
var tokens []string
16+
17+
for i := 0; i < len(str); i++ {
18+
switch {
19+
case str[i] == ' ':
20+
// Skip spaces
21+
continue
22+
case isDigit(str[i]):
23+
// If it's a digit, extract the entire number
24+
start := i
25+
for i < len(str) && isDigit(str[i]) {
26+
i++
27+
}
28+
tokens = append(tokens, str[start:i])
29+
i--
30+
default:
31+
// If it's not a digit, add the operator or parenthesis as a token
32+
tokens = append(tokens, string(str[i]))
33+
}
34+
}
35+
36+
return tokens
37+
}
38+
39+
func isDigit(char byte) bool {
40+
return char >= '0' && char <= '9'
41+
}
42+
43+
func evaluate(tokens []string) (int, []string) {
44+
var stack []int
45+
var operators []string
46+
47+
for len(tokens) > 0 {
48+
token := tokens[0]
49+
tokens = tokens[1:]
50+
51+
switch token {
52+
case "(":
53+
// Recursively evaluate the expression inside the parentheses
54+
result, remainingTokens := evaluate(tokens)
55+
stack = append(stack, result)
56+
tokens = remainingTokens
57+
case ")":
58+
// Return the result and the remaining tokens
59+
return stack[0], tokens
60+
case "+", "-", "*", "/":
61+
// Handle operators
62+
operators = append(operators, token)
63+
default:
64+
// Handle numbers
65+
num, _ := strconv.Atoi(token)
66+
stack = append(stack, num)
67+
}
68+
69+
// Check if there are operators on the stack that can be applied
70+
for len(operators) > 0 && len(stack) >= 2 && precedence(operators[len(operators)-1]) >= precedence(token) {
71+
operator := operators[len(operators)-1]
72+
operators = operators[:len(operators)-1]
73+
74+
num2 := stack[len(stack)-1]
75+
stack = stack[:len(stack)-1]
76+
77+
num1 := stack[len(stack)-1]
78+
stack = stack[:len(stack)-1]
79+
80+
// Apply the operator to the top two numbers on the stack
81+
result := applyOperator(num1, num2, operator)
82+
stack = append(stack, result)
83+
}
84+
}
85+
86+
// Evaluate any remaining operators on the stack
87+
for len(operators) > 0 && len(stack) >= 2 {
88+
operator := operators[len(operators)-1]
89+
operators = operators[:len(operators)-1]
90+
91+
num2 := stack[len(stack)-1]
92+
stack = stack[:len(stack)-1]
93+
94+
num1 := stack[len(stack)-1]
95+
stack = stack[:len(stack)-1]
96+
97+
result := applyOperator(num1, num2, operator)
98+
stack = append(stack, result)
99+
}
100+
101+
// Return the final result and the remaining tokens
102+
return stack[0], tokens
103+
}
104+
105+
func precedence(operator string) int {
106+
switch operator {
107+
case "+", "-":
108+
return 1
109+
case "*", "/":
110+
return 2
111+
}
112+
return 0
113+
}
114+
115+
func applyOperator(num1, num2 int, operator string) int {
116+
switch operator {
117+
case "+":
118+
return num1 + num2
119+
case "-":
120+
return num1 - num2
121+
case "*":
122+
return num1 * num2
123+
case "/":
124+
return num1 / num2
125+
default:
126+
return 0
127+
}
128+
}
129+
130+
func main() {
131+
// Example usage
132+
result := Calculator("2+(3-1)*3")
133+
fmt.Println(result) // Output: 8
134+
135+
result = Calculator("(2-0)(6/2)")
136+
fmt.Println(result) // Output: 6
137+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy