-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraining8_1.cpp
More file actions
342 lines (298 loc) · 8.02 KB
/
training8_1.cpp
File metadata and controls
342 lines (298 loc) · 8.02 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
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
#include "utils.h"
#include<iostream>
#include<fstream>
#include<string>
class Table;
class Cell
{
protected:
int x, y;
Table *table;
string data;
public:
virtual string stringify();
virtual int to_numeric();
Cell(string data, int x, int y, Table *table);
};
Cell::Cell(string data, int x, int y, Table *table)
: data(data), x(x), y(y), table(table) {}
string Cell::stringify() { return data; }
int Cell::to_numeric() { return 0; }
class Table
{
protected:
// 행 및 열의 최대 크기
int max_row_size, max_col_size;
// 데이터를 보관하는 테이블
// Cell* 을 보관하는 2차원 배열이라 생각하면 편하다
Cell ***data_table;
public:
Table(int max_row_size, int max_col_size);
~Table();
// 새로운 셀을 row 행 col 열에 등록한다.
void reg_cell(Cell *c, int row, int col);
// 해당 셀의 정수값을 반환한다.
// s : 셀 이름 (Ex. A3, B6 과 같이)
int to_numeric(const string &s);
// 행 및 열 번호로 셀을 호출한다.
int to_numeric(int row, int col);
// 해당 셀의 문자열을 반환한다.
string stringify(const string &s);
string stringify(int row, int col);
virtual string print_table() = 0;
};
Table::Table(int max_row_size, int max_col_size)
: max_row_size(max_row_size), max_col_size(max_col_size)
{
data_table = new Cell **[max_row_size];
for (int i = 0; i < max_row_size; i++)
{
data_table[i] = new Cell *[max_col_size];
for (int j = 0; j < max_col_size; j++)
{
data_table[i][j] = NULL;
}
}
}
Table::~Table()
{
for (int i = 0; i < max_row_size; i++)
{
for (int j = 0; j < max_col_size; j++)
{
if (data_table[i][j])
delete data_table[i][j];
}
}
for (int i = 0; i < max_row_size; i++)
{
delete[] data_table[i];
}
delete[] data_table;
}
void Table::reg_cell(Cell *c, int row, int col)
{
if (!(row < max_row_size && col < max_col_size))
return;
if (data_table[row][col])
{
delete data_table[row][col];
}
data_table[row][col] = c;
}
int Table::to_numeric(const string &s)
{
// Cell 이름으로 받는다.
int col = s[0] - 'A';
int row = atoi(s.c_str() + 1) - 1;
if (row < max_row_size && col < max_col_size)
{
if (data_table[row][col])
{
return data_table[row][col]->to_numeric();
}
}
return 0;
}
int Table::to_numeric(int row, int col)
{
if (row < max_row_size && col < max_col_size && data_table[row][col])
{
return data_table[row][col]->to_numeric();
}
return 0;
}
string Table::stringify(const string &s)
{
// Cell 이름으로 받는다.
int col = s[0] - 'A';
int row = atoi(s.c_str() + 1) - 1;
if (row < max_row_size && col < max_col_size)
{
if (data_table[row][col])
{
return data_table[row][col]->stringify();
}
}
return 0;
}
string Table::stringify(int row, int col)
{
if (row < max_row_size && col < max_col_size && data_table[row][col])
{
return data_table[row][col]->stringify();
}
return "";
}
std::ostream &operator<<(std::ostream &o, Table &table)
{
o << table.print_table();
return o;
}
class TxtTable : public Table
{
string repeat_char(int n, char c);
// 숫자로 된 열 번호를 A, B, .... Z, AA, AB, ... 이런 순으로 매겨준다.
string col_num_to_str(int n);
public:
TxtTable(int row, int col);
// 텍스트로 표를 깨끗하게 출력해준다.
string print_table();
};
TxtTable::TxtTable(int row, int col) : Table(row, col) {}
// 텍스트로 표를 깨끗하게 출력해준다.
string TxtTable::print_table()
{
string total_table;
int *col_max_wide = new int[max_col_size];
for (int i = 0; i < max_col_size; i++)
{
unsigned int max_wide = 2;
for (int j = 0; j < max_row_size; j++)
{
if (data_table[j][i] &&
data_table[j][i]->stringify().length() > max_wide)
{
max_wide = data_table[j][i]->stringify().length();
}
}
col_max_wide[i] = max_wide;
}
// 맨 상단에 열 정보 표시
total_table += " ";
int total_wide = 4;
for (int i = 0; i < max_col_size; i++)
{
if (col_max_wide[i])
{
int max_len = std::max(2, col_max_wide[i]);
total_table += " | " + col_num_to_str(i);
total_table += repeat_char(max_len - col_num_to_str(i).length(), ' ');
total_wide += (max_len + 3);
}
}
total_table += "\n";
// 일단 기본적으로 최대 9999 번째 행 까지 지원한다고 생각한다.
for (int i = 0; i < max_row_size; i++)
{
total_table += repeat_char(total_wide, '-');
total_table += "\n" + std::to_string(i + 1);
total_table += repeat_char(4 - std::to_string(i + 1).length(), ' ');
for (int j = 0; j < max_col_size; j++)
{
if (col_max_wide[j])
{
int max_len = std::max(2, col_max_wide[j]);
string s = "";
if (data_table[i][j])
{
s = data_table[i][j]->stringify();
}
total_table += " | " + s;
total_table += repeat_char(max_len - s.length(), ' ');
}
}
total_table += "\n";
}
return total_table;
}
string TxtTable::repeat_char(int n, char c)
{
string s = "";
for (int i = 0; i < n; i++)
s.push_back(c);
return s;
}
// 숫자로 된 열 번호를 A, B, .... Z, AA, AB, ... 이런 순으로 매겨준다.
string TxtTable::col_num_to_str(int n)
{
string s = "";
if (n < 26)
{
s.push_back('A' + n);
}
else
{
char first = 'A' + n / 26 - 1;
char second = 'A' + n % 26;
s.push_back(first);
s.push_back(second);
}
return s;
}
class HtmlTable : public Table
{
public:
HtmlTable(int row, int col);
string print_table();
};
class CSVTable : public Table
{
public:
CSVTable(int row, int col);
string print_table();
};
HtmlTable::HtmlTable(int row, int col) : Table(row, col) {}
string HtmlTable::print_table()
{
string s = "<table border='1' cellpadding='10'>";
for (int i = 0; i < max_row_size; i++)
{
s += "<tr>";
for (int j = 0; j < max_col_size; j++)
{
s += "<td>";
if (data_table[i][j])
s += data_table[i][j]->stringify();
s += "</td>";
}
s += "</tr>";
}
s += "</table>";
return s;
}
CSVTable::CSVTable(int row, int col) : Table(row, col) {}
string CSVTable::print_table()
{
string s = "";
for (int i = 0; i < max_row_size; i++)
{
for (int j = 0; j < max_col_size; j++)
{
if (j >= 1)
s += ",";
// CSV 파일 규칙에 따라 문자열에 큰따옴표가 포함되어 있다면 "" 로
// 치환하다.
string temp;
if (data_table[i][j])
temp = data_table[i][j]->stringify();
for (int k = 0; k < temp.length(); k++)
{
if (temp[k] == '"')
{
// k 의 위치에 " 를 한 개 더 집어넣는다.
temp.insert(k, 1, '"');
// 이미 추가된 " 를 다시 확인하는 일이 없게 하기 위해
// k 를 한 칸 더 이동시킨다.
k++;
}
}
temp = '"' + temp + '"';
s += temp;
}
s += '\n';
}
return s;
}
// 생략
int main()
{
TxtTable table(5, 5);
std::ofstream out("test.txt");
table.reg_cell(new Cell("Hello~", 0, 0, &table), 0, 0);
table.reg_cell(new Cell("C++", 0, 1, &table), 0, 1);
table.reg_cell(new Cell("Programming", 1, 1, &table), 1, 1);
std::cout << std::endl
<< table;
out << table;
}