If statement and expressions question advice

2020-04-21 07:48发布

I just did this question out of KN King's C Programming: A Modern Aprroach. It's not homework, I'm just teaching myself out of the book...

The following table shows the daily flights from one city to another:
Departure time - Arrival time
8:00am - 10:16am
9:43am - 11:52am
11:19am - 1:31pm
12:47pm - 3:00pm
2:00pm - 4:08pm
3:45pm - 5:55pm
7:00pm - 9:20pm
9:45pm - 11:58pm

Write a program that asks user to enter a time (expressed in hours and minutes, using the 24 hour clock). The program then displays the departure and arrival times for the flight whose departure time is closest to that entered by the user:

Enter a 24 hour time: 13:15
Closest depature time is 12:47pm., arriving at 3:00pm.

Hint: Convert the input into a time expressed in minutes since midnight, and compare it to the departure times, also expressed in minutes since midnight. For example, 13:15 is 13 x 60 + 15 = 795 minutes since midnight, which is closer to 12:47pm (767 minutes since midnight) than to any of the other departure times.

So far we have only covered basic comparison expressions and the if and switch statement, so my answer has to be based around these obviously and nothing too fancy. My code that I've come up with is below, I'm wondering if someone would be willing to take a look at it and see if I'm on the right track, it seems to work but seems to be a lot of code for such a small thing. Maybe it was just to teach us the logic involved. I haven't pasted the whole code, the rest is just the same thing over and over as it compares the values. I have no programming experience, so please be gentle!

Thanks for your time, Andrew

#include <stdio.h>

int main (int argc, const char * argv[]) {


// Flight departure times since midnight
// 8am, 9:45am, 11:19am, 12:47pm
// 2pm, 3:45pm, 7pm, 7:45pm
int a = 480, b = 585, c = 679, d = 767,
    e = 840, f = 945, g = 1140, h = 1185;

// Flight arrival times for respective departure times.
int a1 = 616, b1 = 712, c1 = 811, d1 = 900,
    e1 = 968, f1 = 1075, g1 = 1280, h1 = 1438;

int hours, minutes, time, t, u;

// Get the users time

printf("Enter a 24 hour time (hh:mm): \n");
scanf("%d:%d", &hours, &minutes);

time = hours * 60 + minutes;

printf("Closest departure time is ");

if (time <= a)
    printf("8:00am");
else
    if (time > a && time <= b) {
                t = time - a; 
                u = b - time;
                if (t < u) {
                    printf("%.2d:%.2d", a / 60, a % 60);
                        if (a / 60 == 0)
                            printf("am");
                        else if (a / 60 < 12)
                            printf("am");
                        else if (a / 60 == 12)
                            printf("pm");
                        else
                            printf("pm");
                    printf(", arriving at %d:%.2d", a1 / 60, a1 % 60);
                        if (a1 / 60 == 0)
                            printf("am");
                        else if (a1 / 60 < 12)
                            printf("am");
                        else if (a1 / 60 == 12)
                            printf("pm");
                        else
                            printf("pm");
                }
                else {
                        printf("%.2d:%.2d", b / 60, b % 60);
                        if (b / 60 == 0)
                            printf("am");
                        else if (b / 60 < 12)
                            printf("am");
                        else if (b / 60 == 12)
                            printf("pm");
                        else
                            printf("pm");
                    printf(", arriving at %d:%.2d", b1 / 60, b1 % 60);
                        if (b1 / 60 == 0)
                            printf("am");
                        else if (b1 / 60 < 12)
                            printf("am");
                        else if (b1 / 60 == 12)
                            printf("pm");
                        else
                            printf("pm");
                }
    }

Changes as per advice I've been given: (Thanks xamypah and Gabe) ...

int hours, minutes, time, t, u, x, y;

// Get the users time

printf("Enter a 24 hour time (hh:mm): \n");
scanf("%d:%d", &hours, &minutes);

time = hours * 60 + minutes;

printf("Closest departure time is ");

if (time <= a)
    printf("8:00am");
else
    if (time > a && time <= b) {
                t = time - a; 
                u = b - time;

                if (t <= u) {
                    x = a;
                    y = a1;
                }
                else {
                    x = b;
                    y = b1;
                }

Then at the end of the program after several of the above:

printf("%.2d:%.2d", x / 60, x % 60);

if (x / 60 < 12)
    printf("am");
else 
    printf("pm");
    printf(", arriving at %d:%.2d", y / 60, y % 60);

if (y / 60 < 12)
    printf("am");
else
    printf("pm");

Actually I had to make some changes to that end print statement, or else it was displaying the times in 24 hour format with am and pm after:

if (x / 60 < 12)
    printf("%d:%.2d am", x / 60, x % 60);
else 
    printf("%d:%.2d pm", (x / 60) - 12, x % 60);

printf(", arriving at ");

if (y / 60 < 12)
    printf("%d:%.2d am", y / 60, y % 60);
else
    printf("%d:%.2d pm", (y / 60) - 12, y % 60);

8条回答
▲ chillily
2楼-- · 2020-04-21 08:16

Made changes to make it more perfect

include

int main()
{
int h,m,t,temp1,temp2,
    a1,a2,d1,d2;      //a1,a1 & d1,d2 are ariival & dep. time res.

//Departure times
// dt1 = 480, dt2 = 583, dt3 = 679, dt = 767, 
// dt5 = 840, dt6 = 945, dt = 1140, dt = 1305;

//Arrival times    
// at1 = 616, at2 = 712, at3 = 811, at4 = 900, 
// at5 = 968, at6 = 1075, at7 = 1280, at8 = 1438;

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

t = h*60 + m;

printf("Closest departure time is ");

//CONDITIONS FOR CLOSEST DEPARTURE TIME.
if(t <= 480)
{
    temp1 = 480 - t;
    temp2 = t + 135; //Since time is 9:45p.m and not midnight;
    d1 = 480; d2 = 1305;
    a1 = 616; a2 = 1438;
}
else if(t <= 583) 
{
     temp1 = 583 - t;
     temp2 = t - 480;
     d1 = 583; d2 = 480;
     a1 = 712; a2 = 616;
}
else if(t <= 679)
{
     temp1 = 679 - t;
     temp2 = t - 582;
     d1 = 679; d2 = 582;
     a1 = 811; a2 = 712;
}
else if(t <= 767)
{
     temp1 = 767 - t;
     temp2 = t - 679;
     d1 = 767; d2 = 679;
     a1 = 900; a2 = 811;
}
else if(t <= 840)
{
     temp1 = 840 - t;
     temp2 = t - 767;
     d1 = 840; d2 = 767;
     a1 = 968; a2 = 900;
}
else if(t <= 945)
{
     temp1 = 945 - t;
     temp2 = t - 840;
     d1 = 945; d2 = 840;
     a1 = 1075; a2 = 968;
} 
else if(t <= 1140)
{
     temp1 = 1140 - t;
     temp2 = t - 945;
     d1 = 1140; d2 = 945;
     a1 = 1280; a2 = 1075;
}
else if(t <= 1305 || t >= 1305)
{
     temp1 = 1305 - t;
     temp2 = t - 1140;
     d1 = 1305; d2 = 1140;
     a1 = 1438; a2 = 1280;
}

// COMPUTING CLOSEST DEPARTURE TIME.
if(temp1 < temp2)
{
     h = d1/60;
     m = d1%60;
}
else
{
    h = d2/60;
    m = d2%60;
}

//PRINTING CLOSEST DEPARTURE TIME.

if(h < 12)
     printf("%.2d:%.2d a.m.",h,m);
else
{
    if(h == 12)
         printf("%.2d:%.2d p.m.",h,m);
    else         
        printf("%.2d:%.2d p.m.",h - 12,m);
}

//COMPUTING CORRESPONDING ARRIVAL TIME.
printf(", arriving at ");
if(temp1 < temp2)
{
     h = a1/60;
     m = a1%60;
}
else
{
    h = a2/60;
    m = a2%60;
}

//PRINTING CORRESPONDING ARRIVAL TIME.
 if(h < 12)
     printf("%.2d:%.2d a.m.",h,m);
else
{
    if(h == 12)
         printf("%.2d:%.2d p.m.",h,m);
    else         
        printf("%.2d:%.2d p.m.",h - 12,m);
}                         
getch();

}

查看更多
Melony?
3楼-- · 2020-04-21 08:16

1.write a program that asks the user for a 12-hour time, the display the time in 24-hour form.

Enter a 12-hour time: 9:11 PM

Equivalent 24-hour time: 21:11

查看更多
做自己的国王
4楼-- · 2020-04-21 08:19

Well, yes you are on the right track, but you also can significantly reduce the size of code. Look, the same piece of code, I think, is repeated very often in your program:

                    printf("%.2d:%.2d", a / 60, a % 60);
                        if (a / 60 == 0)
                            printf("am");
                        else if (a / 60 < 12)
                            printf("am");
                        else if (a / 60 == 12)
                            printf("pm");
                        else
                            printf("pm");
                    printf(", arriving at %d:%.2d", a1 / 60, a1 % 60);
                        if (a1 / 60 == 0)
                            printf("am");
                        else if (a1 / 60 < 12)
                            printf("am");
                        else if (a1 / 60 == 12)
                            printf("pm");
                        else
                            printf("pm");

It is repeated for a, for b etc...

And it is usually done at the end of the execution.

So, if you break your algorithm on two parts: first - finding the right time range, and the second - printing it out, then you get rid off this copy/pasting.

查看更多
在下西门庆
5楼-- · 2020-04-21 08:20

My approach:

#include <stdio.h>

int main(void)
{
    int hours, minutes, time;

    printf("Enter a 24-hour time: ");
    scanf("%d:%d", &hours, &minutes);

    time = hours * 60 + minutes;

    if(time < /*hour in minutes*/8*60+/*minutes*/0+((/*minutes to the next hour*/60-0+/*more hours in minutes?*/0+/*extra minutes*/43)/*dividing by 2 gives the half of time*/2.0)){
        printf("Closest departure time is 8:00 a.m, arriving at 10:16 a.m.\n");
    }
    else if(time < 9*60+43+((60-43+60+19)/2.0)){
        printf("Closest departure time is 9:43 a.m, arriving at 11:52 a.m.\n");
    }
    else if(time < 11*60+19+((60-19+0+47)/2.0)){
        printf("Closest departure time is 11:19 a.m, arriving at 1:31 p.m.\n");
    }
    else if(time < 12*60+47+((60-47+60+0)/2.0)){
        printf("Closest departure time is 12:47 p.m, arriving at 3:00 p.m.\n");
    }
    else if(time < 14*60+0+((60-0+0+45)/2.0)){
        printf("Closest departure time is 2:00 p.m, arriving at 4:08 p.m.\n");
    }
    else if(time < 15*60+45+((60-45+60*3+0)/2.0)){
        printf("Closest departure time is 3:45 p.m, arriving at 5:55 p.m.\n");
    }
    else if(time < 19*60+45+((60-0+60+45)/2.0)){
        printf("Closest departure time is 7:00 p.m, arriving at 9:20 p.m.\n");
    }
    else{
        printf("Closest departure time is 9:45 p.m, arriving at 11:58 p.m.\n");
    }

  return 0;
}
查看更多
\"骚年 ilove
6楼-- · 2020-04-21 08:21

Look closely at this code:

if (b / 60 == 0)
    printf("am");
else if (b / 60 < 12)
    printf("am");

Do you see why it's redundant?

Since 0 < 12, anytime b / 60 == 0 is true, b / 60 < 12 will also be true. You can reduce your code to if (b / 60 < 12) printf("am"); else printf("pm");

查看更多
叛逆
7楼-- · 2020-04-21 08:22

I think you need a loop expression to sort and compute the nearest time point. And you can pack your condition expression into a function. Like this:

char* getTimeSuffix(int time) {
    char* ret = "pm"
    if ((time/60 == 0) || (time/60 < 12))
        return "am";
    return ret;
}
查看更多
登录 后发表回答