您的位置:首页 > 其它

控制台输出表格示例

2009-03-02 23:25 337 查看
在控制台输出表格主要使用表格线字符来完成,如何对齐表格线对学生来说是一个挑战。表格线有中文(unicode)和英文(ascii)的,下文中使用的是中文表格字符,在VC6上调试通过

#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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: