-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path1441Table.cpp
More file actions
153 lines (147 loc) · 3.88 KB
/
1441Table.cpp
File metadata and controls
153 lines (147 loc) · 3.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include<iostream>
#include<stdio.h>
#include<vector>
#include<string>
#include<sstream>
#include<string.h>
using namespace std;
typedef basic_string<unsigned char> ustring;
//if contain '─' not a text line
unsigned char ccc[]={
179, 196, // '│' '─'
218, 194, 191, // '┌' '┬' '┐'
195, 197, 180, // '├' '┼' '┤'
192, 193, 217, // '└' '┴' '┘'
};
int col;
unsigned char line[300];
inline int ustrlen(unsigned char *s){
int len = 0;
while(s[len]!=0) len++;
return len;
}
vector<vector<vector<unsigned char *> > > table;
unsigned char* trim(unsigned char* token){
int len;
for(len=0;token[len]!=0;len++){}
int l=0,r=len-1;
while(l<len && token[l]==' ') l++;
while(r>=0 && token[r]==' ') r--;
if(l>r) {
unsigned char *ret = (unsigned char *)malloc(1*sizeof(unsigned char));
ret[0] = 0;
return ret;
}
unsigned char *ret = (unsigned char*)malloc((r-1+2)*sizeof(unsigned char));
for(int i=0;i<=r-1+1;i++)
ret[i] = token[l+i];
ret[r-l+1] = 0;
return ret;
}
inline void print_cell(int i,int j,int k,int w){
if(table[i][j].size()>k){
cout<<" ";
int len = ustrlen(table[i][j][k]);
for(int l=0;l<len;l++) cout<<table[i][j][k][l];
w-=len+1;
}
for(int l=0;l<w;l++)
cout<<" ";
return;
}
int main()
{
unsigned char c;
col = 0;
while(cin>>std::noskipws>>c,c!=10){
if(c==194) col++;
}
col+=1;
vector<vector<unsigned char*> > row;
while(cin>>std::noskipws>>c){
line[0] = c;
int lc = 0;
int sz = 1;
while(cin>>std::noskipws>>c) {
if(c==10) break;
line[sz++] = c;
}
bool bad = false;
for(int i=0;i<sz;i++)
if(line[i]==196) {
bad = true;
break;
}
if(bad) {
table.push_back(row);
row.clear();
}else{
unsigned char token[300];
int t_sz=0;
int cnt = 0;
for(int i=1;i<sz;i++){
if(line[i]==179){
if(cnt==row.size()) row.push_back(vector<unsigned char*>());
token[t_sz] = 0;
row[cnt].push_back(trim(token));
t_sz = 0;
cnt++;
}else token[t_sz++]=line[i];
}
}
}
//calculate the width
vector<int> width(col,0);
//ustreln+2
for(int i=0;i<table.size();i++)
for(int j=0;j<table[i].size();j++)
for(int k=0;k<table[i][j].size();k++){
width[j]=max(width[j],ustrlen(table[i][j][k])+2);
}
//for(int i=0;i<col;i++)
// cout<<width[i]<<endl;
//begin to print
//header line
cout<<ccc[2];
for(int i=0;i<col;i++)
{
for(int j=0;j<width[i];j++)
cout<<ccc[1];
if(i==col-1)cout<<ccc[4]<<endl;
else cout<<ccc[3];
}
for(int i=0;i<table.size();i++){
//how many line this row need
int ln = 0;
for(int j=0;j<table[i].size();j++)
ln = max(ln,(int)table[i][j].size());
for(int l=0;l<ln;l++){
cout<<ccc[0];
for(int j=0;j<table[i].size();j++){
print_cell(i,j,l,width[j]);
cout<<ccc[0];
}
cout<<endl;
}
if(i!=table.size()-1){
//print middle line 5 6 7
cout<<ccc[5];
for(int j=0;j<col;j++)
{
for(int k=0;k<width[j];k++)
cout<<ccc[1];
if(j==col-1)cout<<ccc[7]<<endl;
else cout<<ccc[6];
}
}
}
cout<<ccc[8];
for(int i=0;i<col;i++)
{
for(int j=0;j<width[i];j++)
cout<<ccc[1];
if(i==col-1)cout<<ccc[10];
else cout<<ccc[9];
}
return 0;
}