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

Workshop7 Program2.c

This C program allows users to manage employee data by adding, searching, removing employees or printing employee lists ordered by salary. It uses arrays to store employee codes, names, salaries and allowances. The program displays a menu for users to choose these options and includes functions for each task like adding data, searching by name, removing by code, and printing lists sorted by total compensation.

Uploaded by

Minh Lê Khải
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Workshop7 Program2.c

This C program allows users to manage employee data by adding, searching, removing employees or printing employee lists ordered by salary. It uses arrays to store employee codes, names, salaries and allowances. The program displays a menu for users to choose these options and includes functions for each task like adding data, searching by name, removing by code, and printing lists sorted by total compensation.

Uploaded by

Minh Lê Khải
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

//Develop a C-program that allows user:

//1- Adding a new employee


//2- Find data about employees using a name inputted.
//3- Remove an employee based on a code inputted
//4- Print the list in descending order based on salary + allowance.
//5- Quit

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

m
printf("\n==================================================\n");

er as
printf("\nEnter Your Choice: ");

co
scanf("%d", &choice);

eH w
fflush(stdin);
return choice;

o.
} rs e
ou urc
int isFull(int *pn)
{
return *pn == MAX_VALUE;
}
o
aC s

int isEmpty(int *pn)


v i y re

{
return *pn == 0;
}

char* lTrim(char s[])


ed d

{
ar stu

int i = 0;
while (s[i] == ' ') i++;
if (i > 0) strcpy(&s[0], &s[i]);
return s;
}
sh is

char* rTrim(char s[])


Th

{
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, " ");
}

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

https://www.coursehero.com/file/84158432/Workshop7-Program2c/
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;
printf("\n!--Please Enter Employee Information--!\n");
fflush(stdin);

m
printf("Employee Code: ");

er as
scanf("%[^\n]", codes);

co
fflush(stdin);

eH w
printf("Employee Name: ");
scanf("%[^\n]", names);

o.
printf("Employee Salary (VND): ");
rs e
scanf("%d", &salary);
ou urc
printf("Employee Allowances(VND): ");
scanf("%d", &allowance);
trim(codes);
nameStr(names);
o

strcpy(code[*pn], codes);
aC s

strcpy(name[*pn], names);
v i y re

salaries[*pn] = salary;
allowances[*pn] = allowance;
(*pn)++;
}
ed d

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


ar stu

allowances[], int n)
{
printf("\nCode: %s\n", code[n]);
printf("Name: %s\n", name[n]);
printf("Salary: %d VND\n", salaries[n]);
sh is

printf("Allowances: %d VND\n", allowances[n]);


}
Th

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

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

https://www.coursehero.com/file/84158432/Workshop7-Program2c/
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++)

m
{

er as
strcpy(code[j-1], code[j]);

co
strcpy(name[j-1], name[j]);

eH w
salaries[j-1] = salaries[j];
allowances[j-1] = allowances[j];

o.
} rs e
check = 1;
ou urc
(*pn)--;
}
}
if (check == 1)
o

printf("\nRemoved!\n");
aC s

else
v i y re

printf("\nUnremove!\n");
}

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


allowances[], int*pn)
ed d

{
ar stu

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] +
sh is

allowances[j-1]))
Th

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

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

https://www.coursehero.com/file/84158432/Workshop7-Program2c/
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

m
{

er as
userChoice = menu();

co
switch(userChoice)

eH w
{
case 1:

o.
if (isFull(&n))
rs e
printf("\nSorry! The List is full!\n");
ou urc
else
addEmployee(code, name, salaries, allowances, &n);

printf("\nAdded!\n");
o

break;
aC s

case 2:
v i y re

if (isEmpty(&n))
printf("\nSorry! The List is empty!\n");
else
printBasedName(code, name, salaries, allowances,
n);
ed d

break;
ar stu

case 3:
if (isEmpty(&n))
printf("\nSorry! The List is empty!\n");
else
removeEmployee(code, name, salaries, allowances,
sh is

&n);
Th

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

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

https://www.coursehero.com/file/84158432/Workshop7-Program2c/
getch();
}

m
er as
co
eH w
o.
rs e
ou urc
o
aC s
v i y re
ed d
ar stu
sh is
Th

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

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

You might also like