C语言 C程序如何以表格格式对齐打印

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39025074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:28:06  来源:igfitidea点击:

C program how to print in table format alignment

c

提问by Alif Khair

The input is retrieve from text file. Which contain information of

输入是从文本文件中检索的。其中包含的信息

  • product ID
  • product Name
  • Product Quantity
  • product Price

    1    RAYBAN  1   450.000000 
    900 KEYBOARD 100 290.000000
    78 MINERALWATER 123 345.000000
    2 RAYBAN 2 450.000000
    
  • 产品编号
  • 产品名称
  • 产品数量
  • 产品价格

    1    RAYBAN  1   450.000000 
    900 KEYBOARD 100 290.000000
    78 MINERALWATER 123 345.000000
    2 RAYBAN 2 450.000000
    

After printing the output through command prompt. It was not align with the 1st item. How to make it align with the title of table. As you can see the input of line 1 and 4 almost the same.

通过命令提示符打印输出后。它与第一项不一致。如何使其与表格标题对齐。如您所见,第 1 行和第 4 行的输入几乎相同。

Here is the output.

这是输出。

enter image description here

在此处输入图片说明

Here is the full code. With gotoxyfunction. The display function is on

这是完整的代码。带gotoxy功能。显示功能开启

int displayProduct()

There is a line of code for table titles and also the printf from TXT File.

有一行代码用于表标题以及来自 TXT 文件的 printf。

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <ctype.h>

void gotoxy(int column, int line);
int main();
int addProduct();
int displayProduct(); //prototype

struct product {
    int quantity, reorder, i, id;
    char name[20];
    float price;
};

COORD coord = { 0, 0 };

void gotoxy(int x, int y) {
    coord.X = x; coord.Y = y;

    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

int main() {
    int choice;

    gotoxy(17, 5);
    printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");

    gotoxy(17, 20);
        printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");

    gotoxy(22, 8);
    printf("1. Add Product\n\n");

    gotoxy(22, 10);
    printf("2. Display Product\n\n");

    gotoxy(22, 12);
    printf("3. Search Product\n\n");

    gotoxy(22, 14);
    printf("4. Reorder Level of Product\n\n");

    gotoxy(22, 16);
    printf("5. Update Product\n\n");

    gotoxy(22, 18);
    printf("6. Exit\n\n");

    gotoxy(22, 22);
    printf("Please Enter Your Choice : ");
    scanf(" %d", &choice);

    switch (choice) {
      case 1:
        addProduct();
        break;
      case 2:
        displayProduct();
        break;
      case 3:
        searchProduct();
        break;
      case 4:
        reorderProduct();
        break;
      case 5:
        updateProduct();
        break;
      case 6:
        break;
      default:
        system("cls");
        main();
    }
    return (0);
}

/*MENU CODE ENDS !*/

int addProduct() {
    FILE *fp;

    int i = 0;
    struct product a;
    system("cls");

    fp = fopen("inventory.txt", "a+t");

    char checker;

    do {
        system("cls");

        gotoxy(17, 5);
        printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");

        gotoxy(17, 20);
        printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");

        gotoxy(22, 8);
        printf("Enter product ID : ");
        scanf(" %d", &a.id);

        gotoxy(22, 10);
        printf("Enter product name : ");
        scanf(" %s", a.name);

        gotoxy(22, 12);
        printf("Enter product quantity : ");
        scanf(" %d", &a.quantity);

        gotoxy(22, 14);
        printf("Enter product price : ");
        scanf(" %f", &a.price);

        gotoxy(22, 17);
        fprintf(fp, "%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price); //SAVE TO TXT FILE LINE !
        printf("Record saved!\n\n");

        fclose(fp);

        gotoxy(22, 22);
        printf("Do you want to enter new product? Y / N : ");

        scanf(" %c", &checker);
        checker = toupper(checker);

        i++;

        system("cls");
    } while (checker=='Y');

    if (checker == 'N') {
        main();
    } else {
        do {
            system("cls");

            gotoxy(17, 5);
            printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");

            gotoxy(17, 20);
            printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");

            gotoxy(18, 8);
            printf(">>> Wrong Input! Please Enter Y Or N Only! <<<");

            gotoxy(19, 12);
            printf("Do You Want To Enter New Product? Y / N : ");
            scanf(" %c", &checker);
            checker = toupper(checker);
        } while (checker != 'Y' && checker != 'N');

        if (checker == 'Y'){
            addProduct();
        }

        if (checker == 'N') {
            system("cls");
            main();
        }
    }
    return(0);
}

/*ADD PRODUCT LINE ENDS !*/

int displayProduct() {
    FILE *fp;
    struct product a;

    char true;
    system("cls");

    fp = fopen("inventory.txt", "r");

    gotoxy(17, 5);
    printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");

    gotoxy(5, 6);
    printf("======================================================================");

    gotoxy(5, 7);
    printf("Product ID\t\t Product Name\t\t Quantity\t Unit Price\n"); //TABLE TITLES !

    gotoxy(5, 8);
    printf("======================================================================");

    gotoxy(5,10);
    while (fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price) == 4) {
        printf("%d\t\t\t %s\t\t\t %d\t\t %.2f\n\n", a.id, a.name, a.quantity, a.price); //PRINT FROM TXT FILE TO COMMAND PROMPT.
    }
    fclose(fp);

    printf("\t\t \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");

    printf("\nPress any key to return to Main Menu.");

    getch();

    int main();

    return (0);
}

Updated one, changes made :

更新一,所做的更改:

gotoxy(5,10);
                while(fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price)==4)
                {
                printf("%-10d\t\t %-12s\t\t %8d\t %8.2f\n\n", a.id, a.name, a.quantity, a.price);
                }

                fclose(fp);

enter image description here

在此处输入图片说明

回答by chqrlie

The code to print the inventory should use the length specifier in printfformat like this:

打印库存的代码应使用如下printf格式的长度说明符:

gotoxy(0, 10);
while (fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price) == 4) {
    printf("    %-10d\t\t %-12s\t\t %8d\t %8.2f\n\n", a.id, a.name, a.quantity, a.price);
}

Some notes regarding the code:

关于代码的一些注意事项:

  • it is very bad style to call main()recursively. Use a loop instead.

  • write a function that prints the header instead of duplicating the code multiple times.

  • the statement int main();at the end of displayProduct()is a local declaration for function main, it does not generate a call.

  • main()递归调用是非常糟糕的风格。改用循环。

  • 编写一个打印标题而不是多次复制代码的函数。

  • int main();末尾的语句displayProduct()是 function 的局部声明main,它不会生成调用。