-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordAnalysis(1).h
424 lines (418 loc) · 7.03 KB
/
wordAnalysis(1).h
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
enum NoteType
{
id, num
};
struct Notation{
string note;
NoteType type;
int ind;
Notation(string s, NoteType t) :note(s), type(t){};
};
vector<Notation>NoteList;
//输出符号列表。。。。。?要更换i:id,n:num,好吧
void OutputList(void){
for each (Notation var in NoteList)
{
if (var.type == id)
{
cout << "id" << var.ind << '\t' << var.note << endl;
}
else
{
cout << "num" << var.ind << '\t' << var.note << endl;
}
}
}
const string T = "∫";
const string M = "∑";
struct symbol
{
char ch = 'T';//T,M,i,n,(,),' '
int id_num_ind;
bool printable = true;
int top = 200;
int left = 500;
int front_size = 100;
string font_style = "normal";//oblique ,normal
void symbol::printsymbol(ofstream&out)const;
};
void symbol::printsymbol(ofstream&out)const
{
if (!printable)
{//{,},^,_,$
return;
}
out << "<div style=\"position: absolute;top: " << top << "px; left:" << left << "px;\"><";
out << "span style=\"font-size:" << front_size << "px; font-style:" << font_style << ";";
out << "line-height:100%;\">";
if (ch == 'i')
{
string not;
for each (Notation var in NoteList)
{
if (var.type == id&&var.ind == id_num_ind)
{
not = var.note;
break;
}
}
out << not;
}
else
{
if (ch == 'n')
{
string not;
for each (Notation var in NoteList)
{
if (var.type == num&&var.ind == id_num_ind)
{
not = var.note;
break;
}
}
out << not;
}
else
{
if (ch == 'T')
{
out << T;
}
else
{
if (ch == 'M')
{
out << M;
}
else
{
out << ch;//(,),' '
}
}
}
}
out << "</span></div>" << endl;
}
typedef vector<symbol>SymboList;
vector<Notation>InstallIntoList(string id_num, NoteType t, SymboList &out){
Notation n(id_num, t);
int c = 0;
for each (Notation var in NoteList)
{
if (var.note.compare(id_num) == 0)
{
if (t == id)
{
symbol e;
e.ch = 'i';
e.id_num_ind = c + 1;
out.push_back(e);
}
else
{
symbol e;
e.ch = 'n';
e.id_num_ind = c + 1;
out.push_back(e);
}
return NoteList;
}
if (var.type == t)
{
c++;
}
}
n.ind = c + 1;
NoteList.push_back(n);
if (t == id)
{
symbol e;
e.ch = 'i';
e.id_num_ind = c + 1;
out.push_back(e);
}
else
{
symbol e;
e.ch = 'n';
e.id_num_ind = c + 1;
out.push_back(e);
}
return NoteList;
}
//大括号匹配
bool BigBracketMatch(const string B, int pos){
stack<char> bigbracket;
size_t length = B.length();
int ec = 0;
for (size_t i = 0; i < length; i++)
{
if (B[i] == '{')
{
bigbracket.push(B[i]);
}
else
{
if (B[i] == '}')
{
if (bigbracket.empty())
{
ec = 1;//too much '}'
cout << "Big Bracket can not match at POSITION:" << pos + i << endl;
cout << "too much '}'" << endl;
cout << B << endl;
return false;
}
bigbracket.pop();
}
}
}
if (!bigbracket.empty())
{
ec = 2;//too much '{'
cout << "Big Bracket can not match at POSITION:" << pos << endl;
cout << "too much '{'" << endl;
cout << B << endl;
return false;
}
return true;
}
//小括号匹配
bool LittleBracketMatch(const string B, int pos){
stack<char> bigbracket;
size_t length = B.length();
int ec = 0;
for (size_t i = 0; i < length; i++)
{
if (B[i] == '(')
{
bigbracket.push(B[i]);
}
else
{
if (B[i] == ')')
{
if (bigbracket.empty())
{
ec = 1;//too much ')'
cout << "Little Bracket can not match at POSITION:" << pos + i << endl;
cout << "too much ')'" << endl;
cout << B << endl;
return false;
}
bigbracket.pop();
}
}
}
if (!bigbracket.empty())
{
ec = 2;//too much '('
cout << "Little Bracket can not match at POSITION:" << pos << endl;
cout << "too much '('" << endl;
cout << B << endl;
return false;
}
return true;
}
bool BracketMatch(const string B){
return BigBracketMatch(B, 0) && LittleBracketMatch(B, 0);
}
//judge Special charcter \,(,),{,},_,^,'\0','\n'
bool isSpeciNotation(char c){
if (c == '(' || c == ')' || c == '{' || c == '}' || c == '_' || c == '^' || c == '\\' || c == ' ' || c == '\n')
{
return true;
}
return false;
}
//judge charcter whether expected
bool NoOtherNotation(const string B){
char c;
for (size_t i = 0; i < B.length(); i++)
{
c = B.at(i);
if (!isalnum(c) && !isSpeciNotation(c)){
//输出意外符号的ascii和位置
cout << "These is an unexpected notation: ASCII:" << toascii(c) << endl;
cout << "Position:" << i << endl;
return false;
}
}
return true;
}
//read sample.txt in
string readFile2String(char*filename){
ifstream ifile(filename);
ostringstream buf;
char ch;
while (buf&&ifile.get(ch))
buf.put(ch);
return buf.str();
}
string MatchNum(const string B, size_t &p){
string ret;
if (!isdigit(B[p]))
{
cout << "Function Call Error:MatchNum" << endl;
return ret;
}
for (; p < B.length(); p++)
{
if (isdigit(B[p])){
ret.push_back(B[p]);
}
else
{
break;
}
}
return ret;
}
string MatchId(const string B, size_t &p){
string ret;
if (!isalpha(B[p]))
{
cout << "Function Call Error:MatchId" << endl;
return ret;
}
for (; p < B.length(); p++)
{
if (isalnum(B[p])){
ret.push_back(B[p]);
}
else
{
break;
}
}
return ret;
}
bool MatchBlank(const string B, size_t &p){
if (p + 5 >= B.length())
{
return false;
}
string b = "blank";
if (b.compare(B.substr(p + 1, 5)) == 0){
p += 6;
return true;
}
else
{
return false;
}
}
bool MatchInt(const string B, size_t &p){
if (p + 3 >= B.length())
{
return false;
}
string b = "int";
if (b.compare(B.substr(p + 1, 3)) == 0){
p += 4;
return true;
}
else
{
return false;
}
}
bool MatchSum(const string B, size_t &p){
if (p + 3 >= B.length())
{
return false;
}
string b = "sum";
if (b.compare(B.substr(p + 1, 3)) == 0){
p += 4;
return true;
}
else
{
return false;
}
}
vector<Notation> InstallNote(const string B, SymboList &out){
size_t p = 0, length = B.length(); char c; string id_num;
while (p < length)
{
c = B.at(p);
if (isSpeciNotation(c))
{
if (c == '\\')
{
if (MatchBlank(B, p))
{
symbol e;
e.ch = ' ';
out.push_back(e);
continue;
}
if (MatchInt(B, p))
{
symbol e;
e.ch = 'T';
out.push_back(e);
continue;
}
if (MatchSum(B, p))
{
symbol e;
e.ch = 'M';
out.push_back(e);
continue;
}
cout << "Error at " << p << " ,'\' has wrong follow" << endl;
p++;
}
else
{
symbol e;
e.ch = c;
out.push_back(e);
p++;
}
}
else
{
if (isalpha(c))
{
id_num = MatchId(B, p);
NoteList = InstallIntoList(id_num, id, out);
}
else
{
if (isdigit(c))
{
id_num = MatchNum(B, p);
NoteList = InstallIntoList(id_num, num, out);
}
else
{
cout << "Error at Postition" << p << endl;
}
}
}
}
return NoteList;
}
string noindex(const string text)
{
string ret;
for each (char var in text)
{
if (!isdigit(var))ret.push_back(var);
}
return ret;
}
string covert2string(const SymboList sl)
{
string ret;
for each (symbol var in sl)
{
ret.push_back(var.ch);
}
ret.push_back('$');
return ret;
}