2014-02-11 90 views
0

在下面的代碼中,我需要關於switch case替代品的幫助,我的意思是我該如何避免切換並使用其他選項來執行代碼。交換機的替代品

代碼:

cout << " *********************Intensive Program 1***********************\n\n" << endl; 
cout << "\nHere's the menu of choices: "; 
cout << "\n1. Add a Circle.\n"; 
cout << "2. Print a Circle. \n"; 
cout << "3. Print all Cricles \n"; 
cout << "4. Exit.\n"; 
cout << "\nPlease enter your choice: "; 
cin >> choice; 
while (choice <4){ 
    switch (choice){ 

     case 1: 
      // For adding cirles to the class 
      cout << " Enter the value for the radius of the circle: "; 
      cin >> radius; 
      cout << " Enter the value for the center of the circle: "; 
      cin >> center; 
      myCircle[thisPosition] = new Circle(radius, center); 
      myCircle[thisPosition]->PrintCircle(); 
      thisPosition++; 
      break; 

     case 2: // For printing a particular cirle from the list of cirles 
      cout << " Enter the Value for which Circle to Print: "; 
      cin >> printPosition; 
      myCircle[printPosition - 1]->PrintCircle(); 
      break; 

     case 3: // For printing all the circles in the class object array pointer list 
      cout << "\n"; 
      for (int i = 0; i < thisPosition; i++){ 
       myCircle[i]->PrintCircle(); 
       cout << "\n==============================" << endl; 
      } 
      break; 

     case 4: 
      cout << "\n Good Bye !!! \n " << endl; 
      break; 
     }  
     cout << "\nHere's the menu of choices: "; 
     cout << "\n1. Add a Circle.\n"; 
     cout << "2. Print a Circle. \n"; 
     cout << "3. Print all Cricles \n"; 
     cout << "4. Exit."; 
     cout << "Please enter your choice: "; 
     cin >> choice; 
     system("pause"); 
    } 
} 
+1

您的其他選擇是使用一堆'if/else if'風格的語句,但在這種情況下,切換是一個更好的選擇。 – Annabelle

+1

你指的是什麼「其他選擇/替代」?在這種情況下,switch語句可能是最好的選擇。 – TypeIA

+4

有許多選項,包括一堆'if'和'goto'語句。告訴我們*爲什麼*你想避免'switch'對於給你一個實際有用的答案是非常有幫助的。你試圖解決的**實際問題是什麼?我懷疑你有[XY問題](http://meta.stackexchange.com/q/66377/167210)。 –

回答

0

使用的if-else梯是switch語句的選擇之一。

cout << " *********************Intensive Program 1***********************\n\n" << endl; 
cout << "\nHere's the menu of choices: "; 
cout << "\n1. Add a Circle.\n"; 
cout << "2. Print a Circle. \n"; 
cout << "3. Print all Cricles \n"; 
cout << "4. Exit.\n"; 
cout << "\nPlease enter your choice: "; 
cin >> choice; 
while (choice <4){ 

    if(choice==1) 
    { 
     // For adding cirles to the class 
     cout << " Enter the value for the radius of the circle: "; 
     cin >> radius; 
     cout << " Enter the value for the center of the circle: "; 
     cin >> center; 
     myCircle[thisPosition] = new Circle(radius, center); 
     myCircle[thisPosition]->PrintCircle(); 
     thisPosition++; 
    } 
    else if(choice==2) 
    { // For printing a particular cirle from the list of cirles 
     cout << " Enter the Value for which Circle to Print: "; 
     cin >> printPosition; 
     myCircle[printPosition - 1]->PrintCircle(); 
    } 
    else if(choice==3) // For printing all the circles in the class object array pointer list 
    { 
     cout << "\n"; 
     for (int i = 0; i < thisPosition; i++){ 
      myCircle[i]->PrintCircle(); 
      cout << "\n==============================" << endl; 
     } 
    } 
    else 
    { 
     cout << "\n Good Bye !!! \n " << endl; 
    } 
    }  
    system("pause"); 
} 
+0

桌子的函數指針或類似函數可以更好地替代具有順序值的開關語句。 –

4

你也可以使用一個std::vector<std::function<void()>> choices(5); ...並調用它像choices[choice]();你喜歡choices[1] = &function_1;等替代選擇,充滿後...

但我有一種感覺,真正的問題不是真正如何避免switch ...

[編輯]

基於您的評論,我覺得這個問題是如何避免d重複「菜單」輸出。簡單地重構像這樣使用do..while

cout << " *********************Intensive Program 1***********************\n\n" << endl; 
cout << "\nHere's the menu of choices: "; 
cout << "\n1. Add a Circle.\n"; 
cout << "2. Print a Circle. \n"; 
cout << "3. Print all Cricles \n"; 
cout << "4. Exit.\n"; 
cout << "\nPlease enter your choice: "; 
do 
{ 
    cin >> choice; 
    switch (choice){ 

     case 1: 
      // For adding cirles to the class 
      cout << " Enter the value for the radius of the circle: "; 
      cin >> radius; 
      cout << " Enter the value for the center of the circle: "; 
      cin >> center; 
      myCircle[thisPosition] = new Circle(radius, center); 
      myCircle[thisPosition]->PrintCircle(); 
      thisPosition++; 
      break; 

     case 2: // For printing a particular cirle from the list of cirles 
      cout << " Enter the Value for which Circle to Print: "; 
      cin >> printPosition; 
      myCircle[printPosition - 1]->PrintCircle(); 
      break; 

     case 3: // For printing all the circles in the class object array pointer list 
      cout << "\n"; 
      for (int i = 0; i < thisPosition; i++){ 
       myCircle[i]->PrintCircle(); 
       cout << "\n==============================" << endl; 
      } 
      break; 

     case 4: 
      cout << "\n Good Bye !!! \n " << endl; 
      break; 
     }  
     system("pause"); 
    } 
} while(choice != 4); 

如果你仍然想重複每一個選擇菜單,然後簡單地剪切和粘貼菜單印刷在do..while循環的開始。

作爲一個附註,我強烈建議您在下次閱讀https://mikeash.com/getting_answers.html