-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplementation.go
More file actions
141 lines (129 loc) · 2.69 KB
/
implementation.go
File metadata and controls
141 lines (129 loc) · 2.69 KB
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
package lab2
import (
"fmt"
)
type StackNode struct {
data string
next *StackNode
}
func getStackNode(data string, top *StackNode) *StackNode {
// return new StackNode
return &StackNode{
data,
top,
}
}
type MyStack struct {
top *StackNode
count int
}
func getMyStack() *MyStack {
// return new MyStack
return &MyStack{
nil,
0,
}
}
// Returns the number of element in stack
func (this MyStack) size() int {
return this.count
}
func (this MyStack) isEmpty() bool {
if this.size() > 0 {
return false
} else {
return true
}
}
// Add a new element in stack
func (this *MyStack) push(data string) {
// Make a new stack node
// And set as top
this.top = getStackNode(data, this.top)
// Increase node value
this.count++
}
// Add a top element in stack
func (this *MyStack) pop() string {
var temp string = ""
if this.isEmpty() == false {
// Get remove top value
temp = this.top.data
this.top = this.top.next
// Reduce size
this.count--
}
return temp
}
// Used to get top element of stack
func (this MyStack) peek() string {
if !this.isEmpty() {
return this.top.data
} else {
return ""
}
}
// Check operator
func isOperator(text byte) bool {
if text == '+' || text == '-' ||
text == '*' || text == '/' ||
text == '^' || text == '%' {
return true
}
return false
}
// Check operands
func isOperands(text byte) bool {
if (text >= '0' && text <= '9') ||
(text >= 'a' && text <= 'z') ||
(text >= 'A' && text <= 'Z') {
return true
}
return false
}
// Converting the given postfix expression to
// infix expression
func postfixToInfix(postfix string) (string, error) {
// Get the size
var size int = len(postfix)
// Create stack object
var s *MyStack = getMyStack()
// Some useful variables which is using
// of to storing current result
var auxiliary string = ""
var op1 string = ""
var op2 string = ""
var isValid bool = true
for i := 0; i < size && isValid; i++ {
// Check whether given postfix location
// at [i] is an operator or not
if isOperator(postfix[i]) {
// When operator exist
// Check that two operands exist or not
if s.size() > 1 {
op1 = s.pop()
op2 = s.pop()
auxiliary = "(" + op2 + string(postfix[i]) + op1 + ")"
s.push(auxiliary)
} else {
isValid = false
}
} else if isOperands(postfix[i]) {
// When get valid operands
auxiliary = string(postfix[i])
s.push(auxiliary)
} else {
// Invalid operands or operator
isValid = false
}
}
if isValid == false {
// When have something wrong
// fmt.Println("Invalid postfix : ", postfix)
return postfix, fmt.Errorf("invalid expression")
} else {
// Display calculated result
// fmt.Println(" Postfix : ", postfix)
return s.pop(), nil
}
}