控制台输出表格示例

#include <iostream.h> 
//////////////////////////////////////// 
// 功能:打印表格线 
// 参数: 
// location 表格线的位置, 0 表头线,1 表中线,2 表尾线 
// colCount 表格的列数 
// colWidth 表格的列宽 
///////////////////////////////////////// 
void PrintTableLine(int location, int colCount, int colWidth) 

 //注意:这些是中文符号,所以要用3个字符装(包括/0) 
 char lineHead[][3] = {"┌", "├", "└"}; 
 char lineMid1[][3] = {"─", "─", "─"}; 
 char lineMid2[][3] = {"┬", "┼", "┴"}; 
 char lineTail[][3] = {"┐", "┤", "┘"}; 
 cout << lineHead[location]; //行首 
 for (int i = 0; i < colCount; i++) 
 { 
  for (int j = 0; j < colWidth/2; j++) 
  { 
   cout << lineMid1[location]; 
  } 
  if (i < colCount - 1) cout << lineMid2[location]; 
 } 
 cout << lineTail[location] << endl; //行尾 

///////////////////////////////////////////////////////// 
// 功能:获取指定二维数组中的最大显示宽度 
// 参数:A 二维数组名,rowCount 行数,colCount 列数 
///////////////////////////////////////////////////////// 
int GetMaxWidth(int* A, int rowCount, int colCount) 

 int width = 0; 
 for (int i = 0; i < rowCount; i++) 
 { 
  for (int j = 0; j < colCount; j++) 
  { 
   int c = 1; 
   int temp = A[i*rowCount+j]; 
   while (temp) 
   { 
    temp /= 10; 
    c++; 
   } 
   width = width < c ? c : width; 
  } 
 } 
 return width; 

/////////////////////////////////////// 
// 功能:将二维数组打印成表格样式 
// 参数:A 二维数组名,rowCount 行数,colCount 列数 
////////////////////////////////////// 
void PrintArray(int* A, int rowCount, int colCount) 

 char tablines[] = {"┌┬┐├┼┤└┴┘─│"}; 
 int i, j, colWidth; 
 
 colWidth = GetMaxWidth(A, rowCount, colCount); //获取所有数据中的最大宽度 
 //打印表头线 
 PrintTableLine(0, colCount, colWidth); 
 
 //打印表格内容 
 for (i = 0; i < rowCount; i++) 
 { 
  if (i > 0) PrintTableLine(1, colCount, colWidth); //打印表中线 
  cout << "│";  //行首 
  for (j = 0; j < colCount; j++) 
  { 
   cout.width(colWidth);       //内容按指定宽度输出 
   cout << A[i*rowCount+j];  
   if (j < colCount - 1 ) cout << "│"; // 表中竖线 
  } 
  cout << "│" << endl;    //行尾 
 } 
 
 PrintTableLine(2, colCount, colWidth); //打印表尾 

void main() 

 int A[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; 
 PrintArray(&A[0][0], 3, 3); 
 } 

http://blog.youkuaiyun.com/yycec/archive/2009/03/02/3951112.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值