Advertisement
Oddlyshapedtree

Untitled

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