كود:
#include <stdio.h>
//#include <conio.h>
#define SIZE 100
struct date
{ int d;
int m;
int y;
};
struct rec
{
long ID;
char Name[10];
int age;
struct date Birthday;
struct date Hiredday;
float salary;
int grade;
float yearincome;
};
int calculatage(struct rec a[]);
float Calculateincome(struct rec *a);
int count=0,i=0,flag=0;
main()
{
struct rec a[SIZE];
char ans,h;
int ch,i,flag=0,id;
do{
// clrscr();
printf("\nChoose one from the following:\n"
"\t1. Insert an emoloyee record.\n"
"\t2. Display all records.\n"
"\t3. Update a record.\n"
"\t4. Delete a record.\n"
"\t5. Exit.\n"
"\tEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
flag=1;
do
{
printf("\nEnter ID number: ");
scanf("%ld",&a[i].ID);
printf("\nEnter the name of empyolee: ");
scanf("%s",a[i].Name);
printf("\nEnter the Birthday (d/m/y): ");
scanf("%d%d%d",&a[i].Birthday.d,&a[i].Birthday.m,&a[i].Birthday.y);
printf("\nEnter the Hiredday (d/m/y): ");
scanf("%d%d%d",&a[i].Hiredday.d,&a[i].Hiredday.m,&a[i].Hiredday.y);
printf("\nEnter the salary: ");
scanf("%f",&a[i].salary);
printf("\nEnter the grade: ");
scanf("%d",&a[i].grade);
a[i].age=calculatage(&a[i]);
a[i].yearincome=Calculateincome(&a[i]);
i++;
count++;
printf("\nDo you want to insert new record?");
ans=getchar();
if(ans=='y'||ans=='Y')
{/*clrscr();
i++; count++;}*/
}
}while(ans!='n');
//end for
getchar();
//clrscr();
break;
}
case 2:
{
printf ("displaying");
fflush (stdout);
if(flag==0)
printf("\nThere are no record ro display.");
else
for(i=0;i<=count;i++)
{
printf("\n\nID: %ld.\nName: %s.\nAge: %d.\nBirthday: %d/%d/%d."
"\nHiredday: %d/%d/%d.\n Salary: %.2f.\nGrade: %d.\nYearIncome: %.2f."
,a[i].ID,a[i].Name,a[i].age,a[i].Birthday.d,a[i].Birthday.m
,a[i].Birthday.y,a[i].Hiredday.d,a[i].Hiredday.m
,a[i].Hiredday.y,a[i].salary,a[i].grade,a[i].yearincome);
}
getchar();
//clrscr();
break;
}//end case 2
case 3:
{ int n;
if(flag==0)
printf("\nThere is no record to update.");
else
{ printf("\nEnter the ID no. that u want to update its information: ");
scanf("%d",&id);
for(i=0;i<=count;i++)
{ if(id==a[i].ID)
{ n=i;
printf("\nEnter ID number: ");
scanf("%ld",&a[n].ID);
printf("\nEnter the name of empyolee: ");
scanf("%s",a[n].Name);
printf("\nEnter the Birthday (d/m/y): ");
scanf("%d%d%d",&a[n].Birthday.d,&a[n].Birthday.m,&a[n].Birthday.y);
printf("\nEnter the Hiredday (d/m/y): ");
scanf("%d%d%d",&a[n].Hiredday.d,&a[n].Hiredday.m,&a[n].Hiredday.y);
printf("\nEnter the salary: ");
scanf("%f",&a[n].salary);
printf("\nEnter the grade: ");
scanf("%d",&a[n].grade);
a[n].age=calculatage(&a[n]);
a[n].yearincome=Calculateincome(&a[n]);
}
}
if(i>count)
printf("\nThe ID not found");
}
getchar();
//clrscr();
break;}//end case 3
case 4:
{ int n;
if(flag==0)
printf("\nThese is no record to delete");
else
{printf("\nEnter the ID no. that you want to delete its record: ");
scanf("%d",&id);
for(i=0;i<count;i++)
{ if(id==a[i].ID)
{n=i;
for(n;n<=count;n++)
a[n]=a[n+1];
break;}
}
if(i>count)
printf("\nThe ID not found");
}
getchar();
//clrscr();
break;
}
case 5:
{
printf("Thank you for using our program>>>>Goodbye");
goto end;
}
default:
printf("\nOoOoPs!!! you enter the wrong choich");
} //end switch
printf("\n Do you want to choose other choice ?");
h=getchar();
//clrscr();
}while(h=='y'||h=='Y');
end:
getchar();
return 0;
}
int calculatage(struct rec a[])
{
return (a[i].age=2005-a[i].Birthday.y);
}
float Calculateincome(struct rec *a)
{
return a->salary *12.0;
}