forked from sapai05/cpp-and-datastructures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshuntingYard.cpp
More file actions
118 lines (109 loc) · 3.04 KB
/
shuntingYard.cpp
File metadata and controls
118 lines (109 loc) · 3.04 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
#include <iostream>
#include <stack>
#include <queue>
#include <string>
#include <sstream>
/*
* Written by: Sahil Pai
* This is a shunting yard algorithm that puts integers on one side and operators on another side.
* Used the wikipedia linked to help write this.
*/
using namespace std;
//Binary tree
struct Node {
char op;
double num;
Node* left;
Node* right;
};
//create new node
Node* newNode(char op, double num) {
Node* node = new Node;
node->op = op;
node->num = num;
node->left = NULL;
node->right = NULL;
return node;
}
//check if operator
bool isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')' || c == '%' || c == '^' || c == '!');
}
int getPrecedence(char c) {
if (c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')' || c == '%' || c == '^' || c == '!') {
return 1;
}
else {
return 0;
}
}
void shuntingYard(char input[40]) {
//make stack
stack<char> opStack;
queue<Node*> outputQueue;
stringstream ss(input);
string token;
while (getline(ss, token, ' ')) {
//if number...
if (isdigit(token[0])) {
double num;
stringstream(token) >> num;
outputQueue.push(newNode('\0', num));
}
//if operator...
else if (isOperator(token[0])) {
char op = token[0];
while (!opStack.empty() && isOperator(opStack.top()) && getPrecedence(op) <= getPrecedence(opStack.top())) {
outputQueue.push(newNode(opStack.top(), 0));
opStack.pop();
}
opStack.push(op);
}
}
while (!opStack.empty()) {
outputQueue.push(newNode(opStack.top(), 0));
opStack.pop();
}
stack<Node*> evalStack;
while (!outputQueue.empty()) {
Node* node = outputQueue.front();
outputQueue.pop();
if (node->op == '\0') {
evalStack.push(node);
}
else {
Node* right = evalStack.top();
evalStack.pop();
Node* left = evalStack.top();
evalStack.pop();
node->left = left;
node->right = right;
evalStack.push(node);
}
}
//eval postfix
Node* root = evalStack.top();
evalStack.pop();
stack<Node*> printStack;
printStack.push(root);
while (!printStack.empty()) {
Node* node = printStack.top();
printStack.pop();
if (node->op == '\0') {
cout << node->num << " ";
}
else {
cout << node->op << " ";
printStack.push(node->right);
printStack.push(node->left);
}
}
}
int main() {
//string input = "3 + 2 + 5 - 4 * 5 - 3 ^ 2 + 5";
char input[40];
cout << "Enter your expression, seperating each character with a space." << endl;
cin.getline(input, 100);
shuntingYard(input);
return 0;
}