Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- David Valladares
- HW#7
- COP 3223 Section 2
- Annie Wu
- */
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- struct Shift
- {
- char name[100];
- char day_of_week[100];
- int start_hour;
- int end_hour;
- };
- struct Shift shift_data[100];
- struct Shift temp[100];
- int read_data(struct Shift shift_data[], int *num_shifts);
- void sort_data(struct Shift shift_data[], int *num_shifts);
- void print_data(struct Shift shift[], int *num_shifts);
- int main (void)
- {
- int num_shifts;
- read_data(shift_data, &num_shifts);
- sort_data(shift_data, num_shifts);
- print_data(shift_data, num_shifts);
- return 0;
- }
- // Preconditions: array of structure "Shift" to store data
- // Postconditions: number of shifts read in from data file
- // Actions: Ask user for name of input file. Read the number
- // of shifts, then read in the data for all
- // of the shifts. Return the number of shifts.
- int read_data(struct Shift shift_data[], int *num_shifts)
- {
- int i;
- char infile_name[50];
- char shift_name[100], shift_day[100];
- printf("What is the name of your input file?\n"); //ask user for input file name
- scanf("%s", infile_name);
- FILE *input; //declare file pointer
- input = fopen(infile_name, "r"); //open input file
- fscanf(input, "%d", num_shifts);
- for (i=0; i<*num_shifts; i++) // read in data
- {
- fscanf(input, "%s", &shift_data[i].name);
- fscanf(input, "%s", &shift_data[i].day_of_week);
- fscanf(input, "%d", &shift_data[i].start_hour);
- fscanf(input, "%d", &shift_data[i].end_hour);
- }
- fclose(input);
- return num_shifts;
- }
- // Preconditions: array of structure "Shift"
- // integer value indicating number of shifts
- // Postconditions: none - this function does not return anything.
- // Actions: Sort the shifts by the TA's first name.
- void sort_data(struct Shift shift_data[], int *num_shifts)
- {
- int i;
- for(i=0; i>num_shifts; i++)
- {
- if (i == 0)
- strcpy(shift_data[i].name, temp[i].name);
- // Update if we find a new first name.
- else if (strcmp(temp[i].name, shift_data[i].name) < 0)
- strcpy(shift_data[i].name, temp[i].name);
- }
- }
- // Preconditions: array of structure "Shift"
- // integer value indicating number of shifts
- // Postconditions: none - this function does not return anything.
- // Actions: Print the sorted data in the format described below.
- void print_data(struct Shift shift[], int *num_shifts)
- {
- int i;
- printf("\n\n\n");
- for (i=0; i<num_shifts; i++)
- {
- printf("%-5s\t", shift_data[i].name);
- printf("%-8s\t", shift_data[i].day_of_week);
- //decide whether to convert time value and place am and pm in correct locations
- if (shift_data[i].start_hour > 12)
- {
- shift_data[i].start_hour -= 12;
- printf("%3d:00 pm to ", shift_data[i].start_hour);
- }
- else if (shift_data[i].start_hour < 12)
- {
- printf("%3d:00 am to ", shift_data[i].start_hour);
- }
- else if (shift_data[i].start_hour == 12)
- {
- printf("%3d:00 pm to ", shift_data[i].start_hour);
- }
- if (shift_data[i].end_hour > 12)
- {
- shift_data[i].end_hour -= 12;
- printf("%3d:00 pm\n", shift_data[i].end_hour);
- }
- else if (shift_data[i].start_hour < 12)
- {
- printf("%3d:00 am\n", shift_data[i].end_hour);
- }
- else if (shift_data[i].end_hour == 12)
- {
- printf("%3d:00 pm\n", shift_data[i].end_hour);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement