Tuesday 1 November 2011

4.1. Employee DB (Structure without pointers)



//Structure manipulation for employee database without pointers



  #include<stdio.h>
  #include<conio.h>
  //main()
  struct employee
   {
     int id;
     char name[20];
     float salary;
     int dflag;
   };
 int main(void)
  {
    //variable declarations
    int i=0,choice=0,no=0,delno=0;
    struct employee e[50]={0,'\0',0.0};
    //function prototypes
    void accept(struct employee[],int );
    void display(struct employee[],int );
    void deleterec(struct employee[],int,int);
    int append(struct employee[],int);
    void modify(struct employee[],int,int);
    clrscr();
    do
     {
      printf("\n\n*****MENU*****");
      printf("\n\t1.Accept.\n");
      printf("\t2.Display.\n\t3.Delete\n\t4.Append\n\t5.Modify\n");
      printf("Enter your choice:");
      scanf("%d",&choice);
      switch(choice)
       {
case 1:
    //To accept records
     printf("Enter the no. of records:  ");
     scanf("%d",&no);
     accept(e,no);
     break;
case 2:
     //to display records
     display(e,no);
     break;
case 3:
    //To delete a record
    printf("Enter the id to be deleted: ");
    scanf("%d",&delno);
    deleterec(e,no,delno);
    break;
case 4:
     //to append a record
     no=append(e,no);
     break;
case 5:
    //to modify a record
    printf("Enter the employee id to modify");
    scanf("%d",&delno);
    modify(e,no,delno);
    break;
default:
      printf("\nwrong choice");
       }
     }while(choice>0&&choice<=5);
     getch();
     return 0;
  }

  //Function to accept records
  void accept(struct employee e1[],int no)
  {
    int i=0;float sal=0.0;
    for(i=0;i<no;i++)
     {
       printf("Enter the following info.for %d emp:\n",i+1);
       printf("Enter Id:");
       scanf("%d",&e1[i].id);
       printf("\nEnter name:");
       scanf("%s",e1[i].name);
       printf("Enter salary:");
       scanf("%f",&sal);
       e1[i].salary=sal;
       e1[i].dflag=1;
     }
  }

  //function to display records
  void display(struct employee e1[],int no)
   {
     int i=0,j=0;
     printf("The info is:\n");
     for(i=0;i<=no;i++)
      {
       if(e1[i].dflag==1)
       {
printf("\nId:");
printf("%d",e1[i].id);
printf("\tName:");
printf("%s",e1[i].name);
printf("\tSalary:");
printf("%f",e1[i].salary);
       }
     }
   }

   //function to delete a record
  void deleterec(struct employee e1[],int no,int delno)
   {
     int i=0;
     for(i=0;i<no;i++)
     {
       if(e1[i].id==delno)
       {
if(e1[i].dflag==0)
{
   printf("\nRecord is already deleted\n");
   break;
}
else
{
  e1[i].dflag=0;
  break;
}
}
      }
   }

   //function to add a record at the end
  int append(struct employee e1[],int no)
   {
     int i=0;float sal=0.0;
     no=no+1;
     printf("Enter the following info.for %d emp:\n",i+1);
     printf("Enter Id:");
     scanf("%d",&e1[no].id);
     printf("\nEnter name:");
     scanf("%s",e1[no].name);
     printf("Enter salary:");
     scanf("%f",&sal);
     e1[no].salary=sal;
     e1[no].dflag=1;
     return no;
   }

   //function to modify a record
   void modify(struct employee e1[],int no,int delno)
    {
      float sal=0.0;
      int i=0,flag=0;
      for(i=0;i<no;i++)
      {
       if(e1[i].id==delno&&e1[i].dflag==1)
       {
flag=1;
break;
       }
      }
      if(flag==1)
      {
printf("Enter the following modified info.for %d emp:\n",no);
printf("Enter Id:");
scanf("%d",&e1[i].id);
printf("\nEnter name:");
scanf("%s",e1[i].name);
printf("Enter salary:");
scanf("%f",&sal);
e1[i].salary=sal;
      }
      else
      {
printf("Record is deleted");
      }
   }

No comments:

Post a Comment