Advertisement
Oddlyshapedtree

Untitled

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