0% found this document useful (0 votes)
37 views

Subject: PRF192-PFC Workshop 07: Problem 1: Managing A List of Student Names

This document provides code and instructions for managing student and employee data using parallel arrays in C. For students, it includes functions to add, remove, search, and print a list of names. For employees, it stores code, name, salary, and allowance in parallel arrays and allows adding, searching by name, removing by code, and printing in descending order of total pay. The code demonstrates techniques like string manipulation, parallel arrays, and menus to manage lists.

Uploaded by

Dang Khoa Phung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Subject: PRF192-PFC Workshop 07: Problem 1: Managing A List of Student Names

This document provides code and instructions for managing student and employee data using parallel arrays in C. For students, it includes functions to add, remove, search, and print a list of names. For employees, it stores code, name, salary, and allowance in parallel arrays and allows adding, searching by name, removing by code, and printing in descending order of total pay. The code demonstrates techniques like string manipulation, parallel arrays, and menus to manage lists.

Uploaded by

Dang Khoa Phung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Subject: PRF192- PFC

Workshop 07
Objectives:
Managing strings
Managing parallel arrays

Problem 1: (3 marks) Managing a list of student names

Write a C-program that helps user managing a list of 100 student names using the
following menu:
1- Add a student
2- Remove a student
3- Search a student
4- Print the list in ascending order
5- Quit

Criteria
- Names stored in uppercase, all extra blanks in a name will be removed by code.

Recommendation

Refer to algorithms on string in the lecture’s slide

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_VALUE 100

int menu()
{
int choice;
printf("\n=======================MENU=======================");
printf("\n| 1- Add a student Press: 1 |");
printf("\n| 2- Remove a student Press: 2 |");
printf("\n| 3- Search a student Press: 3 |");
printf("\n| 4- Print the list in ascending 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 == MAX_VALUE;
}

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

void addStudent(char names[][31], int *pn)


{
char name[31];
int i, exist;
do
{
printf("\nEnter name of student: ");
scanf("%[^\n]", name);
fflush(stdin);
strupr(name);
trim(name);
exist = 1;
for (i = 0; i < *pn; i++)
if (strcmp(name, names[i]) == 0)
{
printf("\nName of student has exist! Enter again!\n");
exist = 0;
i = *pn - 1;
}
}
while (exist == 0);
strcpy(names[*pn], name);
(*pn)++;
}

void removeStudent(char names[][31], int *pn)


{
printf("\nList of student: \n");
int i, j;
for (i = 0; i < *pn; i++)
printf("[%d]Name: %s\n", i, names[i]);
int remove;
printf("\nEnter number of student you wanna remove: ");
scanf("%d", &remove);
if (remove >= 0 && remove < *pn)
{
for (j = remove + 1; j < *pn; j++)
strcpy(names[j-1], names[j]);
printf("\nRemoved!!!\n");
(*pn)--;
}
else
printf("\nUnremove!!!\n");
}

void searchStudent(char names[][31], int n)


{
char name[31];
printf("\nEnter name of student you wanna searh: ");
scanf("%[^\n]", name);
fflush(stdin);
strupr(name);
int i, x;
int check = 0;
for (i = 0; i < n; i++)
{
x = strcmp(names[i], name);
if (x == 0)
{
printf("\nFound it!\n");
printf("\nResult: Name[%d]: %s\n", i, names[i]);
check = 1;
}
}
if (check == 0)
printf("\nNot found!\n");
}

void printStudent(char names[][31], int *pn)


{
int i, j;
for (i = 0; i < *pn-1; i++)
for (j = *pn-1; j > i; j--)
if (strcmp(names[j] , names[j-1]) < 0)
{
char trans[31];
strcpy(trans, names[j]);
strcpy(names[j], names[j-1]);
strcpy(names[j-1], trans);
}
for (i = 0; i < (*pn); i++)
printf("Name[%d]: %s \n", i, names[i]);
}

int main()
{
int userChoice, check = 1;
char names[MAX_VALUE][31];
int n = 0;
do
{
userChoice = menu();
switch(userChoice)
{
case 1:
if (isFull(n))
printf("\nSorry! The List is full!\n");
else
addStudent(names, &n);
printf("\nAdded!\n");
break;
case 2:
if (isEmpty(n))
printf("\nSorry! The List is empty!\n");
else
removeStudent(names, &n);
break;
case 3:
if (isEmpty(n))
printf("\nSorry! The List is empty!\n");
else
searchStudent(names, n);
break;
case 4:
if (isEmpty(n))
printf("\nSorry! The List is empty!\n");
else
printStudent(names, &n);
break;
default:
if (userChoice == 5)
check = 0;
else
printf("\n>>>Wrong input!!!!\n");
break;
}
}
while (check == 1);
printf("\nGood Bye!");
getchar();
}

Problem 2: (3 marks) Managing a parallel arrays

• Data about an employee: Code(char 8), name (char 20), salary(double),


allowance(double)
• Develop a C-program that allows user:
– Adding a new employee
– Find data about employees using a name inputted.
– Remove an employee based on a code inputted
– Print the list in descending order based on salary + allowance.

( Helps for this problem are introduced in the lecture’s slide)


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_VALUE 100

int menu()
{
int choice;
printf("\n=======================MENU=======================");
printf("\n| 1- Add a new employee Press: 1 |");
printf("\n| 2- Search a data of employee Press: 2 |");
printf("\n| 3- Remove an employee Press: 3 |");
printf("\n| 4- Print the list in descending order 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 *pn)


{
return *pn == MAX_VALUE;
}

int isEmpty(int *pn)


{
return *pn == 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 addEmployee(char code[][9], char name[][21], int salaries[], int allowances[], int*pn)
{
char codes[9], names[21];
int salary, allowance, totals = 0;
printf("\n!--Please Enter Employee Information--!\n");
fflush(stdin);
printf("Employee Code: ");
scanf("%[^\n]", codes);
fflush(stdin);
printf("Employee Name: ");
scanf("%[^\n]", names);
printf("Employee Salary (VND): ");
scanf("%d", &salary);
printf("Employee Allowances(VND): ");
scanf("%d", &allowance);
trim(codes);
nameStr(names);
strcpy(code[*pn], codes);
strcpy(name[*pn], names);
salaries[*pn] = salary;
allowances[*pn] = allowance;
(*pn)++;
}

void printList(char code[][9], char name[][21], int salaries[], int allowances[], int n)
{
printf("\nCode: %s\n", code[n]);
printf("Name: %s\n", name[n]);
printf("Salary: %d VND\n", salaries[n]);
printf("Allowances: %d VND\n", allowances[n]);
}

void printBasedName(char code[][9], char name[][21], int salaries[], int allowances[], int
n)
{
char names[21];
printf("\nEnter NAME of employee you wanna find the information: ");
scanf("%[^\n]", names);
nameStr(names);
int check, i;
check = 0;
for (i = 0; i < n; i++)
{
if (strcmp(names, name[i]) == 0)
{
printf("\n-- Information of Employee --\n");
printList(code, name, salaries, allowances, i);
check = 1;
}
}
if (check == 0)
printf("\nNot found information of employee was entered!\n");
}

void removeEmployee(char code[][9], char name[][21], int salaries[], int allowances[],


int*pn)
{
int i;
char codes[9];
printf("Enter CODE of employee you wanna REMOVE: ");
scanf("%[^\n]", codes);
trim(codes);
int check = 0;
for (i = 0; i < *pn; i++)
{
if (strcmp(code[i], codes) == 0)
{
int j;
for (j = i + 1; j < *pn; j++)
{
strcpy(code[j-1], code[j]);
strcpy(name[j-1], name[j]);
salaries[j-1] = salaries[j];
allowances[j-1] = allowances[j];
}
check = 1;
(*pn)--;
}
}
if (check == 1)
printf("\nRemoved!\n");
else
printf("\nUnremove!\n");
}

void printDec(char code[MAX_VALUE][9], char name[][21], int salaries[], int


allowances[], int*pn)
{
int i, j;
for (i = 0; i < *pn-1; i++)
for (j = *pn-1; j > i; j--)
{
if ((salaries[j] + allowances[j]) > (salaries[j-1] + allowances[j-1]))
{
char trscode[9];
strcpy(trscode, code[j]);
strcpy(code[j], code[j-1]);
strcpy(code[j-1], trscode);

char trsname[21];
strcpy(trsname, name[j]);
strcpy(name[j], name[j-1]);
strcpy(name[j-1], trsname);

int trssa;
trssa = salaries[j];
salaries[j] = salaries[j-1];
salaries[j-1] = trssa;

int trsall;
trsall = allowances[j];
allowances[j] = allowances[j-1];
allowances[j-1] = trsall;
}
}
printf("\n---List of employees---\n");
for (i = 0; i < *pn; i++)
{
printList(code, name, salaries, allowances, i);
printf("\n");
}
}

int main()
{
int userChoice, check = 1;
char code[MAX_VALUE][9];
char name[MAX_VALUE][21];
int salaries[MAX_VALUE];
int allowances[MAX_VALUE];
int n = 0;
do
{
userChoice = menu();
switch(userChoice)
{
case 1:
if (isFull(&n))
printf("\nSorry! The List is full!\n");
else
addEmployee(code, name, salaries, allowances, &n);

printf("\nAdded!\n");
break;
case 2:
if (isEmpty(&n))
printf("\nSorry! The List is empty!\n");
else
printBasedName(code, name, salaries, allowances,
n);
break;
case 3:
if (isEmpty(&n))
printf("\nSorry! The List is empty!\n");
else
removeEmployee(code, name, salaries, allowances,
&n);
break;
case 4:
if (isEmpty(&n))
printf("\nSorry! The List is empty!\n");
else
printDec(code, name, salaries, allowances, &n);
break;
default:
if (userChoice == 5)
check = 0;
else
printf("\n>>>Wrong input!!!!\n");
break;
}
}
while (check == 1);
printf("\nGood Bye!");
getchar();
}

Problem 3: (4 marks) Managing a parallel arrays

• Data about a soft drink: name (char 20), make(char 20), volume (int), price(int),
duration (int- number of days when this product can be drunk)
• 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!");
Getchar();
}

You might also like