Wednesday, January 6, 2010

Train Count

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
      int days,count=0,i,j,A[100],a=0,num=0,d1h,d1m,d2h,d2m,B[100],sumh=0,summ=0;
      char str[10][10];
      printf("All day,hour and minute entries should be separated by ':' or '.'\n");
      printf("Enter the departure time of the train from the source station in hh:mm format: ");
      scanf("%s",str[0]);
      printf("Enter the departure time of the train from the destination station in hh:mm format: ");
      scanf("%s",str[1]);
      printf("Enter the journey duration from source to destination in dd:hh:mm format: ");
      scanf("%s",str[2]);
      //printf("Enter the number of days in a week the train runs from both the stations: ");
      //scanf("%d",&days);
      for(i=0;i<3;i++)
      {
            for(j=0;str[i][j]!='';j++)
            {
                  if(str[i][j]==':' || str[i][j]=='.')
                  {continue;}
                  else
                  {
                         A[a]=str[i][j]-48;
                         a++;
                         num++;
                  }
            }
      }
      i=0;j=0;
      while(i<num)
      {
             B[j]=10*A[i]+A[i+1];
             i+=2;
             j++;
      }
      B[4]=B[4]*24+B[5];
      //B[4]=2*B[4];
      B[5]=B[6];
      j--;
      d1h=B[2]-B[0];
      d1m=B[3]-B[1];
      d2h=B[0]-B[2];
      d2m=B[1]-B[3];
      //printf("\n");
      /*for(i=0;i<j;i++)
      {printf("%d ",B[i]);}*/
      //printf("\n%d %d %d %d ",d1h,d1m,d2h,d2m);
      if(d1h<0)
      {d1h=24+d1h;d1m=d1m;}
      if(d1m<0)
      {d1h=d1h-1;d1m=d1m+60;}
      if(d2h<0)
      {d2h=24+d2h;d2m=d2m;}
      if(d2m<0)
      {d2h=d2h-1;d2m=d2m+60;}
      //printf("\n%d %d %d %d ",d1h,d1m,d2h,d2m);
      while(sumh<B[4] && summ<B[5])
      {
            sumh+=d1h;summ+=d1m;
            while(summ>=60)
            {
                    summ=summ-60;
                    sumh+=1;
            }
            count++;
            if(sumh==B[4] && summ>=B[5])
            {break;}
            if(sumh>B[4])
            {break;}
            sumh+=d2h;summ+=d2m;
            while(summ>=60)
            {
                   summ=summ-60;
                   sumh+=1;
            }
            count++;
            if(sumh==B[4] && summ>=B[5])
            {break;}
            if(sumh>B[4])
            {break;}
      }
      printf("\nAssuming daily operation of the train minimum number of trains required is %d",count);
      //scanf("%d",&i);                                                                                                                   
      return 0;
}

All day hour and minute entries should be separated by ':' or '.'
Enter the departure time of the train from the source station in hh:mm format: 17:35
Enter the departure time of the train from the destination station in hh:mm format: 06:25
Enter the journey duration from source to destination in dd:hh:mm format: 01:02:15
Assuming daily operation of the train minimum number of trains required is 3

All day hour and minute entries should be separated by ':' or '.'
Enter the departure time of the train from the source station in hh:mm format: 06:25
Enter the departure time of the train from the destination station in hh:mm format: 17:35
Enter the journey duration from source to destination in dd:hh:mm format: 01:02:15
Assuming daily operation of the train minimum number of trains required is 3

No comments:

Post a Comment