-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
198 lines (165 loc) · 6.77 KB
/
index.html
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Infix to Postfix Homework</title>
</head>
<body>
<div><input type="text" id="input" value=" ( ( 2 + 1 ) *2 ) ^ ( 8 - 5 )" /></div>
<div id="output">Output:<br /></div>
<script type="text/javascript">
function Stack() {
this.stack = new Array();
this.pop = function() {
return this.stack.pop();
}
this.push = function(item) {
this.stack.push(item);
}
this.peek = function() {
return this.stack[this.stack.length-1];
}
this.isEmpty = function() {
return this.stack.length == 0;
}
}
function InfixToPostfix() {
this.operatorStack = new Stack();
this.infixExpression = "";
this.postFixExpression = "";
this.write = function(msg) {
var div = document.getElementById('output');
div.innerHTML = div.innerHTML + msg + ' Postfix= ' + this.postFixExpression + '<br />';
}
this.isOperator = function(chr) {
return (chr == '+' || chr == '-' || chr == '*' || chr == '/' || chr == '^');
}
this.getPriority = function(operator) {
switch (operator) {
case '^': return 3;
case '*': case '/': return 2;
case '+': case '-': return 1;
}
}
this.checkPrecedence = function(op1, op2) {
if (this.getPriority(op1) <= this.getPriority(op2)) {
return true;
}
}
var tokenIndex = 0;
this.getNextToken = function (infixExpr) {
while (tokenIndex < infixExpr.length && infixExpr[tokenIndex] == ' ') tokenIndex++;
if (tokenIndex == infixExpr.length) return '';
var b = '';
while (tokenIndex < infixExpr.length && infixExpr[tokenIndex] != ' ' && infixExpr[tokenIndex] != '(' && infixExpr[tokenIndex] != ')' && !this.isOperator(infixExpr[tokenIndex])) b += infixExpr[tokenIndex++];
if (b != '') return b;
return infixExpr[tokenIndex++];
};
this.convertToPostfix = function(infixExpr) {
var chr;
while((chr = this.getNextToken(infixExpr)) != '') {
if (chr == '(') {
// Açılış parantezi direkt stacke atıldı
this.operatorStack.push(chr);
this.write(chr + " stacke atıldı.");
}
else if (chr == ')') {
// Açılış parantezi bulunana kadar ilerle
this.write(chr + " stackten alındı.");
while(this.operatorStack.peek() != '(' && !this.operatorStack.isEmpty()) {
var temp = this.operatorStack.pop();
this.postFixExpression += " " + temp;
this.write(temp + " stackten alındı, postfixe atıldı.");
}
var temp = this.operatorStack.pop();
this.write(temp + " stackten alındı.");
}
else if (this.isOperator(chr)) {
while(this.checkPrecedence(chr, this.operatorStack.peek())) {
var temp = this.operatorStack.pop();
this.postFixExpression += " " + temp;
this.write(temp + " stackten alındı, postfixe atıldı.");
}
this.operatorStack.push(chr);
this.write(chr + " stacke atıldı.");
}
else {
// Operand gelirse postfixe at
this.postFixExpression += " " + chr;
this.write(chr + " postfixe atıldı.");
}
}
while(this.operatorStack.peek() != null){
var temp = this.operatorStack.pop();
this.postFixExpression += " " + temp;
this.write(temp + " stackten alındı, postfixe atıldı.");
}
return this.postFixExpression;
}
}
function PostfixCalculator() {
this.operatorStack = new Stack();
this.operandStack = new Stack();
this.init = function(postFixExpr) {
var postfixItems = postFixExpr.trim().split(' ');
for (var i = 0; i < postfixItems.length; i++) {
var chr = postfixItems[i];
if (chr == '+' || chr == '-' || chr == '*' || chr == '/' || chr == '^') {
this.operatorStack.push(chr);
}
else{
this.operandStack.push(chr);
}
}
}
this.sum = function () {
var result = parseFloat(this.operandStack.stack[this.operandStack.stack.length - 2]) + parseFloat(this.operandStack.stack[this.operandStack.stack.length - 1])
this.operandStack.stack.pop();
this.operandStack.stack[this.operandStack.stack.length - 1] = result;
}
this.subtract = function() {
var result = parseFloat(this.operandStack.stack[this.operandStack.stack.length - 2]) - parseFloat(this.operandStack.stack[this.operandStack.stack.length - 1])
this.operandStack.stack.pop();
this.operandStack.stack[this.operandStack.stack.length - 1] = result;
}
this.divide = function() {
var result = parseFloat(this.operandStack.stack[this.operandStack.stack.length - 2]) / parseFloat(this.operandStack.stack[this.operandStack.stack.length - 1])
this.operandStack.stack.pop();
this.operandStack.stack[this.operandStack.stack.length - 1] = result;
}
this.multiply = function() {
var result = parseFloat(this.operandStack.stack[this.operandStack.stack.length - 2]) * parseFloat(this.operandStack.stack[this.operandStack.stack.length - 1])
this.operandStack.stack.pop();
this.operandStack.stack[this.operandStack.stack.length - 1] = result;
}
this.power = function() {
var result = Math.pow(parseFloat(this.operandStack.stack[this.operandStack.stack.length - 2]), parseFloat(this.operandStack.stack[this.operandStack.stack.length - 1]))
this.operandStack.stack.pop();
this.operandStack.stack[this.operandStack.stack.length - 1] = result;
}
this.calculate = function(postFixExpr) {
this.init(postFixExpr);
for (var i = 0; i <= this.operatorStack.stack.length - 1; i++) {
switch (this.operatorStack.stack[i]) {
case '+': this.sum(); break;
case '-': this.subtract(); break;
case '/': this.divide(); break;
case '*': this.multiply(); break;
case '^': this.power(); break;
}
}
return this.operandStack.stack[0];
}
}
document.getElementById("input").onkeypress = function (e) {
if (e.keyCode == 13) {
document.getElementById("output").innerHTML = "Output:<br />";
var infixToPostfixConverter = new InfixToPostfix();
var pfCalculator = new PostfixCalculator();
var postFixExpression = infixToPostfixConverter.convertToPostfix(document.getElementById("input").value);
alert(pfCalculator.calculate(postFixExpression));
}
}
</script>
</body>
</html>