2016-08-13 82 views
3

我做一個配方分配程序,將根據打印的食譜更高效:使代碼通過減少if語句

  • 食品
  • 需要煮的時間的香料水平

我可能會在路上追加更多。

#include <iostream> 
#include <string> 
using namespace std; 

int main() 
{ 
    string input = ""; 
    string recipe1 = "A mild recipe that takes 10 mins"; 
    string recipe2 = "A mild recipe that takes 20 mins"; 
    string recipe3 = "A medium recipe that takes 10 mins"; 
    string recipe4 = "a mild recipe that takes 20 mins"; 

    cout << "Hello to the recipe dispenser 2000" << endl << "I will now begin with some questions to get the perfect recipe for you" << endl << "Do you like your food mild, medium, or hot?" << endl; 
    getline (cin, input); 
    if(input == "mild") 
     cout << "Would you like a recipe that takes 10 or 20 mins?" << endl; 
     getline (cin, input); 
     if(input == "10" or input == "10 mins") 
     cout << recipe1 << endl; 
} 

但是我現在的代碼看起來效率很低,因爲我必須寫出總共6個if語句才能完成代碼。

有什麼辦法可以縮短這個嗎?
例如,通過爲每個配方添加一些標籤或其他東西,如[10, mild]recipe1,則代碼將根據標籤輸出響應。

任何想法表示讚賞。

+0

這是否編譯? –

+0

您的代碼首先不是正確的。帶'or'關鍵字的if語句總是會計算爲true,因爲它是'(input ==「10」)or(「10 mins」)',你的意思是'(input ==「10」or input == 「10分鐘」)' –

+1

'#define或||'或者可能使用具有特定擴展名的編譯器? – JVApen

回答

0

我認爲這段代碼應該可以工作。但這個例子非常具體。

#include <iostream> 
#include <string> 
using namespace std; 

int main() 
{ 
    string input1 = ""; 
    string input2 = ""; 
    string recipe1 = "A mild recipe that takes 10 mins"; 
    string recipe2 = "A mild recipe that takes 20 mins"; 
    string recipe3 = "A medium recipe that takes 10 mins"; 
    string recipe4 = "a mild recipe that takes 20 mins"; 

    cout << "Hello to the recipe dispenser 2000" << endl << "I will now begin with some questions to get the perfect recipe for you" << endl << "Do you like your food mild, medium, or hot?" << endl; 
    getline (cin, input1); 
    cout << "Would you like a recipe that takes 10 or 20 mins?" << endl; 
    getline (cin, input2); 
    cout<< "A " << input1<<" recipe that takes "<<input2<<" mins"<<endl; 
} 
+0

我想recipe1等只是佔位符 –

+0

@IlayaRajaS正確 –

0

我會寫這樣的:在我看來

#include <map> 
#include <string> 
#include <iostream> 
int main() { 
    std::map<std::string, std::string> recipes = {{"10", "A mild recipe that takes 10 mins"}}; 
    std::string input; 
    getline (std::cin, input); 

    try { 
     std::cout << recipes.at(input); 
    } catch(std::out_of_range& e) { std::cout << e.what();} 


    return 0; 
} 
5
int main() 
{ 
    string input = ""; 
    int inp; 
    map< string,map<int,string> > recipe; 
    recipe["mild"][10]="A mild recipe that takes 10 mins"; 
    recipe["mild"][20]= "A mild recipe that takes 20 mins"; 
    recipe["medium"][10]= "A medium recipe that takes 10 mins"; 

    cout << "Hello to the recipe dispenser 2000" << endl << "I will now begin with some questions to get the perfect recipe for you" << endl << "Do you like your food mild, medium, or hot?" << endl; 
    getline (cin, input); 
    cout << "Would you like a recipe that takes 10 or 20 mins?" << endl; 
    cin>>inp; 
    try 
    { 
     cout<<(recipe.at(input)).at(inp); 
    } 
    catch(exception &e) 
    { 
     cerr<<input<<" , "<<inp<<" has not been invented yet!\n"; 
    } 
    return 0; 
} 

漂亮優雅的使用STL的。希望這可以滿足你的目的。
參考

+0

乾杯,這個作品,最後一個筆記,我應該研究什麼實際上了解這是如何工作或你已經做了什麼? –

+1

'cout << recipe [input] [inp];' - 如果用戶輸入「cold」,那麼這裏有一個小問題,將空白條目添加到地圖中。 – PaulMcKenzie

+0

@PaulMcKenzie讓我解決這個問題 –

0

像這樣的東西,我覺得你可以管理大量的辣味和延誤:

#include <iostream> 
#include <string> 
#include <vector> 
#include <algorithm> 

using namespace std; 

size_t index(const string& str, const vector<string>& v) 
{ 
    auto it = find(v.begin(), v.end(), str); 
    if(it != v.end()) { 
     return distance(v.begin(), it); 
    } 
    // else --> not found 
} 

int main() 
{ 

    string input = ""; 
    string recipe[] = {"A mild recipe that takes 10 mins", 
         "A mild recipe that takes 20 mins", 
         "A medium recipe that takes 10 mins", 
         "A medium recipe that takes 20 mins"}; 

    vector<string> spiciness {"mild", "medium", "hot"}; 
    vector<string> delay {"10", "20"}; 

    int spi; 
    int del; 

    cout << "Hello to the recipe dispenser 2000" << endl 
     << "I will now begin with some questions to get the perfect recipe for you" << endl 
     << "Do you like your food mild, medium, or hot?" << endl; 

    getline (cin, input); 
    spi = index(input, spiciness); 

    cout << "Would you like a recipe that takes 10 or 20 mins?" << endl; 
    getline (cin, input); 
    del = index(input, delay); 

    cout << recipe[delay.size()*spi + del]; 

} 
0

好!最好的解決方案可能是使用二維數組。

Store all the items that are both mild & 10 min in array[1], 
Store all the items that are both mild & 20 min in array[2], 
Store all the items that are both medium & 10 min in array[3], 
Store all the items that are both medium & 20 min in array[4], 
Store all the items that are both hot & 10 min in array[5], 
Store all the items that are both hot & 20 min in array[6], 

請輸入:

int input1,input2; 
cout << "Hello to the recipe dispenser 2000" << endl 
    << "I will now begin with some questions to get the perfect recipe for you" << endl 
    << "Do you like your food 1)mild, 2)medium, or 3)hot?" << endl; 
cin>>input1; 
cout << "Would you like a recipe that takes 1)10 or 2)20 mins?" << endl; 
cin>>input2; 
//print array[((input1-1)*2)+input2 ] 

完成!沒有的話。