2016-08-18 93 views
-1

輸入是從文本文件中檢索的。其中包含的C程序如何以表格格式對齊

  • 產品ID信息
  • 產品名稱
  • 產品數量
  • 產品價格

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

通過命令提示符打印輸出後。它不符合第一項。如何使它與表格的標題對齊。正如你可以看到第1行和第4行的輸入幾乎相同。

這裏是輸出。

enter image description here

下面是完整的代碼。用gotoxy函數。顯示功能是

int displayProduct() 

有一個表格標題和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); 
} 

更新一個變化:

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

+0

如果您需要控制檯中的格式化蒙版,請使用像ncurses這樣的屏幕庫。那隻'gotoxy'-only格式是非常過時的並且容易出錯。 – Olaf

回答

1

打印的庫存應該使用長度說明在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); 
} 

一些關於代碼的說明:

  • 遞歸調用main()是非常不好的風格。改用循環。

  • 編寫一個函數,打印標題而不是重複代碼多次。

  • 聲明int main();displayProduct()的結尾是函數main的本地聲明,它不生成調用。

+0

謝謝。我還在學習。將即興自己:) 你可以檢查更新的命令提示符。看來,第一行只有跟隨。 –

+0

問題來自'gotoxy(5,10);'循環之前。只有第一行有'5'的縮進。我更新瞭解決此問題的答案。 – chqrlie

+0

好吧,很酷。管理解決它。我還有1個問題,這意味着-10,-12,8是什麼意思? –