#include <iostream>
#include <string>
#include <list>
namespace std {} using namespace std;
struct s_frame
{
string left_upper_conner;
string right_upper_conner;
string left_bottom_conner;
string right_bottom_conner;
string vertical_bar;
string horizontal_bar;
string cross;
string up_t_bar;
string down_t_bar;
string left_t_bar;
string right_t_bar;
};
class report
{
private:
string header;
string footer;
string date;
string institute;
s_frame frame_for_report;
list <int> columndef;
list < list <string> > row;
void setFrame();
public:
report();
report(s_frame f);
~report();
void setColumn(list <int> col);
string getHeader();
void setHeader(string h);
void setDate(string rdate);
void setInstitute(string inst);
string mkUpperFrame();
string mkBottomFrame();
string mkMiddleFrame();
void addRow(list <string> content);
string mkRow( list <string> field);
string genReport();
int tableWidth();
};
-------------------------------------------------------------------------------------------------------------------------
#include "report.h"
report::report()
{
header="代收明细报表";
footer="制表人:科技部";
setFrame();
list <int> col;
col.push_back(10);
col.push_back(20);
col.push_back(10);
col.push_back(16);
col.push_back(16);
col.push_back(16);
setColumn(col);
}
report::~report()
void report::setFrame()
{
frame_for_report.left_upper_conner="┌";
frame_for_report.right_upper_conner="┐";
frame_for_report.left_bottom_conner="└";
frame_for_report.right_bottom_conner="┘";
frame_for_report.vertical_bar="│";
frame_for_report.horizontal_bar="─";
frame_for_report.cross="┼";
frame_for_report.up_t_bar="┬";
frame_for_report.down_t_bar="┴";
frame_for_report.left_t_bar="├";
frame_for_report.right_t_bar="┤";
}
void report::setColumn( list <int> col)
{
columndef=col;
}
string report::getHeader()
{
return header;
}
void report::setHeader(string h)
{
header=h;
}
void report::setDate(string rdate)
{
date=rdate;
}
void report::setInstitute(string inst)
{
institute=inst;
}
string report::mkUpperFrame()
{
string frame;
list<int>::iterator columnIterator;
frame+=frame_for_report.left_upper_conner;
for (columnIterator=columndef.begin(); columnIterator!=columndef.end(); ++columnIterator)
{
for(int i=0;i<(*columnIterator)/2;i++)
frame+=frame_for_report.horizontal_bar;
frame+=frame_for_report.up_t_bar;
}
frame.erase(frame.length() -2 ,2);
frame+=frame_for_report.right_upper_conner;
return frame;
}
string report::mkBottomFrame()
{
string frame;
list<int>::iterator columnIterator;
frame+=frame_for_report.left_bottom_conner;
for (columnIterator=columndef.begin(); columnIterator!=columndef.end(); ++columnIterator)
{
for(int i=0;i<(*columnIterator)/2;i++)
frame+=frame_for_report.horizontal_bar;
frame+=frame_for_report.down_t_bar;
}
frame.erase(frame.length() -2 ,2);
frame+=frame_for_report.right_bottom_conner;
return frame;
}
string report::mkMiddleFrame()
{
string frame;
list<int>::iterator columnIterator;
frame+=frame_for_report.left_t_bar;
for (columnIterator=columndef.begin(); columnIterator!=columndef.end(); ++columnIterator)
{
for(int i=0;i<(*columnIterator)/2;i++)
frame+=frame_for_report.horizontal_bar;
frame+=frame_for_report.cross;
}
frame.erase(frame.length() -2 ,2);
frame+=frame_for_report.right_t_bar;
return frame;
}
string report::mkRow( list <string> field)
{
list<string>::iterator fieldIter;
list<int>::iterator columnIterator;
string content;
for(fieldIter=field.begin(), columnIterator=columndef.begin();
fieldIter!=field.end() && columnIterator!=columndef.end();
++fieldIter,++columnIterator)
{
content += frame_for_report.vertical_bar;
content += string(*columnIterator > (*fieldIter).length() ? *columnIterator - (*fieldIter).length():0,' ');
content += *fieldIter;
}
content += frame_for_report.vertical_bar;
return content;
}
string report::genReport()
{
list< list <string> >::iterator rowIter;
list<string>::iterator fieldIter;
string report;
report += string((tableWidth() - header.length())/2,' ');
report += header;
report += '\n';
report += institute;
report += string(tableWidth() - 10 - institute.length() ,' ');
report += date;
report += '\n';
report += mkUpperFrame();
report += '\n';
for(rowIter=row.begin(); rowIter!=row.end();++rowIter)
{
report += mkRow( *rowIter );
report += '\n';
report += mkMiddleFrame();
report += '\n';
}
report.erase(report.length() - mkMiddleFrame().length() - 1 ,mkMiddleFrame().length()+1);
report+=mkBottomFrame();
report+='\n';
return report;
}
void report::addRow(list <string> content)
{
list <string> t;
t=content;
row.push_back(t);
}
int report::tableWidth()
{
list<int>::iterator columnIterator;
int width=0;
for (columnIterator=columndef.begin(); columnIterator!=columndef.end(); ++columnIterator)
{
width+=*columnIterator;
width+=2;
}
return width;
}