使代码通过减少if语句
问题描述:
我做一个配方分配程序,将根据打印的食谱更高效:使代码通过减少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
,则代码将根据标签输出响应。
任何想法表示赞赏。
答
我认为这段代码应该可以工作。但这个例子非常具体。
#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正确 –
答
我会写这样的:在我看来
#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;
}
答
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的。希望这可以满足你的目的。
参考
答
像这样的东西,我觉得你可以管理大量的辣味和延误:
#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];
}
答
好!最好的解决方案可能是使用二维数组。
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 ]
完成!没有的话。
这是否编译? –
您的代码首先不是正确的。带'or'关键字的if语句总是会计算为true,因为它是'(input ==“10”)or(“10 mins”)',你的意思是'(input ==“10”or input == “10分钟”)' –
'#define或||'或者可能使用具有特定扩展名的编译器? – JVApen