Advertisement
kamiakze

recipes

Mar 27th, 2023
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. struct Recipe {
  10. string name;
  11. vector<string> ingredients;
  12. string directions;
  13. };
  14.  
  15. // Function to read a recipe from a file
  16. Recipe read_recipe(string filename) {
  17. ifstream file(filename);
  18. Recipe recipe;
  19. if (file.is_open()) {
  20. getline(file, recipe.name);
  21. string ingredient;
  22. while (getline(file, ingredient)) {
  23. recipe.ingredients.push_back(ingredient);
  24. }
  25. getline(file, recipe.directions, '\0');
  26. file.close();
  27. } else {
  28. cerr << "Error: could not open file '" << filename << "'\n";
  29. }
  30. return recipe;
  31. }
  32.  
  33. // Function to write a recipe to a file
  34. void write_recipe(string filename, Recipe recipe) {
  35. ofstream file(filename);
  36. if (file.is_open()) {
  37. file << recipe.name << '\n';
  38. for (string ingredient : recipe.ingredients) {
  39. file << ingredient << '\n';
  40. }
  41. file << recipe.directions;
  42. file.close();
  43. } else {
  44. cerr << "Error: could not open file '" << filename << "'\n";
  45. }
  46. }
  47.  
  48. // Function to search for recipes containing a list of ingredients
  49. vector<string> search_recipes(vector<string> ingredients, vector<string> filenames) {
  50. vector<string> results;
  51. for (string filename : filenames) {
  52. Recipe recipe = read_recipe(filename);
  53. bool found = true;
  54. for (string ingredient : ingredients) {
  55. if (find(recipe.ingredients.begin(), recipe.ingredients.end(), ingredient) == recipe.ingredients.end()) {
  56. found = false;
  57. break;
  58. }
  59. }
  60. if (found) {
  61. results.push_back(recipe.name);
  62. }
  63. }
  64. return results;
  65. }
  66.  
  67. int main() {
  68. // Read recipe files
  69. vector<string> filenames = {"recipe1.txt", "recipe2.txt", "recipe3.txt"};
  70. vector<Recipe> recipes;
  71. for (string filename : filenames) {
  72. recipes.push_back(read_recipe(filename));
  73. }
  74.  
  75. // Write new recipe
  76. Recipe new_recipe;
  77. new_recipe.name = "Pizza";
  78. new_recipe.ingredients = {"Dough", "Tomato sauce", "Mozzarella cheese", "Pepperoni"};
  79. new_recipe.directions = "1. Preheat oven to 425 degrees F.\n2. Roll dough into a circle.\n3. Spread tomato sauce on dough.\n4. Sprinkle cheese and toppings on pizza.\n5. Bake for 10-15 minutes.\n";
  80. write_recipe("recipe4.txt", new_recipe);
  81.  
  82. // Search for recipes containing certain ingredients
  83. vector<string> ingredients = {"Cheese", "Pepperoni"};
  84. vector<string> results = search_recipes(ingredients, filenames);
  85. if (results.empty()) {
  86. cout << "No recipes found containing " << ingredients[0] << " and " << ingredients[1] << endl;
  87. } else {
  88. cout << "Recipes containing " << ingredients[0] << " and " << ingredients[1] << ":\n";
  89. for (string result : results) {
  90. cout << "- " << result << endl;
  91. }
  92. }
  93.  
  94. return 0;
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement