Home > database >  difference of 00:00 (24:00) and 23:00
difference of 00:00 (24:00) and 23:00

Time:09-22

I have written a program that basically calculates which trains could a person transfer to from the current train he is boarding... we have train A B C that run daily at a known hour and minute. a person needs 5 minutes to walk to a different train so any train that departs less than 5 minutes after the arrival of another train cannot be transferred to, another restriction is that if the arrival of two trains have a difference greater than 3 hours between them they are considered not able of transfer, and of course the obvious restriction is that is the train we want to transfer to even there when our train arrives.. sample inputs and outputs will be below for more clarification

Enter the time of arrival of train A:
12:00.
Enter the departure time of the A train:
12:30
Enter the arrival time of train B:
12:10
Enter the departure time of train B:
12:20
Enter the arrival time of train C:
12:40
Enter the departure time of train C:
13:00
It is possible to change from train A to trains B and C.
It is possible to change from train B to trains A and C.
You cannot change from train C.
Enter the arrival time of train A:
10:00
Enter the departure time of the A train:
10:10
Enter the arrival time of train B:
10:05
Enter the departure time of train B:
10:15
Enter the arrival time of train C:
10:06
Enter the departure time of train C:
10:16
It is possible to change from train A to trains B and C.
It is possible to change from train B to trains A and C.
You can change from train C to train B.

my code works when it comes to these samples however when we reach the case where one arrives before midnight and the other one arrives after midnight I can't seem to find away to make the program understand that the difference between for example 23:00 and 0:0 is just 60 minutes rather than 1380

Enter the time of arrival of train A:
23:00
Enter the departure time of the A train:
23:30
Enter the arrival time of train B:
23:50
Enter the departure time of train B:
0:15
Enter the arrival time of train C:
0:10
Enter the departure time of train C:
0:45
It is possible to change from train A to trains B and C.
You can change from train B to train C.
You can change from train C to train B.
int CanTransfer(int xin,int xout,int yin, int yout);

int main()
{
 int ain, aout, bin, bout, cin, cout, inh, inm;
 int ans1, ans2;

// --------- A train -----------
 
 printf("Enter the arrival time of train A:\n");
 scanf("%d:%d", &inh, &inm);
 ain = inh*60 inm;

 printf("Enter the departure time of the A train:\n");
 scanf("%d:%d", &inh, &inm);
 aout = inh*60 inm;


// --------- B train -----------

 printf("Enter the arrival time of train B:\n");
 scanf("%d:%d", &inh, &inm);
 bin = inh*60 inm;

 printf("Enter the departure time of the B train:\n");
 scanf("%d:%d", &inh, &inm);
 bout= inh*60 inm;

// --------- C train -----------

 printf("Enter the arrival time of train C:\n");
 scanf("%d:%d", &inh, &inm);
 cin = inh*60 inm;

 printf("Enter the departure time of the C train:\n");
 scanf("%d:%d", &inh, &inm);
 cout= inh*60 inm;

// --------- A train -----------

 ans1 = CanTransfer(ain, aout, bin, bout);
 ans2 = CanTransfer(ain, aout, cin, cout);

 if (ans1 && ans2)
 printf("It is possible to change from train A to trains B and C\n");
 else if (ans1)
 printf("It is possible to change from train A to trains B\n");
 else if (ans2)
 printf("It is possible to change from train A to trains C\n");
 else
 printf("You cannot change from train A\n");

// --------- B train -----------

 ans1 = CanTransfer(bin, bout, ain, aout);
 ans2 = CanTransfer(bin, bout, cin, cout);

 if (ans1 && ans2)
 printf("It is possible to change from train B to trains A and C\n");
 else if (ans1)
 printf("It is possible to change from train B to trains A\n");
 else if (ans2)
 printf("It is possible to change from train B to trains C\n");
 else
 printf("You cannot change from train B\n");

// --------- C train -----------
 ans1 = CanTransfer(cin, cout, ain, aout);
 ans2 = CanTransfer(cin, cout, bin, bout);

 if (ans1 && ans2)
 printf("It is possible to change from train C to trains A and B\n");
 else if (ans1)
 printf("It is possible to change from train C to trains A\n");
 else if (ans2)
 printf("It is possible to change from train C to trains B\n");
 else
 printf("You cannot change from train C\n");

return 0;
}

int CanTransfer(int xin,int xout,int yin, int yout)
{
    if (xin < yout && yout - xin >= 5 && yin - xin <= 180)
    return 1;
    else 
    return 0;   
}

any ideas?

CodePudding user response:

The problem can be solved with a bit of modulo ("clock") arithmetic:

/* Return mins modulo (24 * 60) (number of minutes in 24 hours) */
static int ModMins(int mins)
{
    mins %= (24 * 60);
    if (mins < 0)
        mins  = (24 * 60);
    return mins;
}

int CanTransfer(int xin,int xout,int yin, int yout)
{
    return ModMins(yout - xin) >= 5 && ModMins(yin - xin) <= (3 * 60);
}
  •  Tags:  
  • c
  • Related