Advertisement
Looz_Dreemur

P2Q2 by other ppl

Nov 13th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.25 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #define MAX_SIZE 20
  5. #pragma warning (disable:4996)
  6.  
  7. struct Date{
  8.     int day, mon, yrs;
  9. };
  10.  
  11. struct Employee {
  12.     char iD[4], name[30];
  13.     struct Date date;
  14.     char  department[20];
  15. };
  16.  
  17. struct Employee emp[MAX_SIZE] = { {"E01","Alice Chin",{5,12,2008},"R & D"},{"E02","John",{9,12,2011},"IT"},
  18.     {"E03","Vivian",{3,3,2015},"HR"},{"E03","Alice Chin",{4,4,2011},"IT"},{"E05","Vivien Tan",{5,3,2015},"HR"}
  19. };
  20.  
  21. void displayAll();
  22. void search();
  23. void view();
  24. void edit();
  25.  
  26. void main() {
  27.    
  28.     displayAll();
  29.     search();
  30.     rewind(stdin);
  31.     view();
  32.     rewind(stdin);
  33.     edit();
  34.     system("pause");
  35. }
  36.  
  37. void displayAll() {
  38.     int i;
  39.     printf("\t\t\tEmployee Details\n\n");
  40.     printf("Employee ID      Name         Date Joined    Department\n\n");
  41.     for (i = 0; i < 5; i++)
  42.     {
  43.         printf("%-10s      %-10s    %02d/%02d/%04d     %-5s\n", emp[i].iD,emp[i].name,emp[i].date.day, emp[i].date.mon, emp[i].date.yrs,emp[i].department);
  44.     }
  45.  
  46. }
  47.  
  48. void search() {
  49.     int month, years;
  50.     int i;
  51.     printf("Enter a particular date to search employee information:\n");
  52.     printf("Month :");
  53.     scanf("%d", &month);
  54.     printf("Year :");
  55.     scanf("%d", &years);
  56.     for (i = 0; i < 5; i++)
  57.     {
  58.         if (month == emp[i].date.mon&&years==emp[i].date.yrs)
  59.         {
  60.             printf("Employee joined :\n ");
  61.             printf("%s\n\n", emp[i].name);
  62.         }
  63.    
  64.     }
  65. }
  66.  
  67. void view()
  68. {
  69.     int i;
  70.     int found = 0;
  71.     char id[4];
  72.     printf("Enter ID of Employee to view  : ");
  73.     scanf("%s", &id);
  74.     for (i = 0; i < 5; i++)
  75.     {
  76.         if (strcmp(id, emp[i].iD) == 0)
  77.         {
  78.            
  79.             printf("Employee ID    :  %s\n", emp[i].iD);
  80.             printf("Name           :  %s\n", emp[i].name);
  81.             printf("Date Joined    :  %02d/%02d/%04d\n", emp[i].date.day, emp[i].date.mon, emp[i].date.yrs);
  82.             printf("Department     :  %s\n", emp[i].department);
  83.             found++;
  84.         }
  85.         if (found == 0)
  86.             printf("NO records found\n");
  87.     }
  88. }
  89.    
  90.  
  91. void edit()
  92. {
  93.     char name[50], department[15], id[4];
  94.     int day, mon, years;
  95.     int i;
  96.  
  97.     printf("Enter employee ID you want to change : ");
  98.     scanf("%s", &id);
  99.  
  100.     //printf("\n***%s\n", id);
  101.     for (i=0;i<5;i++)
  102.     {
  103.         printf("\n***%s\n",emp[i].iD);
  104.         if (strcmp(id, emp[i].iD) == 0)
  105.         {
  106.             printf("Employee Name : ");
  107.             scanf("%[^\n]", &name);
  108.             printf("Date Joined :\n");
  109.             printf("Day   :");
  110.             scanf("%d", &day);
  111.             printf("Month :");
  112.             scanf("%d", &mon);
  113.             printf("Year  :");
  114.             scanf("%d", &years);
  115.             printf("Department : ");
  116.             scanf("%[^\n]", &department);
  117.             printf("\n\n***%d\n", years);
  118.         }
  119.         printf("\n\nBEFORE\n");
  120.         printf("Employee ID    :  %s\n", emp[i].iD);
  121.         printf("Name           :  %s\n", emp[i].name);
  122.         printf("Date Joined    :  %02d/%02d/%04d\n", emp[i].date.day, emp[i].date.mon, emp[i].date.yrs);
  123.         printf("Department     :  %s\n", emp[i].department);
  124.         strcpy(emp[i].name, name);
  125.         strcpy(emp[i].department,department);
  126.         //emp[i].date.day = day;
  127.         //emp[i].date.mon = mon;
  128.         //emp[i].date.yrs = years;
  129.         printf("----------------------------------------------\n");
  130.         printf("\nAFTER\n");
  131.         printf("Employee ID    :  %s\n", emp[i].iD);
  132.         printf("Name           :  %s\n", emp[i].name);
  133.         printf("Date Joined    :  %02d/%02d/%04d\n", emp[i].date.day, emp[i].date.mon, emp[i].date.yrs);
  134.         printf("Department     :  %s\n", emp[i].department);
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement