2011-12-15 26 views
0

我在Visual C中開發uni項目的這個階段時遇到了問題。我想知道如果你能幫我一個大忙併幫我解決它。
在這個階段,我們必須建立它的菜單包括這五個子菜單:編程簡單程序的菜單階段

  1. 繼續最後一場比賽...
  2. 單人
  3. 選項
  4. 現金
  5. 最高分

我們的導航政策:用向上和向下箭頭滾動瀏覽這5個項目(箭頭) 我們用enter鍵進入子菜單,當我們進入子菜單時,我們應該再次通過Enter回來 Esc幫助我們退出程序!
好的!我的問題是第二部分,我不能通過Enter回到子菜單! 這是我的代碼:

#include<stdio.h> 
#include<conio.h> 
#include<stdlib.h> 
int m1(int n); 
int m2(int n); 
int m3(int n); 
int m4(int n); 
int m5(int n); 
int a(int n); 
char c,x,y; 
int i=1; 
int main(){ 
K: printf("-->Continue last game...\nSingle player\nOptions\nCredits\nTop scores"); 
    while(1){ 
     c=getch(); 
     if (c!=13&&c!=27&&c!=-32) {continue;} 
     if (c==13) {a(i); L:c=getch(); if (c==13) {system("cls");goto K;} else goto L;} 
     if (c==27) {system("cls");printf("Press eny key to Exit...");y=getch();break;} 
     x=getch(); 
     if (c==-32&&x==72&&i!=1) (i--); else if (c==-32&&x==72&&i==1) i=5; 
     if (c==-32&&x==80&&i!=5) (i++); else if (c==-32&&x==80&&i==5) i=1; 
     switch (i){ 
      case 1: 
     system("cls"); 
     m1(i); 
     break; 
     case 2: 
     system("cls"); 
     m2(i); 
     break; 
     case 3: 
     system("cls"); 
     m3(i); 
     break; 
     case 4: 
     system("cls"); 
     m4(i); 
     break; 
     case 5: 
     system("cls"); 
     m5(i); 
     break; 
     }} 
     return 0; 
    } 
int m1(int n){ 
    printf("-->Continue last game...\nSingle player\nOptions\nCredits\nTop scores"); 
    return i; 
} 
int m2(int n){ 
    printf("Continue last game...\n-->Single player\nOptions\nCredits\nTop scores"); 
    return i; 
} 
int m3(int n){ 
    printf("Continue last game...\nSingle player\n-->Options\nCredits\nTop scores"); 
    return i; 
} 
int m4(int n){ 
    printf("Continue last game...\nSingle player\nOptions\n-->Credits\nTop scores"); 
    return i; 
} 
int m5(int n){ 
    printf("Continue last game...\nSingle player\nOptions\nCredits\n-->Top scores"); 
    return i; 
} 

int a(int n){ 
    switch (i){ 
    case 1: {system("cls");printf("you've chosen \"Continue last game...\" \n\n\n\n\n\n\n\n press Enter to return to main menu");} break; 
    case 2: {system("cls");printf("you've chosen \"single player\" \n\n\n\n\n\n\n\n press Enter to return to main menu");}break; 
    case 3: {system("cls");printf("you've chosen \"Options\" \n\n\n\n\n\n\n\n press Enter to return to main menu");}break; 
    case 4:{system("cls");printf("you've chosen \"Options\" \n\n\n\n\n \tLord.Pooria Rajabzadeh (EiNsTEiN.co(class of 2000))\n\tProducer: Pooria on 15 dec 2011\n\n press Enter to return to main menu");}break; 
    case 5: {system("cls");printf("you've chosen \"Top scores\" \n\n\n\t Pooria 100\n\tAkbar 80\n\tAli 60\n\n\n press Enter to return to main menu");}break; 
    return i;} 
} 
+0

一些#defines或枚舉將使這個更容易理解和使用,並且你有很多`i`s,看起來你應該有`n`s(反之亦然)。 – crashmstr 2011-12-15 19:36:42

回答

3

考慮以下幾點:

  1. 您有一個顯示項目的列表,並允許您從列表中選擇一個項目的功能。
  2. 如果您從函數B()中調用函數A(),那麼當您完成執行A()中的代碼時,默認程序流將在B()中恢復。
  3. 如果選擇main()中的菜單項調用另一個功能submenu()會發生什麼情況,該功能會顯示其自己的項目列表?如果您完成執行submenu()並在用戶點擊Enter鍵時返回到main()),那麼在程序流程中會發生什麼?

使用單獨的函數來表示子菜單是一個可行的解決方案,但它可能不是最好的做法。有很多複製和粘貼操作來處理每個函數中的菜單「機制」(顯示基本菜單,表明突出顯示的選項等),這可能會導致修復錯誤和長時間維護此代碼的問題 - 如果存在錯誤如何顯示一個菜單,你必須通過你創建的所有菜單功能,並修復每個菜單中的錯誤。

如果要鞏固這種分解成更緊湊,更易於維護的方法,也有你可能要考慮一些事情:

  • 你在做什麼是顯示列表。
  • 您要顯示的列表是一系列項目。
  • 每個項目可能包含它自己的列表,或者可能只是要顯示的文本,甚至是指向另一個函數的指針。

你將如何構建一個函數來顯示和與這種List對象進行交互?

+0

謝謝你,真的真的很棒!感謝您爲寫出這個驚人的幫助而付出的所有時間和精力。我會跟隨你的領導,我會說結果 – 2011-12-15 22:05:43