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条回答
Root(大扎)
2楼-- · 2020-04-21 08:31

@aussie_aj; 1.Enter time 1:00 a.m. in ur program n u will get Departure time 8:00 a.m. instead 9:45 p.m.!! 2.Enter 12:10 p.m. in ur program n see the MAGIC!! :)

#include <stdio.h>

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 - 0;
    d1 = 480; d2 = 1305;
    a1 = 616; a2 = 1438;
}
else if(t >=480 && 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();

}

查看更多
祖国的老花朵
3楼-- · 2020-04-21 08:33

A sensible answer requires the use of arrays and functions. You also don't seem to be looking nearest very well.

The data could be represented like this, using a macro to hide the repetitive calculation:

#define TIME(hh, mm) ((hh) * 60 + (mm))

static const int dep[] = { TIME( 8,  0), TIME( 9, 45), TIME(11, 19), TIME(12, 47),
                           TIME(14,  0), TIME(15, 45), TIME(19,  0), TIME(19, 45) };
static const int arr[] = { TIME(10, 16), TIME(11, 52), TIME(13, 31), TIME(15,  0),
                           TIME(16,  8), TIME(17, 55), TIME(21, 20), TIME(23, 58) };
enum { NUM_TIMES = sizeof(dep) / sizeof(dep[0]) };

You should also use a function to format the time as a string. Note that the am/pm times are weird; you have to substitute 12 for 0 (so 12:59 am comes just before 1:00 am). The function assumes that the buffer it is passed is big enough (9 bytes minimum) to take the formatted string.

char *timestr(int hhmm, char *buffer)
{
    int hh = (hhmm / 60) % 12;
    if (hh == 0)
        hh = 12;
    sprintf(buffer, "%.2d:%.2d %s", hh, hhmm % 60, (hhmm < TIME(12, 0)) ? "am" : "pm");
    return buffer;
}

Now, given the value read, you can find the the right value fairly simply:

void find_and_print_nearest_time(int time)
{
    int i = 0;
    if (time < dep[i])
        // Before first departure
        print_times(time, dep[i], arr[i]);
    else if (time >= dep[NUM_TIMES-1])
        // After last departure
        print_times(time, dep[NUM_TIMES-1], arr[NUM_TIMES-1]);
    else
    {
        // Somewhere in the middle
        for (i = 0; i < NUM_TIMES - 1; i++)
        {
            if (time >= dep[i+1])
                continue;
            if (time < (dep[i] + dep[i+1]) / 2)
                print_times(time, dep[i], arr[i]);
            else if (time < dep[i+1])
                print_times(time, dep[i+1], arr[i+1]);
            break;
        }
    }
}

void print_times(int u_time, int dep, int arr)
{
    char buffer1[9];
    char buffer2[9];
    char buffer3[9];
    printf("Given %s, the nearest departure time is %s, arriving at %s\n",
           timestr(u_time, buffer1), timestr(dep, buffer2), timestr(arr, buffer3));
}

Leaving with just the main() to write:

#include <stdio.h>

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

    // Get the user's time
    printf("Enter a 24 hour time (hh:mm): \n");
    if (scanf("%d:%d", &hours, &minutes) == 2)
    {
        time = hours * 60 + minutes;
        find_and_print_nearest_time(time);
    }
    return(0);
}

Or, with a loop (easier for testing!):

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

    while (scanf("%d:%d", &hours, &minutes) == 2)
    {
        // Should check 0 <= hours <= 23
        // Should check 0 <= minutes <= 59
        // Might accept 24:00
        time = TIME(hours, minutes);
        find_and_print_nearest_time(time);
    }
    return(0);
}

When testing, I assembled things in a slightly different order, but the code worked as I expected first time. Given input data:

00:00
06:00
08:00
08:50
08:51
08:52
08:53
08:54
08:55
09:42
09:43
09:44
10:30
10:31
10:32
10:33
14:50
14:51
14:52
14:53
14:54
14:55
21:44
21:45
21:46
23:59

The output was:

Given 12:00 am, the nearest departure time is 08:00 am, arriving at 10:16 am
Given 06:00 am, the nearest departure time is 08:00 am, arriving at 10:16 am
Given 08:00 am, the nearest departure time is 08:00 am, arriving at 10:16 am
Given 08:50 am, the nearest departure time is 08:00 am, arriving at 10:16 am
Given 08:51 am, the nearest departure time is 08:00 am, arriving at 10:16 am
Given 08:52 am, the nearest departure time is 09:45 am, arriving at 11:52 am
Given 08:53 am, the nearest departure time is 09:45 am, arriving at 11:52 am
Given 08:54 am, the nearest departure time is 09:45 am, arriving at 11:52 am
Given 08:55 am, the nearest departure time is 09:45 am, arriving at 11:52 am
Given 09:42 am, the nearest departure time is 09:45 am, arriving at 11:52 am
Given 09:43 am, the nearest departure time is 09:45 am, arriving at 11:52 am
Given 09:44 am, the nearest departure time is 09:45 am, arriving at 11:52 am
Given 10:30 am, the nearest departure time is 09:45 am, arriving at 11:52 am
Given 10:31 am, the nearest departure time is 09:45 am, arriving at 11:52 am
Given 10:32 am, the nearest departure time is 11:19 am, arriving at 01:31 pm
Given 10:33 am, the nearest departure time is 11:19 am, arriving at 01:31 pm
Given 02:50 pm, the nearest departure time is 02:00 pm, arriving at 04:08 pm
Given 02:51 pm, the nearest departure time is 02:00 pm, arriving at 04:08 pm
Given 02:52 pm, the nearest departure time is 03:45 pm, arriving at 05:55 pm
Given 02:53 pm, the nearest departure time is 03:45 pm, arriving at 05:55 pm
Given 02:54 pm, the nearest departure time is 03:45 pm, arriving at 05:55 pm
Given 02:55 pm, the nearest departure time is 03:45 pm, arriving at 05:55 pm
Given 09:44 pm, the nearest departure time is 07:45 pm, arriving at 11:58 pm
Given 09:45 pm, the nearest departure time is 07:45 pm, arriving at 11:58 pm
Given 09:46 pm, the nearest departure time is 07:45 pm, arriving at 11:58 pm
Given 11:59 pm, the nearest departure time is 07:45 pm, arriving at 11:58 pm
查看更多
登录 后发表回答