forked from MAYANK25402/Hactober-2023-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecipe_assistant.c
168 lines (148 loc) · 5.62 KB
/
recipe_assistant.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_INGREDIENTS 10
struct Ingredient {
char name[50];
struct Ingredient* next;
};
struct Recipe {
char name[50];
struct Ingredient* ingredients;
char instructions[500];
struct Recipe* next;
};
struct RecipeAssistant {
struct Recipe* recipes;
};
// Function to create a new ingredient
struct Ingredient* create_ingredient(const char* name) {
struct Ingredient* ingredient = (struct Ingredient*)malloc(sizeof(struct Ingredient));
if (ingredient == NULL) {
perror("Memory allocation error");
exit(1);
}
strncpy(ingredient->name, name, sizeof(ingredient->name));
ingredient->next = NULL;
return ingredient;
}
// Function to create a new recipe
struct Recipe* create_recipe(const char* name, const char* instructions) {
struct Recipe* recipe = (struct Recipe*)malloc(sizeof(struct Recipe));
if (recipe == NULL) {
perror("Memory allocation error");
exit(1);
}
strncpy(recipe->name, name, sizeof(recipe->name));
strncpy(recipe->instructions, instructions, sizeof(recipe->instructions));
recipe->ingredients = NULL;
recipe->next = NULL;
return recipe;
}
// Function to add an ingredient to a recipe
void add_ingredient(struct Recipe* recipe, const char* ingredient_name) {
struct Ingredient* ingredient = create_ingredient(ingredient_name);
ingredient->next = recipe->ingredients;
recipe->ingredients = ingredient;
}
// Function to add a recipe to the assistant
void add_recipe(struct RecipeAssistant* assistant, const char* name, const char* instructions) {
struct Recipe* recipe = create_recipe(name, instructions);
recipe->next = assistant->recipes;
assistant->recipes = recipe;
}
// Function to find and return a recipe by name
struct Recipe* find_recipe(struct RecipeAssistant* assistant, const char* name) {
struct Recipe* current = assistant->recipes;
while (current != NULL) {
if (strcmp(current->name, name) == 0) {
return current;
}
current = current->next;
}
return NULL;
}
// Function to list all recipes
void list_recipes(struct RecipeAssistant* assistant) {
struct Recipe* current = assistant->recipes;
while (current != NULL) {
printf("Name: %s\n", current->name);
printf("Ingredients: ");
struct Ingredient* current_ingredient = current->ingredients;
while (current_ingredient != NULL) {
printf("%s, ", current_ingredient->name);
current_ingredient = current_ingredient->next;
}
printf("\nInstructions: %s\n", current->instructions);
current = current->next;
}
}
int main() {
struct RecipeAssistant assistant;
assistant.recipes = NULL;
char user_input[100];
while (1) {
printf("\nMenu:\n");
printf("1. Add Recipe\n");
printf("2. Search Recipe\n");
printf("3. List Recipes\n");
printf("4. Quit\n");
printf("Select an option (1/2/3/4): ");
fgets(user_input, sizeof(user_input), stdin);
int choice = atoi(user_input);
switch (choice) {
case 1: {
printf("\nEnter Recipe Name: ");
fgets(user_input, sizeof(user_input), stdin);
user_input[strcspn(user_input, "\n")] = '\0'; // Remove newline character
char recipe_name[50];
strncpy(recipe_name, user_input, sizeof(recipe_name));
printf("Enter Instructions: ");
fgets(user_input, sizeof(user_input), stdin);
user_input[strcspn(user_input, "\n")] = '\0'; // Remove newline character
add_recipe(&assistant, recipe_name, user_input);
printf("\nEnter Ingredients (enter 'done' to finish adding ingredients):\n");
while (1) {
fgets(user_input, sizeof(user_input), stdin);
user_input[strcspn(user_input, "\n")] = '\0'; // Remove newline character
if (strcmp(user_input, "done") == 0) {
break;
}
add_ingredient(assistant.recipes, user_input);
}
printf("Recipe added successfully!\n");
break;
}
case 2: {
printf("\nEnter Recipe Name to Search: ");
fgets(user_input, sizeof(user_input), stdin);
user_input[strcspn(user_input, "\n")] = '\0'; // Remove newline character
struct Recipe* found_recipe = find_recipe(&assistant, user_input);
if (found_recipe != NULL) {
printf("\nRecipe for %s:\n", found_recipe->name);
printf("Ingredients: ");
struct Ingredient* current_ingredient = found_recipe->ingredients;
while (current_ingredient != NULL) {
printf("%s, ", current_ingredient->name);
current_ingredient = current_ingredient->next;
}
printf("\nInstructions: %s\n", found_recipe->instructions);
} else {
printf("\nRecipe for %s not found.\n", user_input);
}
break;
}
case 3:
printf("\nAvailable recipes:\n");
list_recipes(&assistant);
break;
case 4:
printf("\nGoodbye!\n");
exit(0);
default:
printf("\nInvalid choice. Please select a valid option.\n");
break;
}
}
return 0;
}