-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSource.cpp
74 lines (63 loc) · 1.31 KB
/
Source.cpp
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
#include <iostream>
#include <string>
#include <stack>
using namespace std;
struct Node
{
char c;
Node* left;
Node* right;
};
Node* node = new Node();
stack<Node*> Stack_operator;
stack<Node*> Stack_operand;
void reading(const string expr)
{
cout << expr.length() << " letters "<<endl;
for (int i = 0; i < expr.length(); i++)
{
Node* readTemp = new Node();
if (expr[i] == '+' || expr[i] == '*' || expr[i] == '/' || expr[i] == '%' || expr[i] =='-')
{
readTemp->c = expr[i];
readTemp->left = Stack_operand.top();
readTemp->right = NULL;
Stack_operator.push(readTemp);
}
else
{
readTemp->c = expr[i];
readTemp->left = NULL; readTemp->right = NULL;
if (Stack_operator.size() == 0)
{
Stack_operand.push(readTemp);
}
else
{
Stack_operator.top()->right = readTemp;
Stack_operand.push(Stack_operator.top());
Stack_operator.pop();
}
}
}
}
void post_order_traversing(Node* root)
{
if(root->left) post_order_traversing(root->left);
if(root->right) post_order_traversing(root->right);
cout << root->c;
return;
}
int main(int argc, char* argv[])
{
string expr;
cout << "Enter a regular expression" << endl;
cin >> expr;
reading(expr);
cout << expr << endl;
node = Stack_operand.top();
post_order_traversing(node);
cout << endl;
cin.ignore();
return 0;
}