Tabelle in C mit ASCI?
Hallo wollte fragen wie man in C eine Tabelle mit ASCII Zeichen ausgibt?
Danke!
2 Antworten
for (char c = ' '; c <= '~'; ++c) {
printf("'%c': 0x%02X\n", c, (int)c);
}
Von gutefrage auf Grund seines Wissens auf einem Fachgebiet ausgezeichneter Nutzer
programmieren
Sehr schön, deine Frage hat mir knapp eine Stunde Langeweile erspart. ^^
Und schon gehts los:
Zuerst mal der passende Header (table.h):
#ifndef TABLE_H_F657844E41F822BDAA0A94658630DA0AE98A63EBEA20F1BF9CD0EBF14D1B86EE232AA03C697D226D4B20343FB8F113F675881835114EEAB15EF8FF499A3E2DE2
#define TABLE_H_F657844E41F822BDAA0A94658630DA0AE98A63EBEA20F1BF9CD0EBF14D1B86EE232AA03C697D226D4B20343FB8F113F675881835114EEAB15EF8FF499A3E2DE2
struct table;
typedef struct table table;
table *table_create(int rows, int cols);
char *table_put(table *tbl, int row, int col, const char *data);
void table_print(const table *tbl);
void table_destroy(table *tbl);
#endif /*TABLE_H_F657844E41F822BDAA0A94658630DA0AE98A63EBEA20F1BF9CD0EBF14D1B86EE232AA03C697D226D4B20343FB8F113F675881835114EEAB15EF8FF499A3E2DE2*/
Dann die Implementierung (table.c):
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "table.h"
struct table{
char ***data;
int cols, rows;
};
static inline int max(int a, int b){
if(a > b) return a;
return b;
}
void table_print(const table *tbl){
int width[tbl->cols];
for(int col = 0; col < tbl->cols; ++col) width[col] = 0;
for(int row = 0; row < tbl->rows; ++row){
for(int col = 0; col < tbl->cols; ++col){
if(!tbl->data[row][col]) continue;
width[col] = max(strlen(tbl->data[row][col]), width[col]);
}
}
char fmt[tbl->cols][10];
for(int col = 0; col < tbl->cols; ++col)
sprintf(fmt[col], "%%-%ds", width[col]);
printf("+");
for(int col = 0; col < tbl->cols; ++col){
for(int n = 0; n < width[col]; n++){
printf("-");
}
printf("+");
}
printf("\n");
for(int row = 0; row < tbl->rows; ++row){
printf("|");
for(int col = 0; col < tbl->cols; ++col){
if(!tbl->data[row][col]) printf(fmt[col], "");
else printf(fmt[col], tbl->data[row][col]);
printf("|");
}
printf("\n");
printf("+");
for(int col = 0; col < tbl->cols; ++col){
for(int n = 0; n < width[col]; n++){
printf("-");
}
printf("+");
}
printf("\n");
}
}
table *table_create(int rows, int cols){
table *tbl = malloc(sizeof(table));
if(!tbl) return 0;
tbl->data = malloc(sizeof(char**)*rows);
if(!tbl->data){
free(tbl);
return 0;
}
tbl->rows = rows;
tbl->cols = cols;
for(int row = 0; row < rows; row++){
tbl->data[row] = malloc(sizeof(char*)*cols);
if(!tbl->data[row]){
table_destroy(tbl);
return 0;
}
for(int col = 0; col < cols; ++col){
tbl->data[row][col] = 0;
}
}
return tbl;
}
void table_destroy(table *tbl){
for(int row = 0; row < tbl->rows; row++){
if(!row) continue;
for(int col = 0; col < tbl->cols; ++col){
if(tbl->data[row][col]) free(tbl->data[row][col]);
}
free(tbl->data[row]);
}
free(tbl->data);
free(tbl);
}
char *table_put(table *tbl, int row, int col, const char *data){
int data_len = strlen(data);
if(!tbl->data[row][col]){
tbl->data[row][col] = malloc(sizeof(char) * (data_len + 1));
if(!tbl->data[row][col]) return 0;
}
else{
char *tmp = realloc(tbl->data[row][col], sizeof(char) * (data_len + 1));
if(!tmp) return 0;
}
memcpy(tbl->data[row][col], data, data_len + 1);
return tbl->data[row][col];
}
char *table_get(table *tbl, int row, int col){
return tbl->data[row][col];
}
Und noch ein kleines Testprogramm (table_test.c):
#include "table.h"
int main(int argc, char *argv[]){
int rows = 4, cols = 3;
table *tbl = table_create(rows, cols);
table_put(tbl, 0, 0, "foo");
table_put(tbl, 0, 1, "bar");
table_put(tbl, 1, 2, "fizz");
table_put(tbl, 3, 0, "buzz");
table_print(tbl);
table_destroy(tbl);
}
Ausgabe diese Testprogramms:
+----+---+----+
|foo |bar| |
+----+---+----+
| | |fizz|
+----+---+----+
| | | |
+----+---+----+
|buzz| | |
+----+---+----+
Woher ich das weiß:Hobby