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

3

我正在制作一个食谱分发程序,它将根据以下因素打印食谱:

  • 食物的辣度
  • 烹饪所需时间

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

#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,然后代码将基于标签输出响应。

欢迎提出任何想法。


这个能编译吗? - Awais Chishti
你的代码一开始就不正确。带有'or'关键字的if语句将始终评估为true,因为它是(input == "10") or ("10 mins")。你的意思是(input == "10" or input == "10 mins") - adnan_e
1
#define or || 或者使用一个具有特定扩展的编译器? - JVApen
将食谱类型从字符串更改为map<pair<string,int>,string>。 - Ilaya Raja S
2
还有比这更快的吗?我不这么认为,但是也许你可以通过使用一些数据结构,如map来提高可读性和可扩展性。 - adnan_e
显示剩余10条评论
5个回答

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的一种非常优雅的使用方式。希望这能满足您的需求。
参考资料:

兄弟,太棒了,这个可行。最后,我应该研究什么才能真正理解它是如何工作的或者你所做的事情呢? - user6439666
1
cout<<recipe[input][inp];如果用户输入“cold”,则这里存在一个小问题,即会向映射中添加空条目。 - PaulMcKenzie
@PaulMcKenzie 让我来修复它。 - Ilaya Raja S

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

我猜recipe1等只是占位符。 - Ilaya Raja S

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

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 ] 

完成了!没有if语句。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接