控制台打印表格 (print tables in console application)


/ Published in: C
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /** @file
  2.  * @brief 控制台打印表格 (print tables in console application)
  3.  * Project Name:none
  4.  * <br>
  5.  * Module Name:none
  6.  * <br>
  7.  * @author [email protected]
  8.  * @date 2009/04/01
  9.  * @version 0.1
  10.  * @note none
  11.  */
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15.  
  16. #define HEAD 0
  17. #define MIDDLE 1
  18. #define TAIL 2
  19.  
  20. /**
  21.  * @brief 控制台打印表格 (print tables in console application)
  22.  * @param[in] location 表格线的位置, 0 表头线,1 表中线,2 表尾线
  23.  * @param[in] col_count 表格的列数
  24.  * @param[in] col__widthes 表格的每列的宽度
  25.  * @return 成功返回0,失败返回错误代码
  26.  * @note æ— 
  27.  * @remarks æ— 
  28.  */
  29. int print_table_line(int location, int col_count, int *col__widthes)
  30. {
  31. //注意:这些是中文符号,所以要用3个字符装(包括\0)
  32. char line_head[][3] = {"┌", "├", "└"};
  33. char line_mid1[][3] = {"─", "─", "─"};
  34. char line_mid2[][3] = {"┬", "┼", "┴"};
  35. char line_tail[][3] = {"┐", "┤", "┘"};
  36.  
  37. int i = 0;
  38.  
  39. printf("%s",line_head[location]); //行首
  40.  
  41. for (i = 0; i < col_count; i++) {
  42. int j = 0;
  43. for (j = 0; j < col__widthes[i] / 2; j++) {
  44. printf("%s",line_mid1[location]);
  45. }
  46.  
  47. if (i < col_count - 1)
  48. printf("%s", line_mid2[location]);
  49. }
  50.  
  51. printf("%s\n", line_tail[location]);//行尾
  52.  
  53. return 0;
  54. }
  55.  
  56.  
  57. /** @brief 得到一个整数的宽度 */
  58. static int get_integer_width(void *e)
  59. {
  60. int temp = *(int*)e;
  61. int width = 0;
  62. while (temp)
  63. {
  64. temp /= 10;
  65. width++;
  66. }
  67. return width;
  68. }
  69.  
  70.  
  71. /**
  72.  * @brief 将二维数组打印成表格样式
  73.  * @param[in] a 二维数组名
  74.  * @param[in] row_count 行数
  75.  * @param[in] col_count 列数
  76.  * @return 成功返回0,失败返回错误代码
  77.  * @note æ— 
  78.  * @remarks æ— 
  79.  */
  80. int print_array(int* a, int row_count, int col_count, int(*get_elem_width)(void *e))
  81. {
  82. int i = 0, j = 0;
  83. int *col_width = (int*)malloc(col_count * sizeof(int));
  84. if(NULL == col_width)
  85. {
  86. return -1;
  87. }
  88. memset(col_width, 0, col_count * sizeof(int));
  89.  
  90. // get max width of each column
  91. for(i = 0; i < col_count; i++){
  92. for(j = 0; j < row_count; j++) {
  93. int temp_width = get_elem_width(&a[j * row_count + i]);
  94. col_width[i] = col_width[i] > temp_width ? col_width[i] : temp_width;
  95. }
  96. }
  97.  
  98. for(i = 0; i < col_count; i++){
  99. if(col_width[i] % 2 != 0) {
  100. col_width[i]++; // 如果宽度为奇数,则增1,变成偶数
  101. }
  102. }
  103.  
  104. //打印表头线
  105. print_table_line(HEAD, col_count, col_width);
  106.  
  107. //打印表格内容
  108. for (i = 0; i < row_count; i++)
  109. {
  110. if (i > 0) print_table_line(MIDDLE, col_count, col_width); //打印表中线
  111. printf("│"); //行首
  112. for (j = 0; j < col_count; j++)
  113. {
  114. int k = 0;
  115. int elem_width = get_elem_width(&a[i * col_count + j]);
  116. int space_count = col_width[j] - elem_width;
  117.  
  118. // 居中打印元素
  119. for(k = 0; k < space_count / 2; k++) {
  120. printf(" ");
  121. }
  122.  
  123. printf("%d",a[i * col_count + j]);
  124.  
  125. for(k = 0; k < space_count / 2; k++) {
  126. printf(" ");
  127. }
  128. if(0 != space_count % 2 ) {
  129. printf(" ");
  130. }
  131.  
  132.  
  133. if (j < col_count - 1 )
  134. printf("│"); // 表中竖线
  135. }
  136. printf("│\n"); //行尾
  137. }
  138.  
  139. print_table_line(TAIL, col_count, col_width); //打印表尾
  140.  
  141. free(col_width);
  142.  
  143. return 0;
  144. }
  145.  
  146.  
  147. int main()
  148. {
  149. int A[][3] = {{1, 2000, 3}, {4, 5, 35}, {123456789, 8, 9}};
  150. print_array(&A[0][0], 3, 3, get_integer_width);
  151. return 0;
  152. }

URL: http://www.yanjiuyanjiu.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.