Tuesday 31 October 2017

CO4 Assignment Second question solution

There are 100 records present in a file with the following structure:
struct date
{
int d, m, y ;
} ;
struct employee
{
int empcode[6] ;
char empname[20] ;
struct date join_date ;
float salary ;
} ;
Write a program to read these records, arrange them in ascending order of join_date and write them in to a target file.

Solution:



#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
struct doj
{
int day,mon, year;
};
struct employee
{
int num;
char name[30];
struct doj d;
float salary;
};
struct employee s[100],temp;
int n,i,j,p;
FILE *fp,*fp1;
fp=fopen("emp.txt","a+");
fp1=fopen("target.txt","a+");
printf("\n Enter number of employees");
scanf("%d",&n);
for(i=0;i<n;i++)
{
               printf("\n Enter Number");
               scanf("%d",&s[i].num);
               printf("\n Enter Name");
               scanf("%s",s[i].name);
               printf("\n Enter Date of joining(dd-mm-yy");
               scanf("%d%d%d",&s[i].d.day,&s[i].d.mon,&s[i].d.year);
               printf("enter salary");
               scanf("%f",&s[i].salary);
               fprintf(fp,"%d\t%s\t%d-%d-%d\t %f\n",s[i].num,s[i].name,s[i].d.day,s[i].d.mon,s[i].d.year,s[i].salary);
}

for(p=0;p<n-1;p++)
{
               for(j=0;j<n-p-1;j++)
               {
                              if(s[j].d.year>s[j+1].d.year||s[j].d.year==s[j+1].d.year&&s[j].d.mon>s[j+1].d.mon||s[j].d.year==s[j+1].d.year&&s[j].d.mon==s[j+1].d.mon&&s[j].d.day>s[j+1].d.day)
                              {
                                             temp=s[j];
                                             s[j]=s[j+1];
                                             s[j+1]=temp;
                              }
               }
}
for(i=0;i<n;i++)
fprintf(fp1,"%d\t%s\t%d-%d-%d\t %f\n",s[i].num,s[i].name,s[i].d.day,s[i].d.mon,s[i].d.year,s[i].salary);
fclose(fp);
fclose(fp1);
getch();
}

No comments:

Post a Comment