0

我正在尝试在控制台中打印出包含一些元素的表格。我有一个 macbook pro,用 linux mint 分区。在 linux 中,这完美地工作,一切都正确打印。

但是,在 macOS 中,我没有得到预期的结果。我将链接底部的源代码。源代码是用 linux 编写的,但我只是使用标准 C 的东西。

这是我得到的。

MacOS 终端:

Linux终端:

我对使用 linux 和在两个不同的操作系统之间进行编译非常陌生。有什么我在这里俯瞰的吗。

来源:Ctables.h

源代码

/**************************************************************
*
*   Ctables.h
*
*
**/
 
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
 
#define MAX_OPS 3
 
 
enum {
    FREELY = 2,
    STRICT,
    COLORFUL,
    RAINBOW,
    CENTER,
    LEFT,
    RIGHT,
    NONE
};
 
typedef enum{FALSE,TRUE}bool;
 
typedef struct {
    char *str;
    char *color;
    int width;
    int cell_width;
    int max_cell_w;
}table_cell;
 
 
typedef struct {
    int row_dimension;
    int col_dimension;
    int index_i;
    int index_j;
    int options[MAX_OPS];
    table_cell **info;
    int capacity;
 
}table_t;
/*Extra functions*/
void ms(int space, char symbol); /*prints space or a given symbol*/
int return_biggest(table_t *table, int row); /*finds the biggest string in a given row*/
int *calculate_width(table_t *table);
 
/*Operations*/
table_t *initialze_table(int op[], int dim_i , int dim_j);
void add(table_t *table, char *str);
void print(table_t *table);
void free_table(table_t *table);
 
 
/*initialize a table, what kind it is
 
    Op: fill in the array with your options, pass it to the struct
 
    0. STRICT - User adds dimensions, table handles the indexing
       FREELY - Data is added to table and user
            handles indexing
    1. Options - COLORFUL : make table sensitive to color and higlighting
             RAINBOW : print table in rainbow colors, this will
                automatically turn on colors as well
    2. Alignment - CENTER, LEFT, RIGHT
 
    example:
        int options[3] = {STRICT, COLORFUL, CENTER};
    --create new table
        table_t neu_tableu = initialize_table(options,5,5);
            makes a colorful new table of size 5 * 5
         
 
         
*/
void ms(int space, char symbol) {
int i;
 
    if(symbol == 's') {
 
    for(i = 0; i < space; i++) {
        printf(" ");
        }
    }
 
    else {
     
    for(i = 0; i < space; i++) {
        printf("%c",symbol);
        }
         
    }
 
}
 
int return_biggest(table_t *table, int row) {
int i;
int biggest = table -> info[0][row].width;
    for(i = 0; i < table -> row_dimension; i++) {
        if(table -> info[i][row].width > biggest) {
            biggest = table -> info[i][row].width;
        }
    }
 
    return biggest;
 
}
 
int *calculate_width(table_t *table) {
int i,j;
/*Nr of elements to calculate*/  
int elements = table -> col_dimension;
    /*make container*/
    int *array_biggest = (int *)malloc(elements * sizeof(int));
     
    /*Fill container with biggest element in each row*/
        for(i = 0; i < elements; i++) {
            array_biggest[i] = return_biggest(table,i);
        }
 
/*
    LEFT: Has no space at the beginning, 4 columns at the end
        |example    |
    RIGHT:
          4 columns at the beginning
        |    example|
    CENTER:
        2 columns at beginning, 2 at the end
        |  example  |
 
Note: this is compared to the longest string, to make everything fit
    we have to calculate each space needed for each smaller string
    added to the table in terms of formatting specs
 
*/
    if(table -> options[2] > 6)  {
        for(i = 0; i < table -> col_dimension;i++) {
        for(j = 0; j < table -> row_dimension; j++) {
            if(table -> info[j][i].width < array_biggest[i]) {
                table -> info[j][i].cell_width = ((array_biggest[i] + 4) - table -> info[j][i].width);
            }
            else {
                table -> info[j][i].cell_width = 4;
            }
            table -> info[j][i].max_cell_w = array_biggest[i] + 4;
            }
        }
        }
    else {
        for(i = 0; i < table -> col_dimension; i++) {
            for(j = 0; j < table -> row_dimension; j++) {
                if(table -> info[j][i].width < array_biggest[i]) {
                    table -> info[j][i].cell_width = ((array_biggest[i] + 4) - table -> info[j][i].width) / 2;
    }
                else {
                    table -> info[j][i].cell_width = 2;
                }
              table -> info[j][i].max_cell_w = array_biggest[i] + 4;
            }
        }
        }
 
        return array_biggest;
}
 
table_t *initialize_table(int op[], int dim_i,int dim_j) {
int i,j;
int mul = dim_i * dim_j;
    table_t *new_table = (table_t *)malloc(sizeof(table_t));        
        new_table -> info = (table_cell **)malloc(sizeof(table_cell *) * dim_i);
    for(i = 0; i < dim_i; i++) {
        new_table -> info[i] = (table_cell *)malloc(sizeof(table_cell) * dim_j);
    }
        new_table -> capacity = dim_i * dim_j;
        new_table -> row_dimension = dim_i;
        new_table -> col_dimension = dim_j;
        new_table -> index_i = 0;
        new_table -> index_j = 0;
 
    for(j = 0; j < MAX_OPS;j++) {
        new_table -> options[j] = op[j];
    }
 
    /*Set default for every cell in the table*/
    for(i = 0; i < new_table -> row_dimension; i++) {
        for(j = 0; j < new_table -> col_dimension; j++) {
            new_table -> info[i][j].str = "";    
            new_table -> info[i][j].color = "None";  
            new_table -> info[i][j].width = 0;
            new_table -> info[i][j].cell_width = 4;
        }
    }
 
 
 
    return new_table;
}
 
 
void add(table_t *table, char *in_str) {
 
    table -> info[table -> index_i][table -> index_j].str = in_str;
    table -> info[table -> index_i][table -> index_j].width = strlen(in_str);
 
    table -> index_j++;
        if(table -> index_j == table -> col_dimension) {
            table -> index_j = 0;
            table -> index_i++;
        }   
}
 
void print(table_t *table) {
int i,j;
int *width_arr = calculate_width(table);
int wide = 0;
int check_size;
    /*Get overall table width, add together biggest
      strings from all rows*/
    for(i = 0; i < table -> col_dimension; i++) { 
        wide += width_arr[i] + 4;
    }
    /*On top of this, add */
        wide += table -> col_dimension - 1;
 
    printf("+");
    ms(wide,'-');
    printf("+\n");
 
 
    for(i = 0; i < table -> row_dimension; i++) {
        printf("|");
        for(j = 0; j < table -> col_dimension; j++) {
        switch(table -> options[2]) {
        case LEFT:
            printf("%s",table -> info[i][j].str);
            ms(table -> info[i][j].cell_width,'s');
            printf("|");
            break;
        case RIGHT:
            ms(table -> info[i][j].cell_width,'s');
            printf("%s", table -> info[i][j].str);
            printf("|");
            break;
        case CENTER:
        check_size = (table -> info[i][j].cell_width + table -> info[i][j].width + table ->info[i][j].cell_width);
        if(check_size < table -> info[i][j].max_cell_w) 
         {
            ms(table -> info[i][j].cell_width,'s');
            printf("%s", table -> info[i][j].str);
            ms(table -> info[i][j].cell_width + table -> info[i][j].max_cell_w - check_size,'s');
        }
        else {
            ms(table -> info[i][j].cell_width,'s');
            printf("%s", table -> info[i][j].str);
            ms(table -> info[i][j].cell_width,'s');
        }
            printf("|");
            break;
     
        }
        }
        printf("\n");
    }
 
    printf("+");
    ms(wide,'-');
    printf("+");
 
        printf("\n");
 
    /*FREE CONTAINER*/
        free(width_arr);
 
}
 
void free_table(table_t *table) {
int i;
    for(i = 0; i < table -> row_dimension; i++) {
        free(table ->info[i]);
    }
    free(table -> info);
        free(table);
 
}
4

1 回答 1

4

var 宽线 220 不是初始化 :(

请将其初始化为零或其他

int wide; //<------- DECLARATION
int check_size;
    /*Get overall table width, add together biggest
      strings from all rows*/
    for(i = 0; i < table -> col_dimension; i++) { 
        wide += width_arr[i] + 4; //<----------- USE
    }
    /*On top of this, add */
        wide += table -> col_dimension - 2; //<----------- USE

    printf("+");
    ms(wide,'-'); //<----------- USE
    printf("+\n");

你有声明但没有初始化

你在 linux 上很幸运,因为 Wide 等于 0,但在 Mac OS 中不是

于 2013-03-04T12:57:53.500 回答