-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.cpp
More file actions
150 lines (125 loc) · 2.84 KB
/
node.cpp
File metadata and controls
150 lines (125 loc) · 2.84 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
142
143
144
145
146
147
148
149
150
#ifndef NODE_CPP
#define NODE_CPP
#include <iostream>
#include <string>
#include <sstream>
#include "node.h"
using namespace std;
Node::Node(char var){
data.var = var;
node_t = VARIABLE;
}
Node::Node(operator_type op, Node* operand1, Node* operand2){
data.op = op;
node_t = EXPRESSION;
this->operand1 = operand1;
this->operand2 = operand2;
}
Node::Node(int val){
data.val = val;
node_t = INTEGER;
}
void Node::set_left(Node* n){
operand1 = n;
}
void Node::set_right(Node* n){
operand2 = n;
}
void Node::set_op(operator_type op){
data.op = op;
}
void Node::set_parent(Node* p){
parent = p;
}
int Node::get_int(){
return data.val;
}
char Node::get_char(){
return data.var;
}
operator_type Node::get_op(){
return data.op;
}
char Node::print_operator() const {
if (data.op == PLUS)
return '+';
else if (data.op == MINUS)
return '-';
else if (data.op == MULT)
return '*';
else if (data.op == DIVIDE)
return '/';
}
string Node::print_infix() const {
if (operand1->node_t == EXPRESSION){
string infix = operand1->print_infix();
if (operand2->node_t == EXPRESSION){
string infix2 = operand2->print_infix();
infix = "(" + infix + print_operator() + infix2 + ")";
return infix;
}
}
else {
int i = operand2->data.val;
char d = (i+'0');
stringstream ss,dd,cc;
string target,target1,target2;
ss << d;
ss >> target;
dd << (print_operator());
dd >> target1;
cc << (operand1->get_char());
cc >> target2;
string node = "(" + target2 + target1 + target + ")";
return node;
}
}
string Node::print_prefix() const {
if (operand1->node_t == EXPRESSION){
string prefix = operand1->print_prefix();
if (operand2->node_t == EXPRESSION){
string prefix2 = operand2->print_prefix();
prefix = print_operator() + prefix + prefix2;
return prefix;
}
}
else {
int i = operand2->data.val;
char d = (i+'0');
stringstream ss,dd,cc;
string target,target1,target2;
ss << d;
ss >> target;
dd << (print_operator());
dd >> target1;
cc << (operand1->get_char());
cc >> target2;
string node = target1 + target2 + target;
return node;
}
}
string Node::print_postfix() const {
if (operand1->node_t == EXPRESSION){
string postfix = operand1->print_postfix();
if (operand2->node_t == EXPRESSION){
string postfix2 = operand2->print_postfix();
postfix = postfix + postfix2 + print_operator();
return postfix;
}
}
else {
int i = operand2->data.val;
char d = (i+'0');
stringstream ss,dd,cc;
string target,target1,target2;
ss << d;
ss >> target;
dd << (print_operator());
dd >> target1;
cc << (operand1->get_char());
cc >> target2;
string node = target2 + target + target1;
return node;
}
}
#endif