通过减少if语句使代码更有效

时间:2016-08-13 11:38:03

标签: c++

我正在制作食谱分配器程序,它将根据以下内容打印食谱:

  • 食物的香料水平
  • 烹饪所需的时间

我可能会在以后添加更多内容。

#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,代码将根据标签输出响应。

任何想法都表示赞赏。

4 个答案:

答案 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。希望这符合您的目的。
参考

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

答案 2 :(得分: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];

}

答案 3 :(得分:0)

OK!对此最好的解决方案可能是使用2D阵列。

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 ] 

完成!没有if's。