Advertisement
Oddlyshapedtree

Untitled

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