-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_redundant_paran.cpp
More file actions
138 lines (136 loc) · 1.88 KB
/
remove_redundant_paran.cpp
File metadata and controls
138 lines (136 loc) · 1.88 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
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<ctype.h>
using namespace std;
struct elem{
int left_pa;
char left_op;
char min_op;
};
int min(int a,int b)
{
return a<b?a:b;
}
int priority(char a)
{
switch(a)
{
case '+':
case '-': return 1;
case '*':
case '/':return 2;
case '#':return -1;
default:return -1;
}
}
bool opr(char a)
{
switch(a)
{
case '+':
case '-':
case '*':
case '/': return true;
default:return false;
}
}
bool opd(char a)
{
return isalpha(a);
}
void print_stack(stack<elem>s)
{
while(!s.empty())
{
elem e = s.top();
s.pop();
cout<<e.left_pa<<" "<<e.left_op<<" "<<e.min_op<<endl;
}
}
void remove(string a)
{
int n = a.length();
int i = 0;
stack<elem>s;
vector<pair<int,int> >result;
while(i<n)
{
int flag = 0;
cout<<"\ni: "<<i<<" a[i]: "<<a[i];
elem e;
while(a[i]=='(' && !opd(a[i+1]))
{
e.left_pa = i;
e.left_op = ' ';
e.min_op = ' ';
s.push(e);
flag = 1;
i++;
}
if(flag)
{
//last (
e.left_pa = i;
e.min_op = ' ';
e.left_op = ' ';
i++;
}
if(opd(a[i]))
i++;
if(opr(a[i]))
{
if(e.left_op== ' ')//empty
{
e.left_op = a[i];
e.min_op = a[i];
}
else//update min_op
e.min_op = min(priority(a[i]),s.top().min_op);
s.push(e);
i++;
}
if(a[i]==')')//idx 6,10
{
int saved_idx = i;
elem new_elem = s.top();//idx 2,0
s.pop();
while((i+1)<n && a[i+1]==')') //)
{
elem red = s.top();
s.pop();
i++;//7
cout<<"\nRedundant at : "<<red.left_pa<<" "<<i;
}
if(i==n-1)
{
if(new_elem.left_pa!=' ')
{cout<<"\nRedundant at: "<<new_elem.left_pa<<" "<<i;
return;
}
}
// check if this paranthesis is necessary
if((i+1)<n && opr(a[i+1]))
{
i++;//idx 8
// check priority with popped element
if(priority(a[i])<=priority(new_elem.min_op))
{
cout<<"\nRedundant att : "<<new_elem.left_pa<<" "<<saved_idx;
}
}
i++;
}
}
}
/*
vector<pair<int,int>>::iterator ite;
for(ite = result.begin();ite!=result.end();++ite)
cout<<(*ite)->first<<","<<(*ite)->second<<endl;
*/
main()
{
string a = "(((a-b))*d*(c-d))";
remove(a);
}