#include <stdio.h>
#include <math.h>

/*
 * Solution of homework 1, CS 2073
 * Calculate the location, distance and average of two cars
 *
 */
 
int main()
{

    int at1,at2,at3;
    int as1,as2,as3;
    int bt1,bt2,bt3;
    int bs1,bs2,bs3;
    double Ax=0,Ay=0,Bx=0,By=0;
    double distance;
    double avgA, avgB;
   
	/* option record user's choice */
    int opt;

    /* ask user to choose the option  */
    /* option 1: use the default value to show the demo */
    /* option 2: allow user to input the value for variables */
    /* option 0: exit the program */
    while(1){
    /* information to show */
       printf("Press 1 to show the demo.\n");
       printf("Press 2 to input the value as you want.\n");
       printf("Press 0 to exit.\n");

       /* get the option from the user */
       scanf("%d",&opt);

       if (opt == 1)
       {
          // default demo
          at1 = 5;             // unit : sec 
          at2 = 12;            // unit : min 
          at3 = 1;             // unit : hour
          as1 = 40;            // unit : mph
          as2 = 60;            // unit : mph
          as3 = 50;            // unit : mph

          bt1 = 30;            // unit : sec 
          bt2 = 20;            // unit : min 
          bt3 = 2;             // unit : hour
          bs1 = 25;            // unit : mph
          bs2 = 45;            // unit : mph
          bs3 = 60;            // unit : mph

          break;
       }else if (opt == 2)
       {
          // the user will assign the values for the variables  
          /* ask the user to input the value for variables of car A */
          printf("Please input the value of at1: (sec)\n");
          scanf("%d",&at1);
   
          printf("Please input the value of at2: (min)\n");
          scanf("%d",&at2);

          printf("Please input the value of at3: (hour)\n");
          scanf("%d",&at3);              // unit : hour
                
          printf("Please input the value of as1: (mph)\n");
          scanf("%d",&as1);

          printf("Please input the value of as2: (mph)\n");
          scanf("%d",&as2);

          printf("Please input the value of as3: (mph)\n");   
          scanf("%d",&as3);

          /* ask the user to input the value for variables of car B */
          printf("Please input the value of bt1: (sec)\n");
          scanf("%d",&bt1);

          printf("Please input the value of bt2: (min)\n");
          scanf("%d",&bt2);

          printf("Please input the value of bt3: (hour)\n");
          scanf("%d",&bt3);              // unit : hour
                
          printf("Please input the value of bs1: (mph)\n");
          scanf("%d",&bs1);

          printf("Please input the value of bs2: (mph)\n");
          scanf("%d",&bs2);

          printf("Please input the value of bs3: (mph)\n");   
          scanf("%d",&bs3);          
   
          break;
       }else if (opt == 0)
       {
          // quit the current program
          break;
       }else
       {
          // incorrect option, show warning information
          printf("Invalid input. Please enter 1,2,or 0.\n");
        }//end of if-else pairs

    }//end of while



   /* calculate the location of car A */
   Ax = (double) (at1 * as1 / 3600 + at3 * as3);
   Ay = (double) (at2 * as2 /60);

   /* calculate the location of car B */
   Bx = (double)((-1) * bt1 * bs1 / 3600 + bt2 * bs2 * pow(2,-0.5)/60 + bt3 * bs3);
   By = (double)((-1) * bt2 * bs2 * pow(2,-0.5) / 60);
  
   /* calculate the distance btw car A and car B */
   
   distance = sqrt(pow((Ax-Bx),2) + pow((Ay-By),2));
   
   /* output the location of car A nad car B, as well as their distance */
   printf("A: (%f,%f)\n",Ax,Ay);
   printf("B: (%f,%f)\n",Bx,By);
   printf("distance: %f\n",distance);   

   /* calculate the average speed of car A and car B */
   avgA = (double) (at1 * as1/3600 + at2 * as2 /60 + at3 * as3)/(at1/3600 + at2/60 + at3);
   avgB = (double) (bt1 * bs1/3600 + bt2 * bs2 /60 + bt3 * bs3)/(bt1/3600 + bt2/60 + bt3);
   
   /* output the average speed of car A and car B*/
   printf("Average speed of car A is %f\n",avgA);
   printf("Average speed of car B is %f\n",avgB);

   //system("pause");
   return 1;
}
