Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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;
- int read_data(struct Shift shift_data[], int *num_shifts);
- void sort_data(struct Shift shift_data[], int num_shifts);
- void sort_data(struct Shift shift_data[], int num_shifts);
- int main()
- {
- int num_shifts;
- read_data(shift_data, &num_shifts);
- // void sort_data(struct Shift shift_data[], int 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)
- {
- char input_schedule[100];
- int i;
- printf("What is the name of the file?\n");
- scanf("%s",input_schedule);
- FILE *ifp;
- ifp=fopen(input_schedule,"r");
- fscanf(ifp,"%d",num_shifts);
- printf("numshifts = %d", num_shifts);
- for(i=0;i<*num_shifts;i++)
- {
- fscanf(ifp,"%s",&shift_data[i].name);
- fscanf(ifp,"%s",&shift_data[i].day_of_week);
- fscanf(ifp,"%d",&shift_data[i].start_hour);
- fscanf(ifp,"%d",&shift_data[i].end_hour);
- }
- fclose(ifp);
- 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,j;
- for(i=0;i<num_shifts;i++)
- {
- for(j=i+1;j<num_shifts;j++)
- {
- if(strcmp(shift_data[i].name,shift_data[i].name)<0)
- {
- temp[i]=shift_data[i];
- shift_data[i]=shift_data[j];
- shift_data[i]=temp[i];
- }
- if(strcmp(shift_data[i].day_of_week,shift_data[i].day_of_week)<0)
- {
- temp[i]=shift_data[i];
- shift_data[i]=shift_data[j];
- shift_data[i]=temp[i];
- }
- if(strcmp(shift_data[i].start_hour,shift_data[i].start_hour)<0)
- {
- temp[i]=shift_data[i];
- shift_data[i]=shift_data[j];
- shift_data[i]=temp[i];
- }
- if(strcmp(shift_data[i].end_hour,shift_data[i].end_hour)<0)
- {
- temp[i]=shift_data[i];
- shift_data[i]=shift_data[j];
- shift_data[i]=temp[i];
- }
- }
- }
- */
- // 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\t",shift_data[i].name);
- printf("%-8s\t",shift_data[i].day_of_week);
- if(shift_data[i].start_hour>12)
- {
- shift_data[i].start_hour=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=shift_data[i].end_hour-12;
- printf("%3d:00 pm\n", shift_data[i].end_hour);
- }
- else if(shift_data[i].end_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);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement