Tuesday 1 November 2011

4.2. Employee database (structure) using pointers


/*
Assignment:Structure for employee database(with pointers)
*/




/* header files */
#include<stdio.h>
#include<conio.h>
struct employee              /* Declaration of structure */
{
char name[10];
int id;
float salary;
int dflag;
};
int accept(struct employee *, int ); /* function prototype */
int display(struct employee *, int , int );
int append(struct employee *, int );
int modify(struct employee *, int , int );
int delete1(struct employee *, int , int );
int main(void) /* Starting of main */
{
/* initialisation & declaration */
struct employee *e; /* Pointer to structure declared */
int icNum=0, initial=0, icId=0, ch=0;
char choice='\0';
clrscr();
do /* starting of do while loop */
{
printf("\n\tMenu");
printf("\n\t 1 : Create the database");
printf("\n\t 2 : Display Records");
printf("\n\t 3 : Add Record");
printf("\n\t 4 : Modify existing Record");
printf("\n\t 5 : Delete Record");
printf("\n\t 6 : Exit");
printf("\n\tEnter your choice : ");
scanf("%d",&ch);
switch(ch) /* starting of switch */
{
case 1 : Label:
if(icNum==0)
{
printf("\nEnter the number of employees\t");
flushall();
scanf("%d",&icNum);
accept(e,icNum);
}
else
{
printf("\nDo you wish to overwrite the database?\t");
flushall();
scanf("%c",&choice);
if(choice=='y'||choice=='Y')
{
icNum=0;
goto Label;
}
}
break;
case 2 : if(icNum!=0)
{
printf("\nEmployee database");
display(e,icNum,initial);
}
else
{
printf("\nDatabase is empty!");
}
break;
case 3 : if(icNum!=0)
{
append(e,icNum);
icNum=icNum+1;
}
else
{
printf("\nCannot add record before creating the database!");
}
break;
case 4 : if(icNum!=0)
{
printf("\nEnter the ID to modify\t");
scanf("%d",&icId);
modify(e,icId,icNum);
}
else
{
printf("\nDatabase is empty!");
}
break;
case 5 : if(icNum!=0)
{
printf("Enter the ID to delete\t");
scanf("%d",&icId);
delete1(e,icId,icNum);
}
else
{
printf("\nDatabase is empty!");
}
break;
default:printf("\n\t Exiting program ");
break;
} /* End of switch */
} /* End of do while loop */
while(ch<=5&&ch!=0);
getch();
return 0;
} /* End of main */
/* For accepting a record */
int accept(struct employee *x, int n) /* Function Definition */
{
int i=0, j=0;
struct employee *t;
float sal=0.0;
(x)=(x+0);
(t)=(x);
for(i=1;i<=n;i++)
{
printf("\nEmployee %d",i);
printf("\nEnter name\t");
scanf("%s",&(x)->name);
printf("\nEnter ID\t");
Label : scanf("%d",&(x)->id);
if((x)->id==0)
{
printf("\nID cannot be zero\nEnter Id again\t");
goto Label;
}
for(j=1;j<i;j++)
{
if((t)->id==(x)->id)
{
printf("\nID already exists\nEnter ID again\t");
/* ID must be unique */
goto Label;
}
(t)++;
}
printf("Enter salary\t");
flushall();
scanf("%f",&sal);
(x)->salary=sal;
(x)->dflag=1;
(x)++;
}
return 0;
}
/* For displaying all records */
int display(struct employee *x, int total, int initial)
{
int i=0;
(x)=(x+0);
printf("\n\tName\t\tID\t\tSalary");
for(i=initial+1;i<=total;i++)
{
if((x)->dflag==1)
{
printf("\n\t%s\t\t%d\t\t%f",(x)->name,(x)->id,(x)->salary);
}
(x)++;
}
printf("\n");
return 0;
}
/* For adding a record at the end */
int append(struct employee *x, int total)
{
int j=0;
struct employee *t;
float sal=0.0;

t=x;
printf("\nEmployee %d",total+1);
printf("\nEnter name\t");
scanf("%s",&(x+total+1)->name);
printf("\nEnter ID\t");
Label : scanf("%d",&(x+total)->id);
if((x+total)->id==0)
{
printf("\nID cannot be zero\nEnter Id again\t");
goto Label;
}
for(j=1;j<=total;j++)
{
if((x+total)->id==(t)->id)
{
printf("\nID already exists\nEnter ID again\t");
/* ID must be unique */
goto Label;
}
t++;
}
printf("Enter salary\t");
flushall();
scanf("%f",&sal);
(x+total)->salary=sal;
(x+total)->dflag=1;
x++;
return 0;
}
/* To modify a record except ID */
int modify(struct employee *x, int id, int total)
{
int i=0, flag=0;
float sal=0.0;
char choice='\0';

for(i=1;i<=total;i++)
{
if(id==(x)->id)
{
flag=0;
if((x)->dflag!=-1)
{
display(x,i,i-1);
printf("\n\nDo you wish to modify this record?\n\tY/N\t");
flushall();
scanf("%c",&choice);
if(choice=='y'||choice=='Y')
{
printf("\nEmployee ID : %d",(x)->id);
/* ID will not change as it is unique */
printf("\nEnter name\t");
scanf("%s",&(x)->name);
printf("\nEnter salary\t");
flushall();
scanf("%f",&sal);
(x)->salary=sal;
printf("\nModified Record is ");
display(x,i,i-1);
}
else
{
i++;
}
break;
}
else
{
printf("\nID does not exist\n");
flag=(-1);
}
}
}
if(id!=(x+i-1)->id&&flag!=-1)
{
printf("\nID does not match with any ID in database");
}
return 0;
}
/* For logical deletion of the record */
int delete1(struct employee *x, int id, int total)
{
int i=0;
char choice='\0';
for(i=0;i<=total;i++)
{
if(id==(x)->id)
{
display(x,i,i-1);
printf("\nDo you wish to delete this record\n\tY/N\t");
flushall();
scanf("%c",&choice);
if(choice=='y'||choice=='Y')
{
(x)->dflag=(-1);
/* Logical deletion i.e. record will be
  present physically */
printf("Record successfully deleted");
break;
}
}
else
{
printf("\nID does not exist");
}
}
return 0;
}

No comments:

Post a Comment