Advertisement
Oddlyshapedtree

Untitled

Nov 28th, 2015
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 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[100];
  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. int main (void)
  32. {
  33.  
  34. int num_shifts;
  35.  
  36.  
  37. read_data(shift_data, &num_shifts);
  38.  
  39. sort_data(shift_data, num_shifts);
  40.  
  41. print_data(shift_data, num_shifts);
  42.  
  43.  
  44.  
  45.  
  46. return 0;
  47. }
  48.  
  49.  
  50.  
  51.  
  52. // Preconditions: array of structure "Shift" to store data
  53. // Postconditions: number of shifts read in from data file
  54. // Actions: Ask user for name of input file. Read the number
  55. // of shifts, then read in the data for all
  56. // of the shifts. Return the number of shifts.
  57. int read_data(struct Shift shift_data[], int *num_shifts)
  58. {
  59. int i;
  60. char infile_name[50];
  61. char shift_name[100], shift_day[100];
  62.  
  63.  
  64. printf("What is the name of your input file?\n"); //ask user for input file name
  65. scanf("%s", infile_name);
  66.  
  67. FILE *input; //declare file pointer
  68. input = fopen(infile_name, "r"); //open input file
  69.  
  70. fscanf(input, "%d", num_shifts);
  71.  
  72. for (i=0; i<*num_shifts; i++) // read in data
  73. {
  74. fscanf(input, "%s", &shift_data[i].name);
  75. fscanf(input, "%s", &shift_data[i].day_of_week);
  76. fscanf(input, "%d", &shift_data[i].start_hour);
  77. fscanf(input, "%d", &shift_data[i].end_hour);
  78.  
  79.  
  80. }
  81.  
  82.  
  83. fclose(input);
  84. return num_shifts;
  85. }
  86.  
  87.  
  88.  
  89.  
  90. // Preconditions: array of structure "Shift"
  91. // integer value indicating number of shifts
  92. // Postconditions: none - this function does not return anything.
  93. // Actions: Sort the shifts by the TA's first name.
  94.  
  95. void sort_data(struct Shift shift_data[], int *num_shifts)
  96. {
  97. int i;
  98.  
  99. for(i=0; i>num_shifts; i++)
  100. {
  101. if (i == 0)
  102. strcpy(shift_data[i].name, temp[i].name);
  103.  
  104. // Update if we find a new first name.
  105. else if (strcmp(temp[i].name, shift_data[i].name) < 0)
  106. strcpy(shift_data[i].name, temp[i].name);
  107. }
  108.  
  109.  
  110.  
  111.  
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement