GithubHelp home page GithubHelp logo

Chapter 5 project 8 about c-solutions HOT 3 OPEN

elefrant1 avatar elefrant1 commented on July 17, 2024 2
Chapter 5 project 8

from c-solutions.

Comments (3)

rishabhh1809 avatar rishabhh1809 commented on July 17, 2024 1

`
#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;
}

`

from c-solutions.

Blank0211 avatar Blank0211 commented on July 17, 2024 1

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.

from c-solutions.

Blank0211 avatar Blank0211 commented on July 17, 2024

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

from c-solutions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.