0% found this document useful (0 votes)
70 views6 pages

Workshop7 Program3.c

The document describes a C program that allows a user to perform the following tasks on a list of soft drinks: 1) Add a new soft drink to the list 2) Print out items from the list that belong to a specific origin/country 3) Print out items whose volumes are within a given range of integers 4) Print the list in ascending order based on volume then price

Uploaded by

Minh Lê Khải
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views6 pages

Workshop7 Program3.c

The document describes a C program that allows a user to perform the following tasks on a list of soft drinks: 1) Add a new soft drink to the list 2) Print out items from the list that belong to a specific origin/country 3) Print out items whose volumes are within a given range of integers 4) Print the list in ascending order based on volume then price

Uploaded by

Minh Lê Khải
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

//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;

m
er as
printf("\n==================================== M
E N U ======================================");

co
eH w
printf("\n| 1- Add a new soft drink. Press: 1
|");

o.
printf("\n| 2- Printing out items which belong to a same origin.
(Country) Press: 2 |"); rs e
ou urc
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
o

prices. Press: 4 |");


aC s

printf("\n| 5- Quit. Press: 5 |");


vi y re

printf("\n=========================================
==========================================\n");
ed d

printf("\nEnter Your Choice: ");


scanf("%d", &choice);
ar stu

fflush(stdin);
return choice;
}
is

int isFull(int n)
Th

{
return n == MAXN;
}
sh

int isEmpty(int n)
{
return n == 0;
}

This study source was downloaded by 100000826901668 from CourseHero.com on 06-30-2021 08:37:51 GMT -05:00

https://www.coursehero.com/file/84158257/Workshop7-Program3c/
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;
}

m
er as
char* trim(char s[])
{

co
eH w
rTrim(lTrim(s));
char *ptr = strstr(s, " ");

o.
while (ptr != NULL)
{ rs e
ou urc
strcpy(ptr, ptr+1);
ptr = strstr(s, " ");
}
o

return s;
aC s

}
vi y re

char* nameStr (char s[])


{
ed d

trim(s);
strlwr(s);
ar stu

int L = strlen(s);
int i;
for (i = 0; i < L; i++)
is

if (i == 0 || (i > 0 && s[i-1] == ' '))


s[i] = toupper(s[i]);
Th

return s;
}
sh

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");

This study source was downloaded by 100000826901668 from CourseHero.com on 06-30-2021 08:37:51 GMT -05:00

https://www.coursehero.com/file/84158257/Workshop7-Program3c/
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);

m
er as
strcpy(make[*pn], makes);
volume[*pn] = volumes;

co
eH w
price[*pn] = prices;
duration[*pn] = durations;

o.
(*pn)++;
} rs e
ou urc
void printList(char name[][MAXL], char make[][MAXL], int volume[], int
price[], int duration[], int n)
o

{
aC s

printf("\nName: %s\n", name[n]);


vi y re

printf("Made in: %s\n", make[n]);


printf("Volume: %d ml\n", volume[n]);
printf("Price: %d VND\n", price[n]);
ed d

printf("Duration: %d Days\n", duration[n]);


}
ar stu

void printBaseMake(char name[][MAXL], char make[][MAXL], int volume[], int


price[], int duration[], int n)
is

{
char makes[MAXL];
Th

printf("\nEnter origin of soft drink you wanna print: ");


scanf("%[^\n]", makes);
fflush(stdin);
sh

nameStr(makes);
int i, check;
check = 0;
for (i = 0; i < n; i++)
{
if (strcmp(makes, make[i]) == 0)

This study source was downloaded by 100000826901668 from CourseHero.com on 06-30-2021 08:37:51 GMT -05:00

https://www.coursehero.com/file/84158257/Workshop7-Program3c/
{
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:

m
er as
\n");
printf("Min (ml): ");

co
eH w
scanf("%d", &minVol);
printf("Max (ml): ");

o.
scanf("%d", &maxVol);
if (minVol > maxVol) rs e
ou urc
{
int trs = maxVol;
maxVol = minVol;
o

minVol = trs;
aC s

}
vi y re

int i, check = 0;
for (i = 0; i <= n; i++)
if ((volume[i] >= minVol) && (volume[i] <= maxVol))
ed d

{
printList(name, make, volume, price, duration, i);
ar stu

check = 1;
}
if (check == 0)
is

printf("\nThere are not any soft drinks between %d ml and %d


ml!!!\n", minVol, maxVol);
Th

void printAsc(char name[][MAXL], char make[][MAXL], int volume[], int


sh

price[], int duration[], int *pn)


{
int i, j;
for (i = 0; i < (*pn)-1; i++)
{
for (j = (*pn)-1; j > i; j--)

This study source was downloaded by 100000826901668 from CourseHero.com on 06-30-2021 08:37:51 GMT -05:00

https://www.coursehero.com/file/84158257/Workshop7-Program3c/
{
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];

m
er as
volume[j] = transVol;

co
eH w
int transPri;
transPri = price[j-1];

o.
price[j-1] = price[j];
rs e price[j] = transPri;
ou urc
int transDur;
transDur = duration[j-1];
o

duration[j-1] = duration[j];
aC s

duration[j] = transDur;
vi y re

}
}
}
ed d

for (i = 0; i < *pn; i++)


printList(name, make, volume, price, duration, i);
ar stu

int main()
is

{
int userChoice, check = 1;
Th

char name[MAXN][MAXL];
char make[MAXN][MAXL];
int volume[MAXN];
sh

int price[MAXN];
int duration[MAXN];
int n = 0;
do
{
userChoice = menu();

This study source was downloaded by 100000826901668 from CourseHero.com on 06-30-2021 08:37:51 GMT -05:00

https://www.coursehero.com/file/84158257/Workshop7-Program3c/
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;

m
er as
case 3:
if (isEmpty(n))

co
eH w
printf("\nSorry! The List is empty!\n");
else

o.
printBaseVol(name, make, volume, price,
duration, n); rs e
ou urc
break;
case 4:
if (isEmpty(n))
o

printf("\nSorry! The List is empty!\n");


aC s

else
vi y re

printAsc(name, make, volume, price, duration,


&n);
break;
ed d

default:
if (userChoice == 5)
ar stu

check = 0;
else
printf("\n>>Wrong Input!!!\n");
is

}
Th

}
while (check == 1);
printf("\nGood Bye!");
sh

getch();
}

This study source was downloaded by 100000826901668 from CourseHero.com on 06-30-2021 08:37:51 GMT -05:00

https://www.coursehero.com/file/84158257/Workshop7-Program3c/
Powered by TCPDF (www.tcpdf.org)

You might also like