Source code
#include<stdio.h>
struct Joining_Date
{
int day,month,year;
};
struct Employee
{
int id;
char name[30];
char designation[20];
float salary;
char number[12];
struct Joining_Date jd;
};
struct Employee data[500];
int count=0;
int compare(char str1[], char str2[]);
int menu()
{
printf("
What you want to do????
");
printf("
Enter 1 to Add new employee
Enter 2 to Search an employee by id
Enter 3 to Update employee info
Enter 4 to Delete employee
Enter 5 to Display average salary
Enter 6 to Search employee ids after a given joining date
Enter 7 to Search employee ids having maximum salary
Enter 8 to Search total no of employees by designation
Enter 9 to Search employee ids having the given name
Enter -1 to exit
");
int ch;
printf("
Please Enter Your choice:");
scanf("%d",&ch);
printf("
");
return ch;
}
void add_employee()
{
struct Employee temp;
printf("Enter Employee id :
");
scanf("%d",&temp.id);
// check id
int i;
int f=0;
for(i=0; i<count; i++)
{
if(data[i].id==temp.id)
{
f=1;
break;
}
}
if(f==1)
{
printf("ID duplicate found.
");
return;
}
printf("Enter Employee name:
");
scanf("%s",&temp.name);
printf("Enter Designation/Rank:
");
scanf("%s",&temp.designation);
printf("Enter Salary:
");
scanf("%f",&temp.salary);
printf("Enter Phone no:
");
scanf("%s",&temp.number);
// check number
f=0;
for(i=0; i<count; i++)
{
if(compare(temp.number,data[i].number)==0)
{
f=1;
break;
}
}
if(f==1)
{
printf("Phone no duplicate found.
");
return;
}
///////
printf("Enter Joining Date(dd-mm-yyyy):
");
scanf("%d-%d-%d",&temp.jd.day,&temp.jd.month,&temp.jd.year);
data[count]=temp;
count++;
}
int search_id(int emp_id)
{
int f=0,i,index;
for(i=0; i<count; i++)
{
if(data[i].id==emp_id)
{
index=i;
f=1;
break;
}
}
if(f==0)
{
printf("Not Found
");
return -1;
}
else
{
printf("Employee id : %d
Name : %s
Designation/Rank : %s
Salary : %f
Phone no : %s
Joining date : %d/%d/%d
",data[index].id,data[index].name,data[index].designation,data[index].salary,data[index].number,data[index].jd.day,data[index].jd.month,data[index].jd.year);
return index;
}
}
void update_info(int emp_id)
{
if(search_id(emp_id)==-1)
{
printf("Not exists.
");
}
else
{
int index=search_id(emp_id);
struct Employee temp;
printf("Enter Employee id :
");
scanf("%d",&temp.id);
// check id
int i;
int f=0;
for(i=0; i<count; i++)
{
if(i==index)
continue;
if(data[i].id==temp.id)
{
f=1;
break;
}
}
if(f==1)
{
printf("ID duplicate found.
");
return;
}
printf("Enter Employee name:
");
scanf("%s",&temp.name);
printf("Enter Designation/Rank:
");
scanf("%s",&temp.designation);
printf("Enter Salary:
");
scanf("%f",&temp.salary);
printf("Enter Phone no:
");
scanf("%s",&temp.number);
// check number
f=0;
for(i=0; i<count; i++)
{
if(i==index)
continue;
if(compare(temp.number,data[i].number)==0)
{
f=1;
break;
}
}
if(f==1)
{
printf("Phone no duplicate found.
");
return;
}
///////
printf("Enter Joining Date:
");
scanf("%d %d %d",&temp.jd.day,&temp.jd.month,&temp.jd.year);
data[index]=temp;
printf("Updated successfully.
");
}
}
void delete_employee(int emp_id)
{
int index=search_id(emp_id),i;
if(index!=-1)
{
for(i=index; i<count; i++)
{
data[i]=data[i+1];
}
count--;
}
}
double display_avg_sal()
{
float avg=0;
int i;
for(i=0; i<count; i++)
{
avg=avg+data[i].salary;
}
avg=(avg)/(float)count;
double ans=avg;
return ans;
}
void search_join_date(int day, int month, int year)
{
int i;
for(i=0; i<count; i++)
{
if(data[i].jd.year>year)
{
printf("Employee id : %d
Name : %s
Designation/Rank : %s
Salary : %f
Phone no : %s
Joining date : %d/%d/%d
",data[i].id,data[i].name,data[i].designation,data[i].salary,data[i].number,data[i].jd.day,data[i].jd.month,data[i].jd.year);
}
else if(data[i].jd.year==year)
{
if(data[i].jd.month>month)
{
printf("Employee id : %d
Name : %s
Designation/Rank : %s
Salary : %f
Phone no : %s
Joining date : %d/%d/%d
",data[i].id,data[i].name,data[i].designation,data[i].salary,data[i].number,data[i].jd.day,data[i].jd.month,data[i].jd.year);
}
else if(data[i].jd.month==month)
{
if(data[i].jd.day>=day)
{
printf("Employee id : %d
Name : %s
Designation/Rank : %s
Salary : %f
Phone no : %s
Joining date : %d/%d/%d
",data[i].id,data[i].name,data[i].designation,data[i].salary,data[i].number,data[i].jd.day,data[i].jd.month,data[i].jd.year);
}
}
}
}
}
double max_sal()
{
int i,index=-1;
if(count==0)
return 0;
float max=data[0].salary;
for(i=0; i<count; i++)
{
if(data[i].salary>max)
{
index=i;
max=data[i].salary;
}
}
printf("The ID is:%d
",data[index].id);
return max;
}
int count_rank(char string[])
{
int c=0;
int i;
for(i=0; i<count; i++)
{
if(compare(data[i].designation,string)==0)
c++;
}
return c;
}
void search_name(char name[])
{
int i;
for(i=0; i<count; i++)
{
if(!compare(data[i].name,name))
{
printf("Id : %d
",data[i].id);
}
}
}
int main()
{
printf("
WELCOME To The Best Employee Software
");
while(1)
{
int t=menu();
if(t==-1)
{
printf("
Thanks For Using our Software
!!!!!!
Good Bye
");
break;
}
else if(t==1)
{
add_employee();
}
else if(t==2)
{
int c;
printf("
PLease Enter The ID you want search:");
scanf("%d",&c);
search_id(c);
}
else if(t==3)
{
int c;
printf("
Please Enter the ID Which you want to update:");
scanf("%d",&c);
update_info(c);
}
else if(t==4)
{
int c;
scanf("%d",&c);
printf("
Please Enter the ID Which you want to delete:");
delete_employee(c);
}
else if(t==5)
{
printf("
The Average salary is: %f
",display_avg_sal());
}
else if(t==6)
{
int d,m,y;
printf("
Please Enter the Date(dd-mm-yyyy):");
scanf("%d-%d-%d",&d,&m,&y);
search_join_date(d,m,y);
}
else if(t==7)
{
printf("
Max salary : %f
",max_sal());
}
else if(t==8)
{
char str[20];
printf("
Please Enter the Rank you want to count: ");
scanf("%s",str);
if(count_rank(str)!=0)
{
printf("There are %d people who are %s.
",count_rank(str),str);
}
else if(count_rank(str)>0)
{
printf("There is %d people who is %s.
",count_rank(str),str);
}
else if(count_rank(str)==0)
{
printf("
There is NO ID found Who is %s.",str);
}
}
else if(t==9)
{
char st[20];
printf("
PLease Enter The Name You want to Search: ");
scanf("%s",st);
search_name(st);
}
}
}
int compare(char str1[], char str2[])
{
int i = 0;
while(str1[i] == str2[i])
{
if(str1[i] == '