2010-10-09 65 views
1

//我的程序要長,並有錯誤..我想使它簡單易於閱讀..如何改變它在切換程序?請幫幫我!! =(如果其他程序切換程序

#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; 

} 
+5

顯示兩部不好的電影,只顯示鋼鐵俠,你會砍掉2/3的代碼。 – 2010-10-09 06:16:34

+0

@詹姆斯麥克奈利斯,但是...我喜歡哈利波特.. :) – liaK 2010-10-09 06:18:28

+1

更嚴重的是,你需要小心你的輸入操作。從流中提取可能會失敗,並且您需要檢查並處理失敗。例如,如果用戶爲他的電影選項(「hello」不是有效整數)輸入「hello」,則將選擇變量保持未初始化狀態,並且將設置流中的失敗狀態。你可以使用'if(!cin){/ * handle error * /}'來測試流狀態以查看提取是否成功。你還必須調用'cin.clear()'來清除錯誤狀態並使用'cin.ignore()'來忽略無效輸入。 – 2010-10-09 06:23:28

回答

4

作爲一個經驗法則,你發現自己複製和粘貼代碼超過一兩行的任何時候,你應該停下來想一想,是否有清潔重構代碼到一個函數。

在這種情況下,您可以創建一個函數do_movie_specific_stuff(),它以三個電影之間不同的數據爲參數(如果在三個開關之間有任何不同的情況),那麼switch語句中的每個case都是單個聲明:用正確的參數調用該函數。

+1

我強烈懷疑這個人的班級尚未學習編寫自定義功能。早期,函數只能被調用,而不是寫入,而'int main()'仍然只是一個魔法咒語,它在程序的頂部。 – 2010-10-09 16:08:11

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) 

如果您需要跟蹤的是什麼電影的用戶已經試過了,你可以做到這一點,你只需要跟蹤某個地方,並在邏輯中處理它。

0

你所顯示的代碼是純粹線性代碼問題的一個很好的例子:它只是不起作用。

有一個小竅門:我要麼創建三個函數來更好處理每部電影,或者創建一個通用的函數來處理所有的電影,這將需要一些類的數據:如果停止

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