用C语言画图Table

Drawing Table With C Language

The table

您好!我在 table 关闭时遇到问题。我想关闭带有一些动态字符串的 table,那么如何修复 table 关闭位置?谢谢

#include <stdio.h>

char top[500] = "\xda\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc2\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xbf\n";

char border[500] = "\xc3\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc5\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xb4\n";

char bottom[500] = "\xc0\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc1\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xd9\n";

char nama[30];
char alamat[150];
char telepon[15];
char email[50];

int main(){
    printf("Masukan Nama Lengkap : ");gets(nama);
    printf("Masukan Alamat Anda : "); gets(alamat);
    printf("Masukan Nomor Telepon Anda : "); gets(telepon);
    printf("Masukan Email Anda : "); gets(email);

    printf(top);
    printf("\xb3 Nama     \xb3 %s                                          \xb3\n", nama);
    printf(border);
    printf("\xb3 Alamat   \xb3 %s                                          \xb3\n", alamat);
    printf(border);
    printf("\xb3 No.Telp  \xb3 %s                                          \xb3\n", telepon);
    printf(border);
    printf("\xb3 Email    \xb3 %s                                          \xb3\n", email);
    printf(bottom);
    printf("\xfb");
    return 0;
}

打印 table 时,您需要控制在各个单元格中打印的字符数。 printf(fprintf) 的转换说明符允许您这样做。标准说(草案 N1570):

Each conversion specification is introduced by the character %. After the %, the following appear in sequence:

...

— An optional minimum field width. If the converted value has fewer characters than the field width, it is padded with spaces (by default) on the left (or right, if the left adjustment flag, described later, has been given) to the field width.

The flag characters and their meanings are:

- The result of the conversion is left-justified within the field. (It is right-justified if this flag is not specified.)

因此,如果您对 printf 使用 %100s,并且您提供的字符串少于 100 个字符,则 printf 将添加空格,以便打印的字符数恰好为 100。

使用 %100s%-100s 您可以控制字符串的对齐方式。

int main(void) 
{
    char* str = "Hello World";
    printf("| %30s |\n", str);
    printf("| %-30s |\n", str);
    return 0;
}

输出:

|                    Hello World |
| Hello World                    |

注意:如果您的字符串比指定的字段宽度长,printf 仍会打印您的所有字符串。因此,您需要确保字符串始终可以包含在 table.

的单元格内

顺便说一句:

你应该阅读 Why is the gets function so dangerous that it should not be used?