if else程序切换程序

时间:2010-10-09 06:12:14

标签: c++ c++builder

//我的程序很长并且有错误..我想让它简单易读..如何在开关程序中更改它?请帮帮我!! =(

#include <iostream>

using namespace std;

int main ()
{
    int movie1;
    int movie2;
    int movie3;
    int seats;
    int price;
    int select;
    char response;

    cout<<"______Wellcome to Strawberry Gold Cinema____\n"
        <<"Now you are booking a ticket cinema and please choose your movie... ";
        cout << "\nPress 1 for= Harry Potter \n"
            << "Press 2 for = Iron Man\n"
            <<"Press 3 for = Romeo and Juliet\n";
        cout<<"Enter your choice > ";
        cin>>select;


 if (select ==1 )

 {


     cout<<"The price of the ticket per seat is RM10.00\n"
        <<"Please enter the number of seat ";
        cin>>seats;


        if (seats<=30)

        {
            price = 10 * seats;

            cout<<"The total price is RM"<<price<<endl;
        }

        else if (seats>=30)

        {
            cout<<"The movie is full.\n"
            <<"Would like to choose another movie??\n"
            <<"Section Y or N \n";
            cout<<"Enter your choice = ";
            cin>>response;
        }



        if ( toupper( response ) == 'Y' )
        {
        cout <<"Please choose movie 2 or movie 3\n"
        <<"Enter your choice ";
        cin>>select;




        if (select == 2)
        {
         cout<<"The price of the ticket per seat is RM11.00\n"
           <<"Please enter the number of seat";
        cin>>seats;


        if (seats<=30)

        {
            price = 11 * seats;

            cout<<"The total price is RM"<<price<<endl;
        }

        }
        else if (seats>=30)

        {
            cout<<"The movie is full.\n"
            <<"Would like to choose another movie??\n"
            <<"Section Y or N \n";
            cout<<"Enter your choice = ";
            cin>>response;

        }
            if ( toupper( response ) == 'Y' )


            {
             cout<<"The price of the ticket per seat is RM13.00\n"
             <<"Please enter the number of seat";
            cin>>seats;


        if (seats<=30)

        {
            price = 13 * seats;

            cout<<"The total price is RM"<<price<<endl;
        }

        else if (seats>=30)

        {
            cout<<"The movie is full.\n"
            <<"Next movie in 5 hours...."<<endl;
        }

else
        cout << "Next movie in 5 hours.\n";
        }
        }
        }

//for 2

 if (select ==2 )

 {


     cout<<"The price of the ticket per seat is RM11.00\n"
        <<"Please enter the number of seat";
        cin>>seats;


        if (seats<=30)

        {
            price = 11 * seats;

            cout<<"The total price is RM"<<price<<endl;
        }

        else if (seats>=30)

        {
            cout<<"The movie is full.\n"
            <<"Would like to choose another movie??\n"
            <<"Section Y or N \n";
            cout<<"Enter your choice = ";
            cin>>response;
        }



        if ( toupper( response ) == 'Y' )
        {
        cout <<"Please choose movie 1 or movie 3\n"
        <<"Enter your choice ";
        cin>>select;




        if (select == 2)
        {
         cout<<"The price of the ticket per seat is RM10.00"
           <<"Please enter the number of seat";
        cin>>seats;


        if (seats<=30)

        {
            price = 10 * seats;

            cout<<"The total price is RM"<<price<<endl;
        }

        }
        else if (seats>=30)

        {
            cout<<"The movie is full.\n"
            <<"Would like to choose another movie??\n"
            <<"Section Y or N \n";
            cout<<"Enter your choice = ";
            cin>>response;

        }
            if ( toupper( response ) == 'Y' )


            {
             cout<<"The price of the ticket per seat is RM13.00\n"
             <<"Please enter the number of seat";
            cin>>seats;


        if (seats<=30)

        {
            price = 13 * seats;

            cout<<"The total price is RM"<<price<<endl;
        }

        else if (seats>=30)

        {
            cout<<"The movie is full.\n"
            <<"Next movie in 5 hours...."<<endl;
        }
else
        cout << "Next movie in 5 hours.\n";

        }
        }
        }



//for seat 3

 if (select ==3 )

 {


     cout<<"The price of the ticket per seat is RM13.00\n"
        <<"Please enter the number of seat";
        cin>>seats;


        if (seats<=30)

        {
            price = 13 * seats;

            cout<<"The total price is RM"<<price<<endl;
        }

        else if (seats>=30)

        {
            cout<<"The movie is full.\n"
            <<"Would like to choose another movie??\n"
            <<"Section Y or N \n";
            cout<<"Enter your choice = ";
            cin>>response;
        }



        if ( toupper( response ) == 'Y' )
        {
        cout <<"Please choose movie 1 or movie 2\n"
        <<"Enter your choice ";
        cin>>select;




        if (select == 1)
        {
         cout<<"The price of the ticket per seat is RM10.00\n"
           <<"Please enter the number of seat";
        cin>>seats;


        if (seats<=30)

        {
            price = 10 * seats;

            cout<<"The total price is RM"<<price<<endl;
        }

        }
        else if (seats>=30)

        {
            cout<<"The movie is full.\n"
            <<"Would like to choose another movie??\n"
            <<"Section Y or N \n";
            cout<<"Enter your choice = ";
            cin>>response;

        }
            if ( toupper( response ) == 'Y' )


            {
             cout<<"The price of the ticket per seat is RM12.00\n"
             <<"Please enter the number of seat";
            cin>>seats;


        if (seats<=30)

        {
            price = 12 * seats;

            cout<<"The total price is RM"<<price<<endl;
        }

        else if (seats>=30)

        {
            cout<<"The movie is full.\n"
            <<"Next movie in 5 hours...."<<endl;
        }
         else
        cout << "Next movie in 5 hours.\n";
        }
        }
        }






 return 0;

}

3 个答案:

答案 0 :(得分:4)

根据经验,每当您发现自己复制并粘贴一两行代码时,您应该停下来思考将该代码重构为函数是否更清晰。

在这种情况下,您可以创建一个函数do_movie_specific_stuff(),该函数将三个电影之间的数据作为参数(如果三个切换案例之间存在任何不同)。然后switch语句中的每个case都是一个语句:用正确的参数调用该函数。

答案 1 :(得分:1)

如果您熟悉的话,我建议将您的逻辑放入循环并将逻辑分解为函数或可能的类。像这样的东西(这是非编译/未经测试的半伪代码......只是为了给你一个想法):

bool done = false;
do
{
    cout << "Please choose a move (1, 2, or 3)" << endl;
    cin >> movieId;

    double price = getMoviePrice(movieId);
    cout << "The price of the movie is: " + price << endl;

    cout << "How many seats" << endl;
    cin >> seats;

    int availableSeats = getAvailableSeats(movieId);

    // and so on...  If the user indicates they want to quit, just set done to true!

} while (!done)

如果你需要跟踪用户已尝试过的电影,你可以这样做,你只需要在某处跟踪它,并在逻辑中处理它。

答案 2 :(得分:0)

您展示的代码是纯线性代码问题的一个很好的例子:它只是不起作用。

作为提示:我要么创建三个函数来处理每个电影,或者更好的是,创建一个通用函数来处理所有电影,这将从某些类中获取其数据:

class Movie {
  std::wstring name;
  int seats;
  int soldSeats;
  int pricePerSeat;
};