控制台输出表格示例
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); }
相关文章推荐
- 控制台输出表格示例
- MyBatis3用log4j在控制台输出SQL的方法示例
- java控制台输出百分比进度条示例
- C语言实现控制台输出表格样式格式化
- 程序设计之C#控制台输出日历示例
- MySql数据库查询结果用表格输出PHP代码示例
- 自定义输出表格,有时用得到[WEB学习示例]
- java控制台输出数字金字塔示例分享
- C#连接数据库在控制台输出结果集(示例)
- 如何把ResultSet的结果集,一表格的形式显示,或以表格的形式输出到控制台!
- MySql数据库查询结果用表格输出PHP代码示例
- C#控制台基础 全角与半角如何切换 全角半角输出的区别
- Java基础知识强化之集合框架笔记36:List练习之键盘录入多个数据在控制台输出最大值
- Log4j 2 同时输出日志到控制台和文件
- 全文检索输出统计表格
- Chrome 控制台新玩法-向输出到console的文字加样式
- MinGW eclipse中运行c++控制台没输出
- 动态输出一个任意行列的表格
- java控制台输出一个菱形
- 在控制台中输出 出现SIGBAT或者EXC_BAD_ACCESS的原因的方法