繁体   English   中英

我正在编写一个程序,要求用户以24小时格式输入时间并显示计划航班的最接近出发时间

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

分钟,使用24小时制),程序将显示其出发时间与用户输入的时间相近的航班的出发和到达时间? 出发时间到达时间8:00 am 10:16 am
上午9:43上午11:52上午11:19下午1:31 12:47下午3:00 pm 2:00 pm 4:08 pm 3:45 pm 5:55 pm 7:00 pm 9:20 pm 9:下午45时11:58下午

#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;


}

我已将小时和分钟表示为例如13:15 = 13 * 60 + 15 = 795分钟,因此它将接近12:47 pm,即12 * 60 + 47 = 767分钟

但没有得到任何输出

问题是,如果我输入13:15(13 * 60 + 15 = 795),则程序应显示12:47 pm出发时间

既然你想最接近输入的时间出发的时间,你不能比较entertime到发车时间,但到了次中途之间连续的发车时间,如代替

    if(entertime<=dept1)

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

最后删除

    if(entertime<=dept8)

以使用户能够在较早的航班都不适合时获得最新的航班。

使用这种方法要容易得多,您还可以使用2D阵列使其变得更加简单。

#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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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