Advertisement
Oddlyshapedtree

Untitled

Dec 1st, 2015
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct Shift
  6.  {
  7.  char name[100];
  8.  char day_of_week[100];
  9.  int start_hour;
  10.  int end_hour;
  11.  };
  12.  
  13. struct Shift shift_data[100];
  14. struct Shift temp;
  15.  
  16. int read_data(struct Shift shift_data[], int *num_shifts);
  17. void sort_data(struct Shift shift_data[], int num_shifts);
  18. void sort_data(struct Shift shift_data[], int num_shifts);
  19.  
  20.  
  21.  
  22.  
  23.  
  24. int main()
  25.   {
  26.     int num_shifts;
  27.  
  28.     read_data(shift_data, &num_shifts);
  29.  
  30.   //  void sort_data(struct Shift shift_data[], int num_shifts);
  31.  
  32.     print_data(shift_data, num_shifts);
  33.  
  34.     return 0;
  35.  
  36.   }
  37.  
  38.  
  39.  
  40. // Preconditions: array of structure "Shift" to store data
  41. // Postconditions: number of shifts read in from data file
  42. // Actions: Ask user for name of input file. Read the number
  43. // of shifts, then read in the data for all
  44. // of the shifts. Return the number of shifts.
  45. int read_data(struct Shift shift_data[], int *num_shifts)
  46. {
  47.     char input_schedule[100];
  48.  
  49.     int i;
  50.  
  51.  
  52.     printf("What is the name of the file?\n");
  53.     scanf("%s",input_schedule);
  54.  
  55.     FILE *ifp;
  56.     ifp=fopen(input_schedule,"r");
  57.  
  58.     fscanf(ifp,"%d",num_shifts);
  59.  
  60.     printf("numshifts = %d", num_shifts);
  61.  
  62.     for(i=0;i<*num_shifts;i++)
  63.     {
  64.         fscanf(ifp,"%s",&shift_data[i].name);
  65.         fscanf(ifp,"%s",&shift_data[i].day_of_week);
  66.         fscanf(ifp,"%d",&shift_data[i].start_hour);
  67.         fscanf(ifp,"%d",&shift_data[i].end_hour);
  68.     }
  69.  
  70.     fclose(ifp);
  71.     return num_shifts;
  72.   }
  73.  
  74.  
  75.  
  76. // Preconditions: array of structure "Shift"
  77. // integer value indicating number of shifts
  78. // Postconditions: none - this function does not return anything.
  79. // Actions: Sort the shifts by the TA's first name.
  80. /*
  81. void sort_data(struct Shift shift_data[], int num_shifts)
  82. {
  83.  
  84. int i,j;
  85.  
  86. for(i=0;i<num_shifts;i++)
  87. {
  88.     for(j=i+1;j<num_shifts;j++)
  89.     {
  90.         if(strcmp(shift_data[i].name,shift_data[i].name)<0)
  91.         {
  92.             temp[i]=shift_data[i];
  93.             shift_data[i]=shift_data[j];
  94.             shift_data[i]=temp[i];
  95.         }
  96.         if(strcmp(shift_data[i].day_of_week,shift_data[i].day_of_week)<0)
  97.         {
  98.             temp[i]=shift_data[i];
  99.             shift_data[i]=shift_data[j];
  100.             shift_data[i]=temp[i];
  101.         }
  102.        if(strcmp(shift_data[i].start_hour,shift_data[i].start_hour)<0)
  103.         {
  104.             temp[i]=shift_data[i];
  105.             shift_data[i]=shift_data[j];
  106.             shift_data[i]=temp[i];
  107.         }
  108.         if(strcmp(shift_data[i].end_hour,shift_data[i].end_hour)<0)
  109.         {
  110.             temp[i]=shift_data[i];
  111.             shift_data[i]=shift_data[j];
  112.             shift_data[i]=temp[i];
  113.         }
  114. }
  115.  
  116. }
  117. */
  118.  
  119. // Preconditions: array of structure "Shift"
  120. // integer value indicating number of shifts
  121. // Postconditions: none - this function does not return anything.
  122. // Actions: Print the sorted data in the format described below.
  123.  
  124. void print_data(struct Shift shift[], int num_shifts)
  125. {
  126.    int i;
  127.  
  128.    printf("\n\n\n");
  129.  
  130.    for(i=0;i<num_shifts;i++)
  131.    {
  132.        printf("%s\t\t",shift_data[i].name);
  133.        printf("%s\t",shift_data[i].day_of_week);
  134.        printf("%d to ",shift_data[i].start_hour);
  135.        printf("%d\n",shift_data[i].end_hour);
  136.  
  137.            if(shift_data[i].start_hour>12)
  138.            {
  139.                shift_data[i].start_hour=shift_data[i].start_hour-12;
  140.                printf("%2d:00 pm to  ", shift_data[i].start_hour);
  141.            }
  142.            else if(shift_data[i].start_hour<12)
  143.            {
  144.                printf("%2d:00 am to  ", shift_data[i].start_hour);
  145.            }
  146.            else if(shift_data[i].start_hour==12)
  147.            {
  148.                printf("%2d:00 pm to  ", shift_data[i].start_hour);
  149.            }
  150.  
  151.  
  152.            if(shift_data[i].end_hour>12)
  153.            {
  154.                shift_data[i].end_hour=shift_data[i].end_hour-12;
  155.                printf("%2d:00 pm\n", shift_data[i].end_hour);
  156.            }
  157.            else if(shift_data[i].end_hour<12)
  158.            {
  159.                printf("%2d:00 am\n", shift_data[i].end_hour);
  160.            }
  161.            else if(shift_data[i].end_hour==12)
  162.            {
  163.                printf("%2d:00 pm\n", shift_data[i].end_hour);
  164.            }
  165.  
  166.     }
  167. return 0;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement