1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
// This program/converter is designed to find the desired 'real' values using Einstein's theory of relativity
#include<iostream>
#include<math.h>
#include<stdlib.h>
using namespace std;
int main()
{
int choice; // User's choice
float c; //Speed of light
c = 299792458;
cout << " " << endl;
cout << "Welcome to Craig McRae's Relitivity Calculator!" << endl;
cout << "What would you like to calculate?" << endl;
cout << "1) Relativistic Length, Mass and Time dilation" << endl;
cout << "2) Relativistic Velocity addition" << endl;
cout << "3) Mass energy equivilence" << endl;
cin >> choice;
cout << " " << endl;
if ( choice == 1 )
{
float V; //Speed
float length, mass, time; //Initial mass, length, and time
float Rlength, Rmass, Rtime; //Relitivistic Length, mass and time
float y; //Gamma or Lorentz transform
cout << "How fast are you going? (m/s)";
cin >> V;
while ( V >= c )
{cout << "IMPOSSIBLE! NOTHING CAN GO THAT FAST!" << endl;
cout << "How fast are you REALLY going? (m/s)";
cin >> V;
}
cout << "How long were you traveling at this speed? (in seconds) ";
cin >> time;
cout << "Enter your mass in kg: ";
cin >> mass;
cout << "Enter your length in meters: ";
cin >> length;
y = sqrt(1 - ((V*V)/(c*c)));
Rmass = mass / y;
Rtime = time / y;
Rlength = length * y;
cout << " " << endl;
cout << "Traveling at a speed of " << V << "m/s, to an observer at rest you would appear to have a length of " << Rlength << " meters, a mass of " << Rmass << " kg, and " << time << " seconds for you would be " << Rtime << " seconds for the observer!" << endl;
}
if ( choice == 2 )
{
float V1, V2, Vf; //Velocities
char ans; //For y/n
cout << "What is the first velocity (in m/s)? ";
cin >> V1;
while ( V1 >= c )
{cout << "Error: Velocity can not be greater the speed of light" << endl;
cout << "What is the first velocity (in m/s)? ";
cin >> V1;
}
cout << "What is the second Velocity (in m/s)? ";
cin >> V2;
while ( V2 >= c )
{cout << "Error: Velocity can not be greater the speed of light" << endl;
cout << "What is the second Velocity (in m/s)? ";
cin >> V2;
}
cout << "Are they going in the same direction? (Y/N)";
cin >> ans;
if ( ans == 'Y' || ans == 'y')
{V2 = -1 * V2;
}
//Calculations
Vf = abs(V1 + V2);
if ( ans == 'Y' || ans == 'y')
{V2 = -1 * V2;
}
Vf = Vf/(1 + (V1*V2)/(c*c));
cout << " " << endl;
cout << "The combined relativistic speed of " << V1 << " and " << V2 << " is " << Vf << "m/s" << endl;
}//End Choice =2
if (choice == 3)
{
int conv; //Which Converter to use
float mass;
float energy;
float V; //Speed
float P; //Momentum
float y; //Gamma
cout << "Which conversion are you doing?" << endl;
cout << "1) Mass & momentum to Energy, or 2) Energy to mass " << endl;
cin >> conv;
if ( conv == 1 )
{cout << "What is the objects mass in kg? " << endl;
cin >> mass;
cout << "What is the objects speed in m/s? " << endl;
cin >> V;
while ( V >= c )
{cout << "Error: Speed can not surpass the speed of light" << endl;
cout << "What is the objects Velocity?" << endl;
cin >> V;
}
y = sqrt(1 - ((V*V)/(c*c)));
P = (mass * V)/y;
energy = sqrt((mass*c*c)*(mass*c*c) + (P*c)*(P*c));
cout << "The total energy stored in a " << mass << " kg object with a speed of " << V << " m/s is " << energy << " joules!" << endl;
}
if ( conv == 2)
{cout << "Enter the energy you want to convert to mass, in joules: " << endl;
cin >> energy;
mass = energy/(c*c);
cout << energy << " joules of energy is equivalent to " << mass << "kg. " << endl;
}
}//Choice = 3
return 0;
}
|