-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path153_3.c
212 lines (206 loc) · 5.66 KB
/
153_3.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct prolific_potion
{
char ing[30];
int qnt;
struct prolific_potion *prev;
struct prolific_potion *next;
} prolific_potion;
typedef struct requiredItems
{
char ing[30];
int qnt;
} required_items;
required_items requirement[5];
int pointer_in_beginning = 0;
int inspect_requirement(prolific_potion *potion)
{
int flag_item_found = 0;
int flag_item_qnt = 0;
for (int i = 0; i < 5; i++)
{
if (strcmp(requirement[i].ing, potion->ing) == 0)
{
flag_item_found = 1;
if (potion->qnt >= requirement[i].qnt)
{
flag_item_qnt = 1;
}
}
}
if (flag_item_found == 1 && flag_item_qnt == 1)
return 1;
else if (flag_item_found == 0)
return 1;
else
return 0;
}
prolific_potion *insert_at_end(prolific_potion *potion)
{
char ing[30];
int qnt;
printf("\nEnter the ing : ");
fflush(stdin);
gets(ing);
fflush(stdin);
printf("\nEnter the qnt of the ing : ");
scanf("%d", &qnt);
prolific_potion *temp = (prolific_potion *)malloc(sizeof(prolific_potion));
strcpy(temp->ing, ing);
temp->qnt = qnt;
temp->prev = NULL;
temp->next = NULL;
if (potion == NULL)
{
if (inspect_requirement(temp))
{
potion = temp;
return potion;
}
}
else
{
if (inspect_requirement(temp))
{
prolific_potion *curr = potion;
while (curr->next != NULL)
{
curr = curr->next;
}
curr->next = temp;
temp->prev = curr;
}
}
return potion;
}
int inspect_items_gather(prolific_potion *potion)
{
if (potion == NULL)
return 0;
prolific_potion *curr = potion;
int count = 0;
while (curr != NULL)
{
char ing[30];
int qnt;
strcpy(ing, curr->ing);
qnt = curr->qnt;
for (int i = 0; i < 5; i++)
{
if (strcmp(ing, requirement[i].ing) == 0 && qnt >= requirement[i].qnt)
count++;
}
curr = curr->next;
}
if (count >= 5)
return 1;
else
return 0;
}
void generatelist()
{
strcpy(requirement[0].ing, "cups of fluxweed");
requirement[0].qnt = 30;
strcpy(requirement[1].ing, "bundles of knotgrass");
requirement[1].qnt = 20;
strcpy(requirement[2].ing, "leeches");
requirement[2].qnt = 42;
strcpy(requirement[3].ing, "Lacewing flies");
requirement[3].qnt = 50;
strcpy(requirement[4].ing, "Bicorn horn");
requirement[4].qnt = 9;
}
prolific_potion *brew_the_potion(prolific_potion *potion)
{
printf("\n");
while (pointer_in_beginning < 5)
{
char ing[30];
strcpy(ing, requirement[pointer_in_beginning].ing);
int qnt = requirement[pointer_in_beginning].qnt;
if (strcmp(ing, potion->ing) == 0)
{
printf("\nItem added for brewing : %s .", ing);
printf("\nqnt left : %d .", potion->qnt - qnt);
if (qnt == potion->qnt && potion->next != NULL)
{
printf("(So the node was deleted.)\n");
prolific_potion *temp = potion;
potion = potion->next;
potion->prev = NULL;
free(temp);
temp = NULL;
}
else if (qnt == potion->qnt && potion->next == NULL)
{
prolific_potion *temp = potion;
potion = NULL;
free(temp);
temp = NULL;
printf("(So the node was deleted.)");
}
else
{
potion->qnt = (potion->qnt) - qnt;
printf("(So the node was updated.)\n");
}
}
else
{
prolific_potion *current = potion;
prolific_potion *previous = NULL;
while (current != NULL)
{
if (strcmp(ing, current->ing) == 0)
{
printf("\nItem added for brewing : %s .", ing);
printf("\nqnt left : %d .", current->qnt - qnt);
if (qnt == current->qnt)
{
printf("(So the node was deleted.)\n");
prolific_potion *temp = current;
if (current->next != NULL)
{
previous->next = current->next;
current->next->prev = previous;
free(temp);
temp = NULL;
}
else
{
previous->next = NULL;
free(current);
current = NULL;
break;
}
}
else
{
current->qnt = (current->qnt) - qnt;
printf("(So the node was updated.)\n");
}
}
previous = current;
current = current->next;
}
}
pointer_in_beginning = pointer_in_beginning + 1;
}
return potion;
}
int main()
{
generatelist();
prolific_potion *potion = NULL;
while (1)
{
if (inspect_items_gather(potion))
break;
else
potion = insert_at_end(potion);
}
potion = brew_the_potion(potion);
printf("\nThe POTION is ready, The dark army can now be defeated again!\n");
}