开关的替代品

时间:2014-02-11 19:17:32

标签: c++ switch-statement

在下面的代码中,我需要有关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");
    }
}

2 个答案:

答案 0 :(得分:4)

您也可以使用std::vector<std::function<void()>> choices(5); ...并在填写choices[choice]();之类的其他选项之后将其称为choices[1] = &function_1; ...

但我觉得真正的问题不是真的关于如何避免switch ......

[编辑]

根据您的评论,我认为问题是如何避免重复“菜单”输出。使用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

答案 1 :(得分: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");
}