我正在编写一个程序,要求用户以24小时格式输入时间并显示最近的航班时刻的离开时间

时间:2014-04-05 09:16:17

标签: c

分钟,使用24小时制时钟表示,显示出发时间与用户输入时间相关的航班起飞和到达时间? 出发时间到达时间 上午8:00 10:16上午 上午9:43 11:52 am 上午11:19至下午1:31 下午12:47下午3:00 下午2:00 4:08 pm 下午3:45 5:55 晚上7点多9点20分 晚上9:45 pm 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分钟

但没有获得任何输出

2 个答案:

答案 0 :(得分:0)

  

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

由于您希望出发时间最接近输入时间,因此您不能将entertime与出发时间进行比较,而应与连续出发时间之间的时间进行比较,例如: G。而不是

    if(entertime<=dept1)

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

最后删除

    if(entertime<=dept8)

使用户能够获得最新的航班,而这些航班都不适合。

答案 1 :(得分:0)

使用这种方式更容易,您也可以使用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;
}