#include<iostream.h>
#include<string.h>
struct TableCell{
char content[128];
int len;
};
int row, col;
int colWidth[128];
int tableWidth;
char header[128];
char nextHeader[128];
bool endFile;
TableCell tableCells[24][128];
//读入一个表格.
void ReadTable();
//输出一个表格.
void PrintTable();
int main()
{
endFile=false;
header[0]='\0';
while(!endFile){
ReadTable();
PrintTable();
}
return 0;
}
void ReadTable()
{
char line[128];
int len;
int i, j;
row=0;
for(i=0;i<128;i++){
colWidth[i]=0;
}
if(header[0]='\0'){
cin.getline(header, 128);
}
else{
strcpy(header,nextHeader);
}
while(true){
cin.getline(line,128);
if(line[0]=='*'){
endFile=true;
break;
}
if(line[0]=='<'||line[0]=='='||line[0]=='>'){
strcpy(nextHeader,line);
break;
}
col=0;
i=0;
len=strlen(line);
while(i<len){
j=0;
while(line[j]!='&'&&i<len){
tableCells[row][col].content[j++]=line[i++];
}
tableCells[row][col].content[j]='\0';
tableCells[row][col].len=strlen(tableCells[row][col].content);
if(colWidth[col]<tableCells[row][col].len){
colWidth[col]=tableCells[row][col].len;
}
i++;
col++;
}
row++;
}
tableWidth=0;
for(i=0;i<col;i++){
tableWidth+=colWidth[i];
}
tableWidth+=3*col-1;
}
void PrintTable()
{
int nspace;
int i, j, k;
cout<<'@';
for(i=0;i<tableWidth;i++){
cout<<'-';
}
cout<<'@'<<endl;
for(i=0;i<row;i++){
cout<<"|";
for(j=0;j<col;j++){
if(j!=0){
cout<<' ';
}
if(header[j]=='<'){
cout<<tableCells[i][j].content;
nspace=colWidth[j]-tableCells[i][j].len;
for(k=0;k<nspace;k++){
cout<<' ';
}
cout<<' ';
}
else if(header[j]=='='){
nspace=(colWidth[j]-tableCells[i][j].len)/2;
for(k=0;k<nspace;k++){
cout<<' ';
}
cout<<tableCells[i][j].content;
if((colWidth[j]-tableCells[i][j].len)%2==1){
nspace++;
}
for(k=0;k<nspace;k++){
cout<<' ';
}
cout<<endl;
}
else{
nspace=colWidth[j]-tableCells[i][j].len;
for(k=0;k<nspace;k++){
cout<<' ';
}
cout<<tableCells[i][j].content;
cout<<' ';
}
if(j!=col-1){
cout<<'|';
}
}
cout<<'|';
cout<<endl;
if(i==0){
cout<<'|';
for(j=0;j<col;j++){
for(k=0;k<colWidth[j]+2;k++){
cout<<'-';
}
if(j!=col-1){
cout<<'+';
}
}
cout<<'|'<<endl;
}
}
cout<<'@';
for(i=0;i<tableWidth;i++){
cout<<'-';
}
cout<<'@'<<endl;
}
poj 1578.Instruens Fabulam
最新推荐文章于 2021-01-13 15:33:34 发布