Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. void ResolveV(float Angle, float Velocity, float *XVelocity, float *YVelocity);
  6. void TimeOfFlight(float YVelocity, float *TOF);
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10.     float Angle, Velocity, XVelocity, YVelocity, TOF, Time[11], XDistance[11], YDistance[11];
  11.     char AngleT[100], VelocityT[100], *next, save, FileName[50];
  12.     int i;
  13.  
  14.     if (argc == 1)
  15.     {
  16.         printf("Please enter the angle in degrees.\n");
  17.         scanf ("%s", &AngleT);
  18.         printf("Please enter the velocity in m/s.\n");
  19.         scanf ("%s", &VelocityT);
  20.         printf("\n");
  21.     }
  22.     else if (argc != 3)
  23.     {
  24.         printf("You have entered %d variables, please enter only an angle in degrees and a velocity.\n", argc - 1);
  25.         return(0);
  26.     }
  27.     else
  28.     {
  29.         strncpy(AngleT, argv[1], 100);
  30.         strncpy(VelocityT, argv[2], 100);
  31.     }
  32.  
  33.     Angle = strtof (AngleT, &next);
  34.  
  35.     if ((next == AngleT) || (*next != '\0') || (Angle <= 0) || (Angle > 90))
  36.     {
  37.         printf("%s is not valid, only positive numbers up to 90 are valid for the angle.\n", AngleT);
  38.         return 0;
  39.     }
  40.     else
  41.     {
  42.         Velocity = strtof (VelocityT, &next);
  43.  
  44.         if ((next == VelocityT) || (*next != '\0') || (Velocity <= 0))
  45.         {
  46.             printf("%s is not valid, only positive numbers are valid for the velocity.\n", VelocityT);
  47.             return 0;
  48.         }
  49.         else
  50.         {
  51.             printf("Angle = %f degrees\n", Angle);
  52.             Angle = Angle * (M_PI / 180);
  53.             printf("Velocity = %f m/s\n", Velocity);
  54.         }
  55.     }
  56.  
  57.     ResolveV(Angle, Velocity, &XVelocity, &YVelocity);
  58.  
  59.     TimeOfFlight(YVelocity, &TOF);
  60.  
  61.     printf("\nTime(s)        |");
  62.     for(i=0; i<11; i++)
  63.     {
  64.         Time[i] = TOF * (float)i / 10;
  65.         XDistance[i] = XVelocity * Time[i];
  66.         YDistance[i] = YVelocity * Time[i] + (-4.905 * Time[i] * Time[i]);
  67.         printf("%7.2f|", Time[i]);
  68.     }
  69.     printf("\nX Distance(m/s)|");
  70.     for(i=0; i<11; i++)
  71.     {
  72.         printf("%7.2f|", XDistance[i]);
  73.     }
  74.     printf("\nY Distance(m/s)|");
  75.     for(i=0; i<11; i++)
  76.     {
  77.         printf("%7.2f|", YDistance[i]);
  78.     }
  79.  
  80.     printf("\n\nWould you like to save this data to a file? \nEnter y if yes, n if no.\n");
  81.  
  82.     do
  83.     {
  84.         save = getch();
  85.  
  86.         if (save == 'n')
  87.         {
  88.             printf("\n\nData will not be saved.");
  89.         }
  90.         else if (save == 'y')
  91.         {
  92.             printf("\n\nEnter the name of the file.\n");
  93.             scanf("%s", FileName);
  94.  
  95.             FILE *fptr;
  96.             if((fptr = fopen(FileName, "w"))== NULL)
  97.             {
  98.                 printf("\nError opening file");
  99.                 return(0);
  100.             }
  101.  
  102.             fprintf(fptr, "Angle: %f degrees\nVelocity: %f m/s\n\nHorizontal velocity: %f m/s\nVertical velocity: %f m/s", Angle * 180 / M_PI, Velocity, XVelocity, YVelocity);
  103.             fprintf(fptr, "\n\nTime(s)        |");
  104.             for(i=0; i<11; i++)
  105.             {
  106.                 Time[i] = TOF * (float)i / 10;
  107.                 XDistance[i] = XVelocity * Time[i];
  108.                 YDistance[i] = YVelocity * Time[i] + (-4.905 * Time[i] * Time[i]);
  109.                 fprintf(fptr, "%7.2f|", Time[i]);
  110.             }
  111.             fprintf(fptr, "\nX Distance(m/s)|");
  112.             for(i=0; i<11; i++)
  113.             {
  114.                 fprintf(fptr, "%7.2f|", XDistance[i]);
  115.             }
  116.             fprintf(fptr, "\nY Distance(m/s)|");
  117.             for(i=0; i<11; i++)
  118.             {
  119.                 fprintf(fptr, "%7.2f|", YDistance[i]);
  120.             }
  121.             fclose(fptr);
  122.  
  123.             printf("\nFile saved.");
  124.         }
  125.         else
  126.         {
  127.             printf("\nInvalid input, press 'y' or 'n'.");
  128.         }
  129.     }
  130.     while ((save !='y')&(save !='n'));
  131.  
  132.     return(0);
  133. }
  134.  
  135. void ResolveV(float Angle, float Velocity, float *XVelocity, float *YVelocity)
  136. {
  137.     *XVelocity = Velocity * (cos(Angle));
  138.     *YVelocity = Velocity * (sin(Angle));
  139.  
  140.     printf("\nThe horizontal velocity is %.2f m/s\n", *XVelocity);
  141.     printf("The vertical velocity is %.2f m/s\n", *YVelocity);
  142. }
  143.  
  144. void TimeOfFlight(float YVelocity, float *TOF)
  145. {
  146.     *TOF = -2 * (YVelocity / -9.81);
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement