pclovechristian

Cannon

Feb 18th, 2012
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <string.h>
  4. #include <limits.h>
  5. #include <iomanip>
  6.  
  7. using namespace std;
  8.  
  9. const double gravity = 9.8;
  10.  
  11. int main()
  12. {
  13.     //Declaration
  14.     {
  15.     double angleDeg = 0.0, v = 0.0, angleRad = 0.0, R = 0.0, x = 0.0, y = 0.0, T = 0.0, t = 0.0, i;// v = velocity
  16.     bool done = false;
  17.  
  18.     //input
  19.     cout <<"What's the angle?" <<endl;
  20.     cin >> angleDeg;
  21.  
  22.         while (angleDeg < 0 || angleDeg > 90){  // between 0 to 90
  23.             cout << "Between 0~90 angle please :)" << endl;
  24.  
  25.         while(!(cin >> angleDeg)) // Loops that goes on until cin fails
  26.         {
  27.  
  28.             cin.clear(); // if fails, clear the failed flag.
  29.             while(cin.get() != '\n'){} // A loop the cleans out anything that is left over in the input stream.
  30.  
  31.             cout << "Only numbers please: " << endl << "What's the angle again?" <<endl;// Output failed prompt. And requires only numbers
  32.         }
  33.  
  34.  
  35.  
  36.                                             }
  37.  
  38.     cout << "Velocity?" <<endl;
  39.     cin >> v;
  40.         while (v < 0){  // between 0 to 90
  41.             cout << "You know you can't go negative.. :)" << endl;
  42.  
  43.         while(!(cin >> v)) // Loops that goes on until cin fails
  44.         {
  45.  
  46.             cin.clear(); // Clears cin if fails
  47.             while(cin.get() != '\n'){} // A loop the cleans out anything that is left over in the input.
  48.  
  49.             cout << "Only numbers please: " << endl << "What's the velocity again?" <<endl;// Output failed prompt. And requires only numbers
  50.  
  51.         }
  52.  
  53.  
  54.  
  55.                      }
  56.     //processing
  57.     angleRad = angleDeg*M_PI/180.0;
  58.     R = (pow(v, 2.0)/gravity)*sin(2.0*angleRad); // = (v^2/g)sin(2x) Trajectory formula (googled the right formula..)
  59.     T = (2.0*v*sin(angleRad))/gravity; // Total time
  60.     x = (v*cos(angleRad)); // x coordinate
  61.     y = (v*sin(angleRad)); // y coordinate
  62.  
  63.         {
  64.  
  65.     //output
  66.     cout << "Your total distance: " << R << "meters" << endl << endl;
  67.     cout << "Your travel time is: " << T << "s" << endl << endl;
  68.     cout << "Max X coordinate: " << x << "meters" << endl << endl;
  69.     cout << "Max Y coordinate: " << y << "meters" << endl << endl;
  70.  
  71.             }
  72.     cout << "Distance" << endl;
  73.     cout << "----------" << endl;
  74.  
  75.     double a = R/20;
  76.     double b = x/20;                    //Declaring doubles
  77.     double c = y/20;
  78.  
  79.     for (double i = R/20; i <= R; i = i + a)// Diving total distance by 20 and finding point
  80.     {                                       // Which start with distance1/20 ~ distance20/20
  81.         cout << i << endl;
  82.     }
  83.  
  84.          cout << endl << "X-Coordinates";
  85.          cout << endl << "--------------" << endl;
  86.  
  87.  
  88.        for (double o = x/20; o <= x/2; o = o + b) // Incrementing the X Vector.
  89.         {
  90.             cout << o << endl;
  91.         }
  92.             for  (double o = x/2; o > x/21; o = o - b) // Decrementing the X Vector.
  93.             {
  94.                cout << o << endl;
  95.             }
  96.  
  97.  
  98.          cout << endl << "Y-Coordinates";
  99.          cout << endl << "--------------" << endl;
  100.  
  101.                 for (double p = y/20; p <= y/2; p = p + c) // Incrementing the Y Vector.
  102.                 {
  103.                     cout << p << endl;
  104.                 }
  105.                         for (double p = y/2; p > y/21; p = p - c)// Decrementing the Y vector.
  106.                         {
  107.                             cout << p << endl;
  108.                         }
  109.  
  110.     cout<< endl << "Thank you for using the program!" << endl;
  111.  
  112.    return 0;
  113.             }
  114.         }
Advertisement
Add Comment
Please, Sign In to add comment