/*				Starting Number 20
			Tasks Solved 1,2,3,4,5

			Only Standard Librarys:
		 <stdio.h>
		 <stdlib.h>
		 <string.h>
		 <conio.h> , are included


*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>


int maxline=80;
FILE *inf,*outf;

struct headpole{
	char *data;
	long l,r,u,d;
	long hornas,vernas;//naslednici
	long parent;
	int  align;
	int valign;
	int colspan;
	int rowspan;
	int h;//1 th 0-td
};
typedef struct headpole headtype;

char *reserved[]={"<tr>","<th","<td","=left","=right","=center","align=",
//		    		0	  1		 2		3			4	    5		6
"colspan","rowspan","valign","=top","=bottom","=middle"};
//  7	       8		9     	10		11		12
int longres=13;

int brojcols=0;//number of cols

char inlin[1000];
headtype *header[300];
int  collong[300];



int headerc=0;
int coltek=0;

int notfound(char *in);
int enter(char *inpu);
int polemake(int parent,int h);




int enter(char *inpu){
  long cur=0;
  char tek;
  *inpu=0;
  while((!feof(inf)) && notfound(inpu)<0 ){
    fscanf(inf,"%c",&tek);
	if(tek==' '||tek=='\n')if(inpu[cur-1]==' '||!cur||inpu[0]=='<')continue;else {inpu[cur++]=' ';continue;}
	if(tek=='<'&&cur){fseek(inf,-1,SEEK_CUR);break;}
	inpu[cur++]=tek;inpu[cur]=0;
	if(tek=='>')break;
  }
  inpu[cur]=0;
  int ret=notfound(inpu);
  if(ret==6)fseek(inf,-1,SEEK_CUR);
  return ret;
}

int notfound(char *in){
  int i;
  strlwr(in);
  for(i=0;i<longres;i++){
	if(!strcmp(in,reserved[i]))return i;
  }
  return -1;
}


void main(int argv,char *argc[]){
  int beg=1,br=0;
  clrscr();
  if(argv<3){exit(1);}
  if(argv==4){maxline=atoi(argc[1]+1);beg++;}

  inf=fopen(argc[beg],"rt");
  outf=fopen(argc[beg+1],"wt");
  if(inf==NULL||outf==NULL){printf("Error while opening files!");exit(2);}

  for(int i=0;i<300;i++)collong[i]=0;


  int bef=-2;
  while(!feof(inf)){
//    printf("%d   %s\n",enter(inlin),inlin);
	if(bef==0)coltek=0;
	bef=polemake(0,bef);
	coltek++;
  };
  brojcols=coltek;



  int j,k;
  long  total=0;
  headtype *ret;

  for(i=0;i<brojcols;i++)total+=collong[i]+3;
  total--;

  for(i=0;i<2*headerc/brojcols+1;i++){
	if(i%2==0){
	  if(!i)fprintf(outf,"+");else fprintf(outf,"|");
	  for(j=0;j<total;j++)fprintf(outf,"-");
	  if(i+2>=2*headerc/brojcols+1)  fprintf(outf,"+");else fprintf(outf,"|");
	  fprintf(outf,"\n");
	}
	else{
	  for(j=0;j<brojcols;j++){
		fprintf(outf,"| ");
		ret=header[i/2*brojcols+j];
		switch(ret->align){
		  default:
		  case 3://left
				fprintf(outf,"%s",ret->data);
				for(k=0;k<collong[j]-strlen(ret->data)+1;k++)fprintf(outf," ");

		  break;
		  case 4://right
				for(k=0;k<collong[j]-strlen(ret->data);k++)fprintf(outf," ");
				fprintf(outf,"%s",ret->data);
				fprintf(outf," ");
		  break;
		  case 5://center
//				fprintf(outf," ");
				for(k=0;k<(collong[j]-strlen(ret->data))/2;k++)fprintf(outf," ");
				fprintf(outf,"%s",ret->data);
				for(k=0;k<(collong[j]-strlen(ret->data)+1)/2;k++)fprintf(outf," ");
				fprintf(outf," ");
		  break;

		}

	  }


	  fprintf(outf,"|");
	  fprintf(outf,"\n");
	}

  }



/*
  for(i=0;i<headrec/brojcols;i++){
	for(j=0;j<brojcols;j++){


	}

  }*/










}

int polemake(int parent,int h){
  int tmp;
  headpole *ret;
  ret = new headpole;if(ret==NULL){printf("Not Enough Memory");exit(10);}
  header[headerc++]=ret;

  if(h!=-2){tmp=h;}
  else tmp=enter(inlin);

  if(tmp==0)tmp=enter(inlin);

  if(tmp==1){ret->h=1;}else ret->h=0;
  if(tmp!=1&&tmp!=2){printf("Error in table format");exit(4);}

  if(ret->h)ret->align=5;else ret->align=3;

  tmp=enter(inlin);
  while(inlin[0]!='>'){
	if(tmp==6){
	  tmp=enter(inlin);
	  ret->align=tmp;
	}

	tmp=enter(inlin);
  };

  tmp=enter(inlin);
  ret->data= new char[1000];if(ret->data==NULL){printf("Not Enough Memory");exit(11);}
  *ret->data=0;
  do{
	strcpy(&ret->data[strlen(ret->data)],inlin);
	tmp=enter(inlin);
  }while(tmp==-1&&!feof(inf));
  if(ret->data[strlen(ret->data)-1]==' ')ret->data[strlen(ret->data)-1]=0;

  if(strlen(ret->data)>collong[coltek])collong[coltek]=strlen(ret->data);

  return tmp;
}