Code: Select all
#include<stdio.h>
#include<stdlib.h>
int which_time_group( int a );
float func(char ch,int *u1,int *u2,int *u3,float R[][3],int lt[],int ut[],int a,int b,int c,int d);
main()
{int a,b,c,d,ph1,ph2,lt[3],ut[3];
int u1,u2,u3,i=0;
float amnt;
char ch;
float R[5][3];
R[1][1] = 0.10;R[1][2] = 0.06;R[1][3] = 0.02;
R[2][1] = 0.25;R[2][2] = 0.15;R[2][3] = 0.05;
R[3][1] = 0.53;R[3][2] = 0.33;R[3][3] = 0.13;
R[4][1] = 0.87;R[4][2] = 0.47;R[4][3] = 0.17;
R[5][1] = 1.44;R[5][2] = 0.80;R[5][3] = 0.30;
lt[1] = 8*60;ut[1] = 18*60;
lt[2] = ut[1];ut[2] = 22*60;
lt[3] = ut[2];ut[3] = lt[1];
do{
do{
scanf("%c",&ch);
}while( !(ch >= 41 && ch <= 69) && !(ch == '#') );
if(ch == '#')
{ printf("\n");
exit(1);
}
scanf("%d%d",&ph1,&ph2);
ph2 *= -1;
scanf("%d%d%d%d",&a,&b,&c,&d);
u1=0;u2=0;u3=0;/* This keeps the time in different time category */
amnt = func(ch,&u1,&u2,&u3,R,lt,ut,a,b,c,d);
printf("\n%5d-",ph1);
if(ph2/1000 > 0)
printf("%4d",ph2);
else{
if(ph2/100 > 0)
printf("0%3d",ph2);
else if(ph2/10 > 0)
printf("00%2d",ph2);
else if(ph2 > 0)
printf("000%d",ph2);
else
printf("0000");
}
printf("%6d%6d%6d%3d%8.2f",u1,u2,u3,ch,amnt);
}while(ch != '#');
}
int which_time_group( int a )
{
if( a>= 8 && a < 18)
return(1);
if(a >= 18 && a< 22)
return(2);
else
return(3);
}
float func(char ch,int *u1,int *u2,int *u3,float R[][3],int lt[],int ut[],int a,int b,int c,int d)
{int g1,g2,z1,z2,t,t1,t2,t3;
int dist_group;
float amnt;
g1 = which_time_group(a);
g2 = which_time_group(c);
*u1=0;
*u2=0;
*u3=0;
/* printf("\n Inside func() \n"); */
if(ch == 'A')
dist_group = 1;
if(ch == 'B')
dist_group = 2;
if(ch == 'C')
dist_group = 3;
if(ch == 'D')
dist_group = 4;
if(ch == 'E')
dist_group = 5;
z1 = 100*a + b;
z2 = 100*c + d;
if(z1 < z2)/* Midnight doesn't come inbetween */
{ /* printf("\nInside func() Midnight doesn't come in between \n ");
printf("\n g1 = %d g2 = %d",g1,g2); */
if(g1 == g2)
{ /* printf("\n g1 = %d g2 = %d",g1,g2); */
if(z2 >= 2200 && z1 <= 800)
{ t3 = 8*60 - (60*a + b)+ 60*c + d - 22*60;
t1 = 10*60;
t2 = 4*60;
*u1 = t1;
*u2 = t2;
*u3 = t3;
return( t3*R[dist_group][3] + t1*R[dist_group][1] + t2*R[dist_group][2]);
}
t = 60*c + d -(60*a + b);
if(g1 == 1)
*u1 = t;
if(g1 == 2)
*u2 = t;
if(g1 == 3)
*u3 = t;
return( t*R[dist_group][g1] );
}
if(g2 - g1 == 1)
{/* printf("\n g1 = %d g2 = %d",g1,g2); */
t1 = ut[g1] - (60*a + b);
t2 = 60*c + d - lt[g2];
if(g1 == 1)
{ *u1 = t1;
*u2 = t2;
}
if( g1 == 2)
{ *u2 = t1;
*u3 = t2;
}
return( t1*R[dist_group][g1] + t2*R[dist_group][g2] );
}
if(g1 == 3 && g2 == 1)
{
t3 = 8*60 - (60*a + b);
t1 = 60*c + d - 8*60;
*u3 = t3;
*u1 = t1;
return( t3*R[dist_group][g1] + t1*R[dist_group][g2] );
}
if(g2 - g1 == 2)
{ /* printf("\n g1 = %d g2 = %d",g1,g2);*/
t1 = ut[g1] - (60*a + b);
t3 = 60*c + d - lt[g2];
*u1 = t1;
*u2 = 4*60;
*u3 = t3;
return( t1*R[dist_group][g1] + 4*60*R[dist_group][g1 +1] + t3*R[dist_group][g2] );
}
if(g1 == 3 && g2 == 2)
{
t3 = 8*60 - (60*a + b);
t1 = 10*60;
t2 = 60*c + d - 18*60;
*u1 = t1;
*u2 = t2;
*u3 = t3;
return( t3*R[dist_group][3] + t1*R[dist_group][1] + t2*R[dist_group][2]);
}
}
else/* Midnight does come in between */
{/* printf("\nInside func() Midnight does come in between \n "); */
amnt = 0;
if(g1 == 3)
{
if(g2 == 3)
{/* printf("\n g1 = %d g2 = %d",g1,g2); */
if(z1>= 2200)/* if time starts before midnight */
{ if(z2 >= 2200)
{*u3 = 24*60 - 60*a-b + 8*60 + 60*c + d - 22*60;
*u1 = 10*60;
*u2 = 4*60;
}
else{
*u3 = 24*60 - 60*a-b + 60*c + d;
}
}
else {/* if time starts after midnight */
*u3 = 8*60 - 60*a-b + 60*c + d + 2*60;
*u1 = 10*60;
*u2 = 4*60;
}
amnt = R[dist_group][3]*(*u3) + R[dist_group][1]*(*u1) +
R[dist_group][2]*(*u2);
}/* END of g2 == 3 */
else if(g2 == 1)
{/* printf("\n g1 = %d g2 = %d",g1,g2); */
*u3 = 24*60 - 60*a-b + 8*60;
*u1 = 60*c + d - 60*8;
amnt = R[dist_group][g1]*(*u3) + R[dist_group][g2]*(*u1);
}
else /* g2 == 2 */
{/* printf("\n g1 = %d g2 = %d",g1,g2); */
*u3 = 24*60 - 60*a-b + 8*60;
*u1 = 10*60;
*u2 = 60*c + d - 18*60;
amnt = R[dist_group][3]*(*u3) + R[dist_group][1]*(*u1) +
R[dist_group][2]*(*u2);
}
return(amnt);
}
if(g1 == 2)
{
if(g2 == 3)
{/* printf("\n g1 = %d g2 = %d",g1,g2); */
*u2 = 22*60 - 60*a-b;
*u3 = 2*60 + 60*c + d;
amnt = R[dist_group][2]*(*u2) + R[dist_group][3]*(*u3) ;
}
else if(g2 == 1)
{/* printf("\n g1 = %d g2 = %d",g1,g2); */
*u2 = 22*60 - 60*a-b;
*u3 = 10*60;
*u1 = 60*c + d - 8*60;
amnt = R[dist_group][3]*(*u3) + R[dist_group][1]*(*u1) +
R[dist_group][2]*(*u2);
}
else
{/* printf("\n g1 = %d g2 = %d",g1,g2); */
*u3 = 10*60;
*u1 = 10*60;
*u2 = 60*c + d -18*60 + 22*60 - 60*a-b;
amnt = R[dist_group][3]*(*u3) + R[dist_group][1]*(*u1) +
R[dist_group][2]*(*u2);
}
return(amnt);
}
if(g1 == 1)
{
if(g2 == 3)
{ /* printf("\n g1 = %d g2 = %d",g1,g2); */
*u1 = 18*60 - 60*a-b;
*u2 = 4*60;
*u3 = 2*60 + 60*c + d;
amnt = R[dist_group][3]*(*u3) + R[dist_group][1]*(*u1) +
R[dist_group][2]*(*u2);
}
else
{ /* printf("\n g1 = %d g2 = %d",g1,g2); */
*u2 = 4*60;
*u3 = 10*60;
*u1 = 18*60 - 60*a - b + 60*c+d - 8*60;
amnt = R[dist_group][3]*(*u3) + R[dist_group][1]*(*u1) +
R[dist_group][2]*(*u2);
}
return(amnt);
}
}
}