-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanaREs.cpp
129 lines (120 loc) · 3.11 KB
/
anaREs.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
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
//
// Created by mush on 2019/12/10.
//
#include"structs.h"
#include<string>
#include<set>
#include<stack>
#include<map>
#include<vector>
#include<fstream>
#include<algorithm>
#include<iostream>
#include <cstring>
using std::string;
using std::stack;
using std::set;
using std::vector;
using std::iostream;
using std::map;
using std::fstream;
//using std::algorithm;
string addDot(string in);
string infix2Shuffix(string in);
void getREs(vector<RE>& reFinal){
fstream in("./re",std::ios::in);
while(!in.eof()){
string cur;
getline(in,cur);
char* buffer;
char* token;
char *curC = new char[cur.size()];
strcpy(curC,cur.c_str());
token= strtok_r(curC, " ", &buffer);
RE* curRE=new RE;
curRE->symbol=token;
token= strtok_r(NULL," ",&buffer);
string tempRe=token;
tempRe=infix2Shuffix(addDot(tempRe));
curRE->re=tempRe;
reFinal.push_back(*curRE);
delete curC;
}
}
string addDot(string in){
set<char> symbolList={'|','*','(',')'};
char* temp=new char[in.size()*2];
int pos=0;
temp[0]=in[0];
for (int i = 1; i < (int)in.size(); ++i) {
if((symbolList.find(in[i])==symbolList.end()&&symbolList.find(temp[pos])==symbolList.end())
||(temp[pos]==')'&&in[i]=='(')
||(temp[pos]==')'&&symbolList.find(in[i])==symbolList.end())
||((in[i]=='('&&symbolList.find(temp[pos])==symbolList.end())
||(temp[pos]=='*'&&symbolList.find(in[i])==symbolList.end()))
){
temp[++pos]='`';
temp[++pos]=in[i];
}else{
temp[++pos]=in[i];
}
}
string res(temp);
delete temp;
return res;
}
string infix2Shuffix(string in){
set<char> symbolList={'|','*','(',')','`'};
stack<char> _operandStack;
_operandStack.push('@');
map<char,int>outStack;
outStack['@']=0;
outStack['(']=0;
outStack[')']=0;
outStack['|']=1;
outStack['`']=2;
outStack['*']=3;
int t=in.size();
char* resC=new char[in.size()];
int pos=0;
int i=0;
while (i<in.size()){
if(symbolList.find(in[i])==symbolList.end()){
resC[pos]=in[i];
pos++;
i++;
}
else{
char temp=in[i];
if (temp=='('){
_operandStack.push(temp);
i++;
}
else if(temp==')'){
while (_operandStack.top()!='('){
resC[pos]=_operandStack.top();
_operandStack.pop();
pos++;
}
_operandStack.pop();
i++;
}
else {
while(outStack[_operandStack.top()]>=outStack[in[i]]){
resC[pos]=_operandStack.top();
_operandStack.pop();
pos++;
}
_operandStack.push(in[i]);
i++;
}
}
}
while(_operandStack.top()!='@'){
resC[pos]=_operandStack.top();
_operandStack.pop();
pos++;
}
string res(resC);
return res;
}