简体   繁体   中英

I am writing a program which ask user to enter time in 24 hour format and diplay closest departute time of schedule flight

minutes,using the 24-hour clock) the pro gramme the displays the departure and arrival time for flight whose departure time is closet to that enter by user? departure time arrival time 8:00 am 10:16 am
9:43 am 11:52 am 11:19 am 1:31 pm 12:47 pm 3:00 pm 2:00 pm 4:08 pm 3:45 pm 5:55 pm 7:00 pm 9:20 pm 9:45 pm 11:58 pm

#include<stdio.h>
int main (void){

    int dept1,dept2,dept3,dept4,dept5,dept6,dept7,dept8,hh,mm,entertime;

    printf("Enter a time in 24-hour format:");
    scanf("%d:%d",&hh,&mm);


    dept1=8*60;
    dept2=9*60+43;
    dept3=11*60+19;
    dept4=12*60+47;
    dept5=14*60;
    dept6=15*60+45;
    dept7=19*60;
    dept8=21*60+45;

    entertime=hh*60+mm;

    if(entertime<=dept1){
        printf("Closet Departure time is 8:00 A.M,arriving at 10:16 A.M");
    }else if(entertime<=dept2){
        printf("Closet Departure time is 9:43 A.M,arriving at 11:52 A.M");
    }else if(entertime<=dept3){
        printf("Closet Departure time is 11:19 A.M,arriving at 1:31 P.M");
    }else if(entertime<=dept4){
        printf("Closet Departure time is 12:47 P.M,arriving at 3:00 P.M");
    }else if(entertime<=dept5){
        printf("Closet Departure time is 02:00 P.M,arriving at 4:08 P.M");
    }else if(entertime<=dept6){
        printf("Closet Departure time is 03:45 P.M,arriving at 5:55 P.M");
    }else if(entertime<=dept7){
        printf("Closet Departure time is 07:00 P.M,arriving at 9:20 P.M");
    }else if(entertime<=dept8){
        printf("Closet Departure time is 09:45 P.M,arriving at 11:58 P.M");
    }

        return 0;


}

i have expressed hour and minute in to for eg 13:15=13*60+15=795 minute so it would be closer to 12:47 pm which is 12*60+47=767 minutes

but not getting any output

problem is that if i enter 13:15(13*60+15=795) then the program should display 12:47 pm departure time

Since you want the departure time closest to the entered time, you must not compare entertime to the departure times, but to the times halfway between to successive departure times, eg instead of

    if(entertime<=dept1)

write

    if (entertime <= (dept1+dept2)/2)

and at last remove the

    if(entertime<=dept8)

to enable the user to get the latest flight when none of the earlier ones fits.

Using this way is much easier and you can also use a 2D array to make it even simpler.

#include<stdio.h>
int main (void){
int hh,mm,entertime;
int dept[8] = {8*60,9*60+43,11*60+19,12*60+47,14*60,15*60+45,19*60,21*60+45};
printf("Enter a time in 24-hour format:");
scanf("%d:%d",&hh,&mm);
entertime=hh*60+mm;

if(entertime<=(dept[0]+dept[1])/2 || entertime>=dept[7]&&entertime<2*60){
    printf("Closet Departure time is 8:00 A.M,arriving at 10:16 A.M");
}else if(entertime<=(dept[1]+dept[2])/2){
    printf("Closet Departure time is 9:43 A.M,arriving at 11:52 A.M");
}else if(entertime<=(dept[2]+dept[3])/2){
    printf("Closet Departure time is 11:19 A.M,arriving at 1:31 P.M");
}else if(entertime<=(dept[3]+dept[4])/2){
    printf("Closet Departure time is 12:47 P.M,arriving at 3:00 P.M");
}else if(entertime<=(dept[4]+dept[5])/2){
    printf("Closet Departure time is 02:00 P.M,arriving at 4:08 P.M");
}else if(entertime<=(dept[5]+dept[6])/2){
    printf("Closet Departure time is 03:45 P.M,arriving at 5:55 P.M");
}else if(entertime<=(dept[6]+dept[7])/2){
    printf("Closet Departure time is 07:00 P.M,arriving at 9:20 P.M");
}else if(entertime<=dept[7]){
    printf("Closet Departure time is 09:45 P.M,arriving at 11:58 P.M");
}else{
    printf("Not a valid time input");
}
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM