You are on page 1of 5

#include<stdio.

h>

#include<stdlib.h>

typedef struct employee {

char *code;

char *name;

double salary;

double allowance;

}employee;

void bubblesort(employee*,int);

int equals(char*,char*);

int main()

employee *e=NULL;

int choice, stop=1;

int n=0; //number of employees

char *c=calloc(8,sizeof(char)); //code of an employee (used in 2 and 3)

while(stop)

printf("1. add a new employee\n2. remove an employee\n3. show data about an


employee\n4. show current employees\n5. exit\n> ");

scanf("%d",&choice);

switch(choice)

case 1: //adding a new employee

e=realloc(e,sizeof(e)+sizeof(employee)); //memory is allocated in order to save


the new employee's data
e[n].code=calloc(8,sizeof(char));

e[n].name=calloc(20,sizeof(char));

printf("code: ");

scanf("%s",e[n].code);

printf("name: ");

scanf("%s",e[n].name);

printf("salary: ");

scanf("%lf",&e[n].salary);

printf("allowance: ");

scanf("%lf",&e[n].allowance);

n++; //the number of employees increases

break;

case 2:

if(n==0)

printf("no employees to remove\n");

break;

printf("insert the employee code: ");

scanf("%s",c);

for(int i=0;i<n;i++)

if(equals(e[i].code,c))

for(int j=i;j<n-1;j++)

e[j]=e[j+1];

n--; //the number of employees decreases

break;
case 3:

if(n==0)

printf("no employees to show\n");

break;

printf("insert the employee code: ");

scanf("%s",c);

for(int i=0;i<8;i++)

if(c[i]=='\n')

c[i]='\0';

for(int i=0;i<n;i++)

if(equals(e[i].code,c)) //I created a function that shows if two strings are


equal

printf("name: %s\nsalary: %lf\nallowance: %lf\


n",e[i].name,e[i].salary,e[i].allowance);

break;

case 4:

if(n==0)

printf("no employees to show\n");

break;

bubblesort(e,n);

for(int i=0;i<n;i++)
printf("code: %s\tname: %s\tsalary: %lf\tallowance: %lf\
n",e[i].code,e[i].name,e[i].salary,e[i].allowance);

break;

case 5:

stop=0;

break;

default:

printf("command not found. try again\n");

break;

return 0;

void bubblesort(employee *e,int n)

int swap=1;

employee aux;

while(swap)

swap=0;

for(int i=0;i<n-1;i++)

for(int j=i;j<n;j++)

if(e[j].salary>e[i].salary)

aux=e[j];

e[j]=e[i];
e[i]=aux;

swap=1;

return;

You might also like