简体   繁体   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

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? 分钟,使用24小时制),程序将显示其出发时间与用户输入的时间相近的航班的出发和到达时间? departure time arrival time 8:00 am 10:16 am 出发时间到达时间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 上午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;


}

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

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

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

    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. 使用这种方法要容易得多,您还可以使用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.

相关问题 给定 -hour AM/PM 格式的时间,将其转换为军用(24 小时)时间 - Given a time in -hour AM/PM format, convert it to military (24-hour) time 12小时AM / PM格式转换为军事(24小时)时间 - 12-hour AM/PM format to military (24-hour) time 我正在编写一个程序,它输出用户输入的最早日期,但出现了一些问题 - I am writing a program which outputs the earliest date the user enter but some problems occurs 正确处理24小时时间 - Handling 24 Hour Time Properly 24小时时间格式 - 24 hours time format 我正在编写一个程序,在该程序中,用户输入金额并显示应收税款,以根据每个收入组计算应缴税额? - I am writing a programm in which user enter a amount and programm display tax due calculatig percntage of tax as per thier income group? 嗨,我需要让用户输入 3 个值,直到他输入 EOF(Ctrl+z)。EOF 部分不起作用,用户每次输入 6 个值,我需要 3 个 - Hi,I need to ask the user to enter 3 values until he enters EOF(Ctrl+z).The EOF part isn't working and the user enters 6 values each time and I need 3 我需要一个要求用户输入图钉的功能,经过3次错误尝试后,他们编程终止 - I need a function that ask the user to enter a pin and after 3 wrong attempts, they program terminates 我正在编写一个小的 c 程序来进行线程同步 - I am writing a small c program which does thread synchronization 我的时间表表程序中的任何建议 - any proposal in my time schedule table program
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM