Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 5 project 8 #113

Open
elefrant1 opened this issue Mar 14, 2023 · 3 comments
Open

Chapter 5 project 8 #113

elefrant1 opened this issue Mar 14, 2023 · 3 comments

Comments

@elefrant1
Copy link

Just a thought.

Not sure if yesterdays flights are to be considered, but currently a user entered time of e.g. 00:45 will give 8:00 a.m. as the closest flight even if the last flight from the day before is closer in time (3 hours back in time to 9:45 p.m. vs 7 hours 15 minutes forward to 8:00 a.m.).

@rishabhh1809
Copy link

`
#include <stdio.h>
#include <stdlib.h>

#define lowest(a,b,c,d,e,f,g,h) (a<b && a<c && a<d && a<e && a<f && a<g && a<h)
#define middletime(i,j) ((((1440-j)+i)/2)+j)
#define inmin(k,l) (60 * k + l)

int main (void)
{
int hh, mm, rr, dh, dm, d, ah, am, a,
    dhfirst, dh2, dh3, dh4, dh5, dh6, dh7, dhlast,
    dmfirst, dm2, dm3, dm4, dm5, dm6, dm7, dmlast,
    dfirst, d2, d3, d4, d5, d6, d7, dlast,
    s1, s2, s3, s4, s5, s6, s7, s8,
    ahfirst, ah2, ah3, ah4, ah5, ah6, ah7, ahlast,
    amfirst, am2, am3, am4, am5, am6, am7, amlast,
    afirst, a2, a3, a4, a5, a6, a7, alast;

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

dhfirst = 8;    dmfirst = 0;     dfirst = inmin(8,dmfirst);
ahfirst = 10;   amfirst = 16;    afirst = inmin(10,amfirst);

dhlast = 9;    dmlast = 45;    dlast = inmin(21,dmlast);   
ahlast = 11;   amlast = 58;    alast = inmin(23,amlast);

if (inmin(hh,mm)>dfirst && inmin(hh,mm)<dlast)
    rr = inmin(hh,mm);
else if (1440<middletime(dfirst,dlast))
    if (inmin(hh,mm)>middletime(dfirst,dlast)-1440 && inmin(hh,mm)<dfirst)
        rr = dfirst + 1;
    else
        rr = dlast - 1;
else if (1440>middletime(dfirst,dlast))
    if (inmin(hh,mm)<middletime(dfirst,dlast) && inmin(hh,mm)>dlast)
        rr = dlast - 1;
    else
        rr = dfirst + 1;
else if (1440==middletime(dfirst,dlast))
    if (inmin(hh,mm)>dlast) 
        rr = dlast - 1;
    else
        rr = dfirst + 1;
else
    rr = inmin(hh,mm);

                                                s1 = abs(dfirst-rr);
dh2 = 9;    dm2 = 43;    d2 = inmin(9,dm2);     s2 = abs(d2-rr);   ah2 = 11;   am2 = 52;    a2 = inmin(11,am2);
dh3 = 11;   dm3 = 19;    d3 = inmin(11,dm3);    s3 = abs(d3-rr);   ah3 = 1;    am3 = 31;    a3 = inmin(13,am3);
dh4 = 12;   dm4 = 47;    d4 = inmin(12,dm4);    s4 = abs(d4-rr);   ah4 = 3;    am4 = 0;     a4 = inmin(15,am4);
dh5 = 2;    dm5 = 0;     d5 = inmin(14,dm5);    s5 = abs(d5-rr);   ah5 = 4;    am5 = 8;     a5 = inmin(16,am5);
dh6 = 3;    dm6 = 45;    d6 = inmin(15,dm6);    s6 = abs(d6-rr);   ah6 = 5;    am6 = 55;    a6 = inmin(17,am6);
dh7 = 7;    dm7 = 0;     d7 = inmin(19,dm7);    s7 = abs(d7-rr);   ah7 = 9;    am7 = 20;    a7 = inmin(21,am7);
                                                s8 = abs(dlast-rr);

if (lowest(s1,s2,s3,s4,s5,s6,s7,s8))
    {dh=dhfirst; dm=dmfirst; ah=ahfirst; am=amfirst; d=dfirst; a=afirst;}
else if (lowest(s2,s1,s3,s4,s5,s6,s7,s8))
    {dh=dh2; dm=dm2; ah=ah2; am=am2; d=d2; a=a2;}
else if (lowest(s3,s2,s1,s4,s5,s6,s7,s8))
    {dh=dh3; dm=dm3; ah=ah3; am=am3; d=d3; a=a3;}
else if (lowest(s4,s2,s3,s1,s5,s6,s7,s8))
    {dh=dh4; dm=dm4; ah=ah4; am=am4; d=d4; a=a4;}
else if (lowest(s5,s2,s3,s4,s1,s6,s7,s8))
    {dh=dh5; dm=dm5; ah=ah5; am=am5; d=d5; a=a5;}
else if (lowest(s6,s2,s3,s4,s5,s1,s7,s8))
    {dh=dh6; dm=dm6; ah=ah6; am=am6; d=d6; a=a6;}
else if (lowest(s7,s2,s3,s4,s5,s6,s1,s8))
    {dh=dh7; dm=dm7; ah=ah7; am=am7; d=d7; a=a7;}
else
    {dh=dhlast; dm=dmlast; ah=ahlast; am=amlast; d=dlast; a=alast;}

printf("Closest departure is ");

if (d<720 && a<720)
    printf("%d:%0.2d a.m., arriving at %d:%0.2d a.m.\n", dh, dm, ah, am);
else if (d<720 && a>720)
    printf("%d:%0.2d a.m., arriving at %d:%0.2d p.m.\n", dh, dm, ah, am);
else if (d>720 && a<720)
    printf("%d:%0.2d p.m., arriving at %d:%0.2d a.m.\n", dh, dm, ah, am);
else
    printf("%d:%0.2d p.m., arriving at %d:%0.2d p.m.\n", dh, dm, ah, am);

return 0;
}

`

@Blank0211
Copy link

What is that code? that looks needlessly complicated. Does it even work?

@Blank0211
Copy link

Blank0211 commented Oct 24, 2023

Just a thought.

Not sure if yesterdays flights are to be considered, but currently a user entered time of e.g. 00:45 will give 8:00 a.m. as the closest flight even if the last flight from the day before is closer in time (3 hours back in time to 9:45 p.m. vs 7 hours 15 minutes forward to 8:00 a.m.).

if (user_time < 173) printf(...), a simple check like this in the beginning, solves the problem. i.e. if time < 2:53, which is the middle point between the two departures.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants