Advertisement
jbn6972

Untitled

Dec 19th, 2021
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. float avg;
  6. typedef struct Node
  7. {
  8.     char name[100];
  9.     int age, emp_num;
  10.     char date[10];
  11.     float salary;
  12.     struct Node *next;
  13. } Node;
  14.  
  15. Node *getNode()
  16. {
  17.     Node *p;
  18.     p = (Node *)malloc(sizeof(Node));
  19.  
  20.     return p;
  21. }
  22.  
  23. typedef struct
  24. {
  25.     int count;
  26.     Node *first;
  27. } list;
  28.  
  29. void Display_all(list *lp)
  30. {
  31.     struct Node *temp;
  32.     if (lp->first == NULL)
  33.     {
  34.         printf("There Are No Employee Details!!\n");
  35.         return;
  36.     }
  37.     temp = lp->first;
  38.     printf("-----------All Employee Details-----------\n");
  39.     while (temp != NULL)
  40.     {
  41.         printf("Employee Name :: %s\n", temp->name);
  42.         printf("Age :: %d\n", temp->age);
  43.         printf("Emp Number :: %d\n", temp->emp_num);
  44.         printf("Joining Date :: %s\n", temp->date);
  45.         printf("Salary :: %f\n", temp->salary);
  46.         printf("\n");
  47.         temp = temp->next;
  48.     }
  49.     printf("Total Number of Employees :: %d", lp->count);
  50. }
  51.  
  52. void Display_particular(list *lp)
  53. {
  54.     struct Node *temp;
  55.     int emp_num;
  56.     if (lp->first == NULL)
  57.     {
  58.         printf("There Are No Employee Details!!\n");
  59.         return;
  60.     }
  61.     printf("Enter the Employee number whose Deatils are to be displayed :: ");
  62.     scanf("%d", &emp_num);
  63.     temp = lp->first;
  64.     while (temp != NULL)
  65.     {
  66.         if (temp->emp_num == emp_num)
  67.         {
  68.             printf("Employee Name :: %s\n", temp->name);
  69.             printf("Age :: %d\n", temp->age);
  70.             printf("Emp Number :: %d\n", temp->emp_num);
  71.             printf("Joining Date :: %s\n", temp->date);
  72.             printf("Salary :: %f\n", temp->salary);
  73.             printf("\n");
  74.             return;
  75.         }
  76.         temp = temp->next;
  77.     }
  78.     printf("Employee not found!!!\n");
  79. }
  80. void Delete_record(list *lp)
  81. {
  82.     struct Node *temp, *prev;
  83.     int emp_num;
  84.     if (lp->first == NULL)
  85.     {
  86.         printf("There Are No Employees To be deleted!!\n");
  87.         return;
  88.     }
  89.     temp = lp->first;
  90.     printf("Enter the Employee number whose Record is to be Deleted :: ");
  91.     scanf("%d\n", &emp_num);
  92.  
  93.     if (temp->emp_num == emp_num)
  94.     {
  95.         lp->first = temp->next;
  96.         avg = ((avg*lp->count)-temp->salary)/(lp->count-1);
  97.         free(temp);
  98.         lp->count--;
  99.         printf("Record Deleted Successfully!!\n");
  100.         return;
  101.     }
  102.     while (temp != NULL)
  103.     {
  104.         if (temp->emp_num != emp_num)
  105.         {
  106.             prev = temp;
  107.             temp = temp->next;
  108.         }
  109.         else
  110.         {
  111.             if (temp->next != NULL)
  112.             {
  113.                 prev->next = temp->next;
  114.                 avg = ((avg*lp->count)-temp->salary)/(lp->count-1);
  115.                 free(temp);
  116.                 lp->count--;
  117.                 printf("Record Deleted Successfully!!\n");
  118.                 return;
  119.             }
  120.             else
  121.             {
  122.                 prev->next = NULL;
  123.                 avg = ((avg*lp->count)-temp->salary)/(lp->count-1);
  124.                 free(temp);
  125.                 lp->count--;
  126.                 printf("Record Deleted Successfully!!\n");
  127.                 return;
  128.             }
  129.         }
  130.     }
  131.     printf("Employee Not Found!!!\n");
  132. }
  133.  
  134. void Insert_record(list *lp)
  135. {
  136.     char name[100];
  137.     int age, emp_num;
  138.     char date[10];
  139.     float salary;
  140.     printf("Enter Employee Name :: ");
  141.     scanf("%s", name);
  142.     printf("Enter Employee Age :: ");
  143.     scanf("%d", &age);
  144.     printf("Enter Employee Number :: ");
  145.     scanf("%d", &emp_num);
  146.     printf("Enter Date of Joining :: ");
  147.     scanf("%s", date);
  148.     printf("Enter Salary :: ");
  149.     scanf("%f", &salary);
  150.  
  151.     Node *employee;
  152.     employee = getNode();
  153.     strcpy(employee->name, name);
  154.     employee->age = age;
  155.     employee->emp_num = emp_num;
  156.     strcpy(employee->date, date);
  157.     employee->salary = salary;
  158.     employee->next = NULL;
  159.  
  160.     if (lp->first == NULL)
  161.     {
  162.         lp->first = employee;
  163.         printf("Employee Records Inserted Successfully!!!\n");
  164.         lp->count++;
  165.         avg = ((avg*(lp->count-1))+employee->salary)/(lp->count);
  166.         return;
  167.     }
  168.     struct Node *temp;
  169.     temp = lp->first;
  170.     while (temp->next != NULL)
  171.     {
  172.         temp = temp->next;
  173.     }
  174.     temp->next = employee;
  175.     printf("Employee Records Inserted Successfully!!!\n");
  176.     lp->count++;
  177.     avg = ((avg*(lp->count-1))+employee->salary)/(lp->count);
  178. }
  179.  
  180. void Display_salary_above_average(list *lp)
  181. {
  182.     struct Node *temp;
  183.     if (lp->first == NULL)
  184.     {
  185.         printf("There Are No Employee Details!!\n");
  186.         return;
  187.     }
  188.     temp = lp->first;
  189.     printf("-----------Employee Details whose Salaray is above Average-----------\n");
  190.     while (temp != NULL)
  191.     {
  192.         if (temp->salary > avg)
  193.         {
  194.             printf("Employee Name :: %s\n", temp->name);
  195.             printf("Age :: %d\n", temp->age);
  196.             printf("Emp Number :: %d\n", temp->emp_num);
  197.             printf("Joining Date :: %s\n", temp->date);
  198.             printf("Salary :: %f\n", temp->salary);
  199.             printf("\n");
  200.         }
  201.         temp = temp->next;
  202.     }
  203. }
  204.  
  205. int main()
  206. {
  207.     printf("-------------EMPLOYEE MANAGEMENT SYSTEM-------------\n");
  208.     int choice;
  209.     list employee;
  210.     employee.first = NULL;
  211.     employee.count = 0;
  212.     avg = 0.0;
  213.     while (1)
  214.     {
  215.         printf("\n");
  216.         printf("Press 1 to Display Records of all Employees\n");
  217.         printf("Press 2 to Display Record of a particular Employee\n");
  218.         printf("Press 3 to Delete Record of a particular\n");
  219.         printf("Press 4 to Insert Record of a new Employee\n");
  220.         printf("Press 5 to Display name of Employees having highest salary than average of all employees salaray\n");
  221.         printf("Press 6 to Exit the program\n");
  222.         printf("Enter your choice :: ");
  223.         scanf("%d", &choice);
  224.  
  225.         switch (choice)
  226.         {
  227.         case 1:
  228.             Display_all(&employee);
  229.             break;
  230.         case 2:
  231.             Display_particular(&employee);
  232.             break;
  233.         case 3:
  234.             Delete_record(&employee);
  235.             break;
  236.         case 4:
  237.             Insert_record(&employee);
  238.             break;
  239.         case 5:
  240.             Display_salary_above_average(&employee);
  241.             break;
  242.         case 6:
  243.             exit(0);
  244.         default:
  245.             printf("Invaild Choice !!!\n");
  246.         }
  247.     }
  248.     return 0;
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement