• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

一个画中文表格的类

发布: 2007-6-08 22:43 | 作者: seanhe | 来源: | 查看: 51次 | 进入软件测试论坛讨论

领测软件测试网 老是要设计报表,干脆写个类,自动替我考虑表格线。

#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;
}

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网