全部博文(116)
分类:
2005-08-30 10:26:10
#include
#include
#include
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
list < list
void setFrame();
public:
report();
report(s_frame f);
~report();
void setColumn(list
string getHeader();
void setHeader(string h);
void setDate(string rdate);
void setInstitute(string inst);
string mkUpperFrame();
string mkBottomFrame();
string mkMiddleFrame();
void addRow(list
string mkRow( list
string genReport();
int tableWidth();
};
-------------------------------------------------------------------------------------------------------------------------
#include "report.h"
report::report()
{
header="代收明细报表";
footer="制表人:科技部";
setFrame();
list
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
{
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
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
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
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
{
list
list
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
list
string report;
report += string((tableWidth() - header.length())/2,' ');
report += header;
report += '
';
report += institute;
report += string(tableWidth() - 10 - institute.length() ,' ');
report += date;
report += '
';
report += mkUpperFrame();
report += '
';
for(rowIter=row.begin(); rowIter!=row.end();++rowIter)
{
report += mkRow( *rowIter );
report += '
';
report += mkMiddleFrame();
report += '
';
}
report.erase(report.length() - mkMiddleFrame().length() - 1 ,mkMiddleFrame().length()+1);
report+=mkBottomFrame();
report+='
';
return report;
}
void report::addRow(list
{
list
t=content;
row.push_back(t);
}
int report::tableWidth()
{
list
int width=0;
for (columnIterator=columndef.begin(); columnIterator!=columndef.end(); ++columnIterator)
{
width+=*columnIterator;
width+=2;
}
return width;
}