forked from khoahd7621/Workshop-Assigment-PRF192
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Workshop7-Program3.c
246 lines (227 loc) · 5.78 KB
/
Workshop7-Program3.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
//Develop a C-program that allows user:
//– Adding a new soft drink
//– Printing out items which belong to a known make.
//– Printing out items whose volumes are between v1 and v2 ( integers)
//– Printing the list in ascending order based on volumes then prices
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXN 100
#define MAXL 20
int menu()
{
int choice;
printf("\n==================================== M E N U ======================================");
printf("\n| 1- Add a new soft drink. Press: 1 |");
printf("\n| 2- Printing out items which belong to a same origin. (Country) Press: 2 |");
printf("\n| 3- Printing out items whose volumes are between [(ml)__and__(ml)]. Press: 3 |");
printf("\n| 4- Printing the list in ascending order based on volumes then prices. Press: 4 |");
printf("\n| 5- Quit. Press: 5 |");
printf("\n===================================================================================\n");
printf("\nEnter Your Choice: ");
scanf("%d", &choice);
fflush(stdin);
return choice;
}
int isFull(int n)
{
return n == MAXN;
}
int isEmpty(int n)
{
return n == 0;
}
char* lTrim(char s[])
{
int i = 0;
while (s[i] == ' ') i++;
if (i > 0) strcpy(&s[0], &s[i]);
return s;
}
char* rTrim(char s[])
{
int i = strlen(s)-1;
while (s[i] == ' ') i--;
s[i+1] = '\0';
return s;
}
char* trim(char s[])
{
rTrim(lTrim(s));
char *ptr = strstr(s, " ");
while (ptr != NULL)
{
strcpy(ptr, ptr+1);
ptr = strstr(s, " ");
}
return s;
}
char* nameStr (char s[])
{
trim(s);
strlwr(s);
int L = strlen(s);
int i;
for (i = 0; i < L; i++)
if (i == 0 || (i > 0 && s[i-1] == ' '))
s[i] = toupper(s[i]);
return s;
}
void addSoftDrink(char name[][MAXL], char make[][MAXL], int volume[], int price[], int duration[], int *pn)
{
char names[MAXL], makes[MAXL];
int volumes, prices, durations;
printf("\n--Enter Information Of Soft Drinks--\n");
printf("Name: ");
scanf("%[^\n]", names);
fflush(stdin);
printf("Made in: ");
scanf("%[^\n]", makes);
fflush(stdin);
printf("Volume (ml): ");
scanf("%d", &volumes);
printf("Price (VND): ");
scanf("%d", &prices);
printf("Duration (Days): ");
scanf("%d", &durations);
printf("\nAdded!\n");
nameStr(names);
nameStr(makes);
strcpy(name[*pn], names);
strcpy(make[*pn], makes);
volume[*pn] = volumes;
price[*pn] = prices;
duration[*pn] = durations;
(*pn)++;
}
void printList(char name[][MAXL], char make[][MAXL], int volume[], int price[], int duration[], int n)
{
printf("\nName: %s\n", name[n]);
printf("Made in: %s\n", make[n]);
printf("Volume: %d ml\n", volume[n]);
printf("Price: %d VND\n", price[n]);
printf("Duration: %d Days\n", duration[n]);
}
void printBaseMake(char name[][MAXL], char make[][MAXL], int volume[], int price[], int duration[], int n)
{
char makes[MAXL];
printf("\nEnter origin of soft drink you wanna print: ");
scanf("%[^\n]", makes);
fflush(stdin);
nameStr(makes);
int i, check;
check = 0;
for (i = 0; i < n; i++)
{
if (strcmp(makes, make[i]) == 0)
{
printf("\n>>List of Soft Drink base on %s:\n", make[i]);
printList(name, make, volume, price, duration, i);
check = 1;
}
}
if (check == 0)
printf("\nThere are not any soft drinks on the list based on %s!!!\n", makes);
}
void printBaseVol(char name[][MAXL], char make[][MAXL], int volume[], int price[], int duration[], int n)
{
int maxVol, minVol;
printf("\nEnter the MIN and MAX mil of soft drinks you wanna print: \n");
printf("Min (ml): ");
scanf("%d", &minVol);
printf("Max (ml): ");
scanf("%d", &maxVol);
int i, check = 0;
for (i = 0; i <= n; i++)
if ((volume[i] >= minVol) && (volume[i] <= maxVol))
{
printList(name, make, volume, price, duration, i);
check = 1;
}
if (check == 0)
printf("\nThere are not any soft drinks between %d ml and %d ml!!!\n", minVol, maxVol);
}
void printAsc(char name[][MAXL], char make[][MAXL], int volume[], int price[], int duration[], int *pn)
{
int i, j;
for (i = 0; i < (*pn)-1; i++)
{
for (j = (*pn)-1; j > i; j--)
{
if (price[j]+duration[j] < price[j-1]+duration[j-1])
{
char transName[MAXL];
strcpy(transName, name[j-1]);
strcpy(name[j-1], name[j]);
strcpy(name[j], transName);
char transMake[MAXL];
strcpy(transMake, make[j-1]);
strcpy(make[j-1], make[j]);
strcpy(make[j], transMake);
int transVol;
transVol = volume[j-1];
volume[j-1] = volume[j];
volume[j] = transVol;
int transPri;
transPri = price[j-1];
price[j-1] = price[j];
price[j] = transPri;
int transDur;
transDur = duration[j-1];
duration[j-1] = duration[j];
duration[j] = transDur;
}
}
}
for (i = 0; i < *pn; i++)
printList(name, make, volume, price, duration, i);
}
int main()
{
int userChoice, check = 1;
char name[MAXN][MAXL];
char make[MAXN][MAXL];
int volume[MAXN];
int price[MAXN];
int duration[MAXN];
int n = 0;
do
{
userChoice = menu();
switch(userChoice)
{
case 1:
if (isFull(n))
printf("\nSorry! The List is full!\n");
else
addSoftDrink(name, make, volume, price, duration, &n);
break;
case 2:
if (isEmpty(n))
printf("\nSorry! The List is empty!\n");
else
printBaseMake(name, make, volume, price, duration, n);
break;
case 3:
if (isEmpty(n))
printf("\nSorry! The List is empty!\n");
else
printBaseVol(name, make, volume, price, duration, n);
break;
case 4:
if (isEmpty(n))
printf("\nSorry! The List is empty!\n");
else
printAsc(name, make, volume, price, duration, &n);
break;
default:
if (userChoice == 5)
check = 0;
else
printf("\n>>Wrong Input!!!\n");
}
}
while (check == 1);
printf("\nGood Bye!");
getch();
}