/ Published in: C
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** @file * @brief 控制å°æ‰“å°è¡¨æ ¼ (print tables in console application) * Project Name:none * <br> * Module Name:none * <br> * @author [email protected] * @date 2009/04/01 * @version 0.1 * @note none */ #include <stdio.h> #include <stdlib.h> #include <string.h> #define HEAD 0 #define MIDDLE 1 #define TAIL 2 /** * @brief 控制å°æ‰“å°è¡¨æ ¼ (print tables in console application) * @param[in] location è¡¨æ ¼çº¿çš„ä½ç½®ï¼Œ 0 表头线,1 表ä¸çº¿ï¼Œ2 表尾线 * @param[in] col_count è¡¨æ ¼çš„åˆ—æ•° * @param[in] col__widthes è¡¨æ ¼çš„æ¯åˆ—的宽度 * @return æˆåŠŸè¿”回0,失败返回错误代ç * @note æ— * @remarks æ— */ int print_table_line(int location, int col_count, int *col__widthes) { //注æ„:这些是ä¸æ–‡ç¬¦å·ï¼Œæ‰€ä»¥è¦ç”¨3个å—符装(包括\0) char line_head[][3] = {"┌", "├", "â””"}; char line_mid1[][3] = {"─", "─", "─"}; char line_mid2[][3] = {"┬", "┼", "â”´"}; char line_tail[][3] = {"â”", "┤", "┘"}; int i = 0; for (i = 0; i < col_count; i++) { int j = 0; for (j = 0; j < col__widthes[i] / 2; j++) { } if (i < col_count - 1) } return 0; } /** @brief 得到一个整数的宽度 */ static int get_integer_width(void *e) { int temp = *(int*)e; int width = 0; while (temp) { temp /= 10; width++; } return width; } /** * @brief 将二维数组打å°æˆè¡¨æ ¼æ ·å¼ * @param[in] a 二维数组å * @param[in] row_count 行数 * @param[in] col_count 列数 * @return æˆåŠŸè¿”回0,失败返回错误代ç * @note æ— * @remarks æ— */ int print_array(int* a, int row_count, int col_count, int(*get_elem_width)(void *e)) { int i = 0, j = 0; if(NULL == col_width) { return -1; } // get max width of each column for(i = 0; i < col_count; i++){ for(j = 0; j < row_count; j++) { int temp_width = get_elem_width(&a[j * row_count + i]); col_width[i] = col_width[i] > temp_width ? col_width[i] : temp_width; } } for(i = 0; i < col_count; i++){ if(col_width[i] % 2 != 0) { col_width[i]++; // 如果宽度为奇数,则增1,å˜æˆå¶æ•° } } //打å°è¡¨å¤´çº¿ print_table_line(HEAD, col_count, col_width); //打å°è¡¨æ ¼å†…容 for (i = 0; i < row_count; i++) { if (i > 0) print_table_line(MIDDLE, col_count, col_width); //打å°è¡¨ä¸çº¿ for (j = 0; j < col_count; j++) { int k = 0; int elem_width = get_elem_width(&a[i * col_count + j]); int space_count = col_width[j] - elem_width; // å±…ä¸æ‰“å°å…ƒç´ for(k = 0; k < space_count / 2; k++) { } for(k = 0; k < space_count / 2; k++) { } if(0 != space_count % 2 ) { } if (j < col_count - 1 ) } } print_table_line(TAIL, col_count, col_width); //打å°è¡¨å°¾ return 0; } int main() { int A[][3] = {{1, 2000, 3}, {4, 5, 35}, {123456789, 8, 9}}; print_array(&A[0][0], 3, 3, get_integer_width); return 0; }
URL: http://www.yanjiuyanjiu.com