[ad_1]
問題 : レストランのプログラムを書きなさい。 プログラムは、顧客が朝食メニュー オプション、ランチ メニュー オプション、ディナー メニュー オプション、チェックアウト オプションのいずれかを選択できるようにするメイン メニューを表示する必要があります。 メイン メニューからオプションを選択した後、プログラムは、利用可能な食品と選択した食事の価格を表示するサブメニューを引き続き表示する必要があります。プログラムは、顧客が食品を選択して注文数量を入力できるようにする必要があります。 あなたのプログラムは、顧客がメイン メニューでチェック アウト オプションを選択するまで、食品を注文できるようにする必要があります。その後、プログラムだけが、顧客が支払う必要がある数量、価格、および合計金額を含む注文リストを表示します。
これは私のコーディングであり、購入したアイテムの領収書を表示するように改善する必要があります。合計金額を支払う必要があり、ユーザーが支払う予定の現金額を入力して残高を計算できるようにする必要があります。 私を助けてください。
#include <stdio.h> //header files #include <ctype.h> #include <windows.h> void bfast(); // prototype's void lunch(); void dinner(); void m_m(); void exit(); int num; //global variable float price; float total; char choice; int again; void main() { m_m(); } void m_m() // main menu screen { char choice = ' ' ; //local variable printf(" Welcome to D'10 Restaurant. \n "); printf(" +============================+ \n\n"); printf(" && Please select the meal that you would like to purchase. && \n\n"); printf("\t\t [A] Breakfast\n"); printf("\t\t [B] Lunch\n"); printf("\t\t [C] Dinner\n"); printf("\t\t [D] Exit\n\n"); printf("Enter your choice here : "); scanf("%c", &choice); system("cls"); { if (toupper(choice) == 'A' ) bfast(); else if (toupper(choice) == 'B') lunch(); else if (toupper(choice) == 'C') dinner(); else if (toupper(choice) == 'D') exit(); else if (toupper(choice) != 'A' , 'B' , 'C' , 'D') { m_m(); } } } void bfast() //Breakfast Menu Screen { int choice = 0; //local variables int quantity = 0; int again = 0; fflush(stdin); printf(" Welcome to D'10 Restaurant. \n "); printf(" +============================+ \n\n"); printf(" $ Breakfast Menu $ \n\n"); printf(" && Please select the food that you would like to purchase. && \n\n"); printf("\t\t [1] Toast - RM 1.00\n"); printf("\t\t [2] Egg Muffin - RM 1.50\n"); printf("\t\t [3] Nasi Lemak - RM 2.00\n"); printf("Enter your choice here : "); scanf("%d", &choice); { if (choice == 1) { printf("Enter quantity : "); scanf("%d", &quantity); total = 1.00 * quantity ; printf("Your total amount is RM%.2f , Please pay at the counter\n\n\n ", total); printf("\nWould you like to buy anything else?\n[1] Yes , [2] No : "); // Allows user to choose whether to check-out or buy anything else. scanf("%d", &again); system("cls"); if (again == 1 ) bfast(); else if (again == 2 ) m_m(); else if (again != 1 , 2) { printf("\n\n\t\tSorry Invalid Decision Entered\n\n\n\n"); exit(); } } else if ( choice == 2) { printf("Enter quantity : "); scanf("%d", &quantity); total = 1.50 * quantity ; printf("Your total amount is RM%.2f , Please pay at the counter\n\n\n ", total); printf("\nWould you like to buy anything else?\n[1] Yes , [2] No : "); // Allows user to choose whether to check-out or buy anything else. scanf("%d", &again); system("cls"); if (again == 1 ) bfast(); else if (again == 2 ) m_m(); else if (again != 1 , 2) { printf("\n\n\t\tSorry Invalid Decision Entered\n\n\n\n"); exit(); } } else if ( choice == 3 ) { printf("Enter quantity : "); scanf("%d", &quantity); total = 2.00 * quantity ; printf("Your total amount is RM%.2f , Please pay at the counter\n\n\n ", total); printf("\nWould you like to buy anything else?\n[1] Yes , [2] No : "); // Allows user to choose whether to check-out or buy anything else. scanf("%d", &again); system("cls"); if (again == 1 ) { bfast(); } else if (again == 2 ) { m_m(); } else if (again != 1 , 2) { printf("\n\n\t\tSorry Invalid Decision Entered\n\n\n\n"); exit(); } } else if (choice != 1 , 2 , 3 ) { fflush(stdin); system("cls"); printf("\n\n\t\t Invalid Choice Entered\n\n"); bfast(); } } } void lunch() // Lunch Screen Menu { int choice; //local variables int quantity; int again; printf(" Welcome to D'10 Restaurant. \n "); printf(" +============================+ \n\n"); printf(" $ Lunch Menu $ \n\n"); printf(" && Please select the food that you would like to purchase. && \n\n"); printf("\t\t [1] Fried Rice - RM 4.00\n"); printf("\t\t [2] Mee Goreng- RM 4.00\n"); printf("\t\t [3] Fish Head Curry with Rice - RM 5.00\n"); printf("Enter your choice here : "); scanf("%d", &choice); { if (choice == 1) { printf("Enter quantity : "); scanf("%d", &quantity); total = 4.00 * quantity ; printf("Your total amount is RM%.2f , Please pay at the counter\n\n\n ", total); { printf("\nWould you like to buy anything else?\n[1] Yes , [2] No : "); scanf("%d", &again); system("cls"); if (again == 1 ) lunch(); else if (again == 2 ) m_m(); else if (again != 1 , 2) { printf("\n\n\t\tSorry Invalid Decision Entered\n\n\n\n"); exit(); } } } else if ( choice == 2) { printf("Enter quantity : "); scanf("%d", &quantity); total = 4.00 * quantity ; printf("Your total amount is RM%.2f , Please pay at the counter\n\n\n ", total); { printf("\nWould you like to buy anything else?\n[1] Yes , [2] No : "); scanf("%d", &again); system("cls"); if (again == 1 ) lunch(); else if (again == 2 ) m_m(); else if (again != 1 , 2) { printf("\n\n\t\tSorry Invalid Decision Entered\n\n\n\n"); exit(); } } } else if ( choice == 3 ) { printf("Enter quantity : "); scanf("%d", &quantity); total = 5.00 * quantity ; printf("Your total amount is RM%.2f , Please pay at the counter\n\n\n ", total); { printf("\nWould you like to buy anything else?\n[1] Yes , [2] No : "); scanf("%d", &again); system("cls"); if (again == 1 ) lunch(); else if (again == 2 ) m_m(); else if (again != 1 , 2) { printf("\n\n\t\tSorry Invalid Decision Entered\n\n\n\n"); exit(); } } } else if (choice != 1 , 2 , 3) { fflush(stdin); system("cls"); printf("\n\n\t\t Invalid Choice Entered\n\n"); lunch(); } } } void dinner() // Dinner Menu Screen { int choice; //local variables int quantity; int again; printf(" Welcome to D'10 Restaurant. \n "); printf(" +============================+ \n\n"); printf(" $ Dinner Menu $ \n\n"); printf(" && Please select the food that you would like to purchase. && \n\n"); printf("\t\t [1] Chicken Chop - RM 6.50\n"); printf("\t\t [2] Spagetti - RM 5.50\n"); printf("\t\t [3] Chicken Burger - RM 3.00\n"); printf("Enter your choice here : "); scanf("%d", &choice); { if (choice == 1) { printf("Enter quantity : "); scanf("%d", &quantity); total = 6.50 * quantity ; printf("Your total amount is RM%.2f , Please pay at the counter\n ", total); { printf("\nWould you like to buy anything else?\n[1] Yes , [2] No : "); scanf("%d", &again); system("cls"); if (again == 1 ) dinner(); else if (again == 2 ) m_m(); else if (again != 1 , 2) { printf("\n\n\t\tSorry Invalid Decision Entered\n\n\n\n"); exit(); } } } else if ( choice == 2) { printf("Enter quantity : "); scanf("%d", &quantity); total = 5.50 * quantity ; printf("Your total amount is RM%.2f , Please pay at the counter\n ", total); { printf("\nWould you like to buy anything else?\n[1] Yes , [2] No : "); scanf("%d", &again); system("cls"); if (again == 1 ) dinner(); else if (again == 2 ) m_m(); else if (again != 1 , 2) { printf("\n\n\t\tSorry Invalid Decision Entered\n\n\n\n"); exit(); } } } else if ( choice == 3 ) { printf("Enter quantity : "); scanf("%d", &quantity); total = 3.00 * quantity ; printf("Your total amount is RM%.2f , Please pay at the counter\n\n\n ", total); { printf("\nWould you like to buy anything else?\n[1] Yes , [2] No : "); scanf("%d", &again); system("cls"); if (again == 1 ) dinner(); else if (again == 2 ) m_m(); else if (again != 1 , 2) { printf("\n\n\t\tSorry Invalid Decision Entered\n\n\n\n"); exit(); } } } else if (choice != 1 , 2 , 3) { fflush(stdin); system("cls"); printf("\n\n\t\t Invalid Choice Entered\n\n"); dinner(); } } } void exit() // Exit Screen { printf(" Thank You Very Much \n "); printf(" +============================+ \n\n"); printf(" && Please come again. && \n\n");
[edit]コードブロックの追加 – OriginalGriff[/edit]
解決策 1
さて、あなたは楽しそうに合計を計算して表示しています。 各関数が合計をメイン関数に返し、そこでそれを使用して支払いを受け取るようにすることは、大きな変更にはなりません。 関数から値を返す方法を知っていますね。
他にもいくつかのことが思い浮かびます。
1) それは本当にユーザーフレンドリーではありません!
if (again != 1 , 2) { printf("\n\n\t\tSorry Invalid Decision Entered\n\n\n\n"); exit(); }
彼が間違ったキーを押したためにプログラムを終了することは、指が太い人にとってはかなり面倒です! :笑う:
2) そうだけど 技術的に if..then..else..if… コードをそのインデントで正しく書くと、非常に読みにくくなります。
if (toupper(choice) == 'A') bfast(); else if (toupper(choice) == 'B') lunch(); else if (toupper(choice) == 'C') dinner(); else if (toupper(choice) == 'D') exit(); else if (toupper(choice) != 'A' , 'B' , 'C' , 'D') { m_m(); }
代わりに、 else
そしてその if
同じ行に:
if (toupper(choice) == 'A') bfast(); else if (toupper(choice) == 'B') lunch(); else if (toupper(choice) == 'C') dinner(); else if (toupper(choice) == 'D') exit(); else if (toupper(choice) != 'A', 'B' , 'C' , 'D') { m_m(); }
(そして、私は最終的な if before でその構造を見たことがありません – それはコンパイルされますか?)
私は実際にそれを変更し、代わりにスイッチを使用します:
switch (toupper(choice)) { case 'A': bfast(); break; case 'B': lunch(); break; case 'C': dinner(); break; case 'D': exit(); default: m_m(); break; }
[ad_2]
コメント