-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.cpp
More file actions
61 lines (54 loc) · 1.32 KB
/
list.cpp
File metadata and controls
61 lines (54 loc) · 1.32 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
#ifndef LIST_CPP
#define LIST_CPP
#include "list.h"
#include "node.h"
using namespace std;
operator_type s_to_operator(char c) {
if (c == '+')
return PLUS;
else if (c == '-')
return MINUS;
else if (c == '*')
return MULT;
else if (c == '/')
return DIVIDE;
}
Node* makeTree(string s){
string s2 = "";
for (int j = 0; j < 5; j++){
if (s[j] != ' ')
s2 += (s[j]);
}
Node* current = new Node(PLUS,NULL,NULL);
Node* top = current;
int i = 1;
char cursor = s[i];
while (i != s.size()){
if (cursor == '('){
Node* n = new Node(PLUS);
if (current->get_left() == NULL){
current->set_left(n);
}
else
current->set_right(n);
n->set_parent(current);
current = n;
}
else if ((cursor == '+')||(cursor=='-')||(cursor=='*')||(cursor=='/'))
current->set_op(s_to_operator(cursor));
else if (cursor == 'x'){
Node* b = new Node(cursor);
current->set_left(b);
}
else if ((cursor == '1')||(cursor == '2')||(cursor == '3')||(cursor == '4')||(cursor == '5')||(cursor == '6')||(cursor == '7')||(cursor == '8')||(cursor == '9')){
Node* a = new Node(cursor-'0');
current->set_right(a);}
else if (cursor == ')'){
current = current->get_parent();
}
i++;
cursor = s[i];
}
return top;
}
#endif