2015-10-18 98 views
0

我剛剛開始編寫代碼,我試圖重新創建Monty Hall問題遊戲節目。我的程序爲用戶提供了在遊戲模式,研究模式和退出遊戲之間的選擇。我試圖簡化我的代碼,並將遊戲模式和研究模式放入函數中,但我所做的所有研究對我來說都沒有多大意義。我將不勝感激,謝謝!如何簡化這段代碼並將其變成函數?

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 


int winning (int win); 
float PERCENTAGE (float win,float playedgames); 
int RANDOM(); 


int main (void) 
{ 
int choice, choice2, Ngames, Ngames2,chosendoor,chosendoor2; 
int gamesplayed,gamesplayed2,winningdoor,winningdoor2,revealeddoor; 
int switcheddoor, revealeddoor2,stayeddoor, doorchoice,winningdoor3; 
int revealeddoor3, choice3, switcheddoor2; 
float percent,percent2; 
float win = 0,win2 = 0; 
int gamesplayed3 = 0,win3 = 0; 


srand(time(NULL)); //randomizes based on time 


while (choice!=3) 
{ 
    printf ("Please enter one of the following options\n\n1. Research mode\n2. Game mode\n3. Exit\n\n"); 
    scanf ("%d", &choice); 


    switch(choice) 
    { 
    case 1: 
     printf ("\nYou have chosen research mode.\n"); 
     printf ("Please indicate whether you will always us the always- switch strategy or the never-switch strategy.\n"); 
     printf ("1. Always switch doors\n2. Never switch doors\n\n"); 
     scanf ("%d", &choice2); 


     switch (choice2) 
     { 
     case 1: 
      printf ("\nYou have chosen to always switch doors.\n"); 
      printf ("How many games would you like to play?\n\n"); 
      scanf ("%d", &Ngames); 


      for (gamesplayed = 0;gamesplayed <= Ngames;gamesplayed++) 
      { 
       winningdoor = RANDOM(); 
       chosendoor = 1; 


       do 
       { 
        revealeddoor = RANDOM(); 
       } while (revealeddoor == chosendoor || revealeddoor == winningdoor); 


       do 
       { 
        switcheddoor = RANDOM(); 
       } while (switcheddoor == chosendoor || switcheddoor == revealeddoor); 


       if (switcheddoor == winningdoor) 
       { 
        win = winning(win); 
       } 
      } 


      percent = PERCENTAGE(win,gamesplayed); 


      printf ("\nYou have chosen the winning door %f percent of the time.\n\n", percent); 


      win = 0; 
      break; 


     case 2: 
      printf ("\nYou have chosen to never switch doors.\n"); 
      printf ("How many games would you like to play?\n\n"); 
      scanf ("%d", &Ngames2); 


      for (gamesplayed2 = 0;gamesplayed2 <= Ngames2; gamesplayed2++) 
      { 
       winningdoor2 = RANDOM(); 
       chosendoor2 = 1; 


       do 
       { 
        revealeddoor2 = RANDOM(); 
       } while (revealeddoor2 == winningdoor2 || revealeddoor2 == chosendoor2); 


       do 
       { 
        stayeddoor = RANDOM(); 
       } while (stayeddoor != chosendoor2); 


       if (stayeddoor == winningdoor2) 
       { 
        win2 = winning(win2); 
       } 
      } 


      percent2 = PERCENTAGE(win2,gamesplayed2); 


      printf ("\nYou chose the winning door %f percent of the time.\n\n", percent2); 


      win2 = 0; 
      break; 


     default: 
      printf ("You have entered an incorrect value."); 
      break; 
     } 
     break; 


    case 2: 
     printf ("\nYou have chosen game mode.\n"); 


     winningdoor3 = RANDOM(); 


     printf ("Which door will you choose? (Door 1,2, or 3)\n\n"); 
     scanf ("%d", &doorchoice); 


     if (doorchoice < 1 || doorchoice > 3) 
     { 
      printf ("That value is not a door.\n\n"); 
      break; 
     } 


     do 
     { 
      revealeddoor3 = RANDOM(); 
     } while (revealeddoor3 == doorchoice); 


     printf ("\nDoor %d was opened and there was no prize behind it.\n", revealeddoor3); 
     printf ("Would you like to:\n1. Switch doors?\n2. Stay with your current door?\n\n"); 
     scanf ("%d", &choice3); 


     switch (choice3) 
     { 
     case 1: 
      printf ("\nYou have chosen to switch doors.\n\n"); 


      do 
      { 
       switcheddoor2 = rand()%2 + 1; 
      } while (switcheddoor2 == doorchoice); 


      if (switcheddoor2 == winningdoor3) 
      { 
       printf ("Good choice! You have won!\n\n"); 
       win3 = winning(win3); 
      } 


      else 
      { 
       printf ("Unfortunately, you lost.\n\n"); 
      } 


      ++gamesplayed3; 


      printf ("You have played a total of %d games and you won a total of %d games.\n", gamesplayed3, win3); 
      break; 


     case 2: 
      printf ("\nYou have decided to stay with your current door.\n"); 


      if (doorchoice == winningdoor3) 
      { 
       printf ("Good choice! You have won!\n\n"); 
       win3 = winning(win3); 
      } 


      else 
      { 
       printf ("Unfortunately, you lost.\n\n"); 
      } 


      gamesplayed3 = gamesplayed3 + 1; 


      printf ("You have played a total of %d games and you won a total of %d games.\n\n", gamesplayed3, win3); 
      break; 


     default: 
      printf ("You have entered an incorrect value.\n\n"); 
      break; 
     } 
     break; 


     case 3: 
      printf("Thanks for playing!"); 
      exit(0); 


     default: 
      printf ("You have entered an incorrect value.\n\n"); 
      break; 
    } 
} 
return 0; 
} 


int winning (int win) 
{ 
int newwin; 
newwin = win + 1; 
return (newwin); 
} 


float PERCENTAGE (float win,float playedgames) 
{ 
float percentage; 
percentage = (win/playedgames)*100; 
return (percentage); 
} 


int RANDOM() 
{ 
int random; 
random = rand()%3; 
return (random); 
} 
+0

這段代碼實際上是不可讀的... atleast將案例中的循環移動到一個函數中。還請簡要準確地說明問題到底是什麼。 – knightrider

+0

我真的很新,而且還在學習。你能否向我展示一個我的循環作爲函數的樣本?我不知道如何。 – Gennnnnaaa

回答

3

下面是一個例子,轉換交換機的情況下一個...

這裏是你的代碼

case 1: 
    printf ("\nYou have chosen to always switch doors.\n"); 
    printf ("How many games would you like to play?\n\n"); 
    scanf ("%d", &Ngames); 
    for (gamesplayed = 0; gamesplayed <= Ngames; gamesplayed++) 
    { 
     winningdoor = RANDOM(); 
     chosendoor = 1; 
     do 
     { 
      revealeddoor = RANDOM(); 
     } 
     while (revealeddoor == chosendoor || revealeddoor == winningdoor); 
     do 
     { 
      switcheddoor = RANDOM(); 
     } 
     while (switcheddoor == chosendoor || switcheddoor == revealeddoor); 
     if (switcheddoor == winningdoor) 
     { 
      win = winning(win); 
     } 
    } 
    percent = PERCENTAGE(win, gamesplayed); 
    printf ("\nYou have chosen the winning door %f percent of the time.\n\n", percent); 

    win = 0; 
    break; 

這裏是一樣的長相如何後的功能提取

case 1: 
    printf ("\nYou have chosen to always switch doors.\n"); 
    printf ("How many games would you like to play?\n\n"); 
    scanf ("%d", &Ngames); 
    percent = PERCENTAGE(win, kernel(Ngames)); 
    printf ("\nYou have chosen the winning door %f percent of the time.\n\n", percent); 
    win = 0; 
    break; 

下面是函數的樣子

int kernel (int Ngames) 
{ 
    int gamesplayed; 
    for (gamesplayed = 0; gamesplayed <= Ngames; gamesplayed++) 
    { 
     int winningdoor = RANDOM(); 
     int chosendoor = 1; 
     int revealeddoor,switcheddoor; 
     do 
     { 
      revealeddoor = RANDOM(); 
     } 
     while (revealeddoor == chosendoor || revealeddoor == winningdoor); 
     do 
     { 
      switcheddoor = RANDOM(); 
     } 
     while (switcheddoor == chosendoor || switcheddoor == revealeddoor); 
     if (switcheddoor == winningdoor) 
     { 
      win = winning(win); 
     } 
    } 
    return gamesplayed; 
} 

現在,當你在特定的開關的情況下,你可以理解它的意義要好得多。只有當你需要知道內核的邏輯時,你必須進入內核。

我會說,如果你正在計算一個具有真正含義的東西將它作爲一個函數移出。如果您重複使用某些代碼,請將其作爲函數。如果你的函數很長或者有太多的循環(while,for)或者有條件的嵌套(if,switch),那麼現在是模塊化代碼的時候了。