forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10467.cpp
201 lines (191 loc) · 4.1 KB
/
10467.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
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
#include <bits/stdc++.h>
using namespace std;
/* @JUDGE_ID: 2905EH 10467 C++ "carfull output!!" */
//2003/3/26 a_q10467.cpp
//#include <cassert>
#define GAP 5
#define ADDR(x, y) (((y) << 16) | (x))
#define GETY(y) ((y) >> 16)
#define GETX(x) ((x)&0xFFFF)
struct PictureStruct
{
map<int, char> pos2char;
int width;
void set(int x, int y, char ch)
{
pos2char[ADDR(x, y)] = ch;
}
/*
char get(int x,int y) const
{
map<int,char>::const_iterator it=pos2char.find(y*65536+x);
if (it==pos2char.end()) return ' ';
return (*it).second;
}
*/
int getFirstLineX() const
{
map<int, char>::const_iterator it = pos2char.begin();
return GETX((*it).first);
}
void print() const
{
int y = 0, x = -1;
map<int, char>::const_iterator it;
for (it = pos2char.begin(); it != pos2char.end(); ++it)
{
int yy = GETY((*it).first);
int xx = GETX((*it).first);
if (y < yy)
{
x = -1;
putchar('\n');
}
for (int k = xx - x - 1; --k >= 0;)
{
putchar(' ');
}
putchar((*it).second);
y = yy;
x = xx;
}
putchar('\n');
}
void paint(int x, int y, const PictureStruct &pic)
{
map<int, char>::const_iterator it;
for (it = pic.pos2char.begin(); it != pic.pos2char.end(); ++it)
{
int yy = GETY((*it).first);
int xx = GETX((*it).first);
pos2char[ADDR((x + xx), (y + yy))] = (*it).second;
}
}
};
char line[256];
PictureStruct parseExpr(int a, int b);
PictureStruct parseTerm(int a, int b);
PictureStruct parseFactor(int a, int b);
PictureStruct parseExpr(int a, int b)
{
PictureStruct ret;
int depth = 0;
for (int i = b; --i >= a;)
{
if (line[i] == ')')
{
depth++;
}
else if (line[i] == '(')
{
depth--;
}
else if (depth == 0 && (line[i] == '+' || line[i] == '-'))
{
PictureStruct left = parseExpr(a, i);
PictureStruct right = parseTerm(i + 1, b);
ret.width = left.width + right.width + GAP;
ret.paint(0, 2, left);
ret.paint(left.width + GAP, 2, right);
int x1 = 0 + left.getFirstLineX();
int x2 = left.width + GAP + right.getFirstLineX();
for (int i = x1; i <= x2; i++)
{
ret.set(i, 1, '=');
}
ret.set(left.width + GAP / 2, 0, 'E');
ret.set(left.width + GAP / 2, 1, '|');
ret.set(left.width + GAP / 2, 2, line[i]);
return ret;
}
}
PictureStruct c = parseTerm(a, b);
ret.width = c.width;
ret.paint(0, 2, c);
int xx = c.getFirstLineX();
ret.set(xx, 0, 'E');
ret.set(xx, 1, '|');
return ret;
}
PictureStruct parseTerm(int a, int b)
{
PictureStruct ret;
int depth = 0;
for (int i = b; --i >= a;)
{
if (line[i] == ')')
{
depth++;
}
else if (line[i] == '(')
{
depth--;
}
else if (depth == 0 && (line[i] == '*' || line[i] == '/'))
{
PictureStruct left = parseTerm(a, i);
PictureStruct right = parseFactor(i + 1, b);
ret.width = left.width + right.width + GAP;
ret.paint(0, 2, left);
ret.paint(left.width + GAP, 2, right);
int x1 = 0 + left.getFirstLineX();
int x2 = left.width + GAP + right.getFirstLineX();
for (int i = x1; i <= x2; i++)
{
ret.set(i, 1, '=');
}
ret.set(left.width + GAP / 2, 0, 'T');
ret.set(left.width + GAP / 2, 1, '|');
ret.set(left.width + GAP / 2, 2, line[i]);
return ret;
}
}
PictureStruct c = parseFactor(a, b);
ret.width = c.width;
ret.paint(0, 2, c);
int xx = c.getFirstLineX();
ret.set(xx, 0, 'T');
ret.set(xx, 1, '|');
return ret;
}
PictureStruct parseFactor(int a, int b)
{
PictureStruct ret;
if (line[a] == '(' && line[b - 1] == ')')
{
PictureStruct c = parseExpr(a + 1, b - 1);
ret.width = c.width + GAP + 1;
ret.paint((GAP + 1) / 2, 2, c);
int xx = c.getFirstLineX() + (GAP + 1) / 2;
for (int k = ret.width; --k >= 0;)
{
ret.set(k, 1, '=');
}
ret.set(xx, 0, 'F');
ret.set(xx, 1, '|');
ret.set(0, 2, '(');
ret.set(ret.width - 1, 2, ')');
return ret;
}
//assert(b==a+1 && line[a]=='i');
ret.width = 1;
ret.set(0, 0, 'F');
ret.set(0, 1, '|');
ret.set(0, 2, 'i');
return ret;
}
int main()
{
int c = 0;
while (gets(line))
{
//assert(strspn(line,"+-*/()i")==strlen(line)); //yes
if (c++)
{
putchar('\n');
}
parseExpr(0, strlen(line)).print();
}
return 0;
}
/*@END_OF_SOURCE_CODE*/