2014-02-11 117 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