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 131 132 133 134 135 136
|
/*G3 Lab#16 4/23/22
In this program */
#include <iostream> //Required for cin, cout, cerr
#include <fstream> //Required for ifstream, ofstream.
#include <string> //Required for string.
#include <cmath> //Required for Pow
using namespace std;
int
main ()
{
int G (6.67 * pow (10, -11)),EscVelEarth(1.12*pow(10,4)); // Declare and initialize objects (variables and i0 objects pointing to files).
string filename;
ifstream planets; // read a file and use for getting data in
ofstream report; // create and write to a file
// first we will create a data file
double MarsMass (0), MarsRadius (0), JupiterMass (0), JupiterRadius (0),
UranusMass (0), UranusRadius (0), PlutoMass (0), PlutoRadius (0),
MarsDiameter (0), JupiterDiameter (0), UranusDiameter (0),
PlutoDiameter (0), GreaterorLessMars, GreaterorLessJupiter,
GreaterorLessUranus, GreaterorLessPluto, EscapeVelocityMars,
EscapeVelocityJupiter, EscapeVelocityUranus, EscapeVelocityPluto,
GreaterorLess, IsGreaterThan, IsLessThan;
cout << "Enter the name of the data file you want"; // Prompt user for name of input file, "birds".
cin >> filename;
report.open (filename.c_str ());
cout <<
" Enter the masses of Mars, Jupiter, Uranus, and Pluto (in kg)." << endl;
cin >> MarsMass >> JupiterMass >> UranusMass >> PlutoMass; // get the values from keypad
report << MarsMass << " " << JupiterMass << " " << UranusMass << " " << PlutoMass << endl; // send values to data file
cout <<
"Now enter the diameter of Mars, Jupiter, Uranus, and Pluto" << endl;
cin >> MarsDiameter >> JupiterDiameter >> UranusDiameter >> PlutoDiameter;
report << MarsDiameter << " " << JupiterDiameter << " " << UranusDiameter
<< " " << PlutoDiameter << endl;
report.close (); // close created data file
cout << "Enter the name of the data file you want to analyse"; // Prompt user for name of input file.
cin >> filename;
planets.open (filename.c_str ()); // Open file and check if it exists.
if (planets.fail ())
{
cerr << "Error opening input file\n";
exit (1);
}
report.open ("MonaesReport.txt"); // open report file.
planets >> MarsMass >> JupiterMass >> UranusMass >> PlutoMass >> MarsDiameter >> JupiterDiameter >> UranusDiameter >> PlutoDiameter; // initial input read the first data point.
while (!planets.eof ()) // While not at the end of the file read and accumulate information
{
MarsRadius = MarsDiameter / 2;
JupiterRadius = JupiterDiameter / 2;
UranusRadius = UranusDiameter / 2;
PlutoRadius = PlutoDiameter / 2;
EscapeVelocityMars =
sqrt (2 * G * MarsMass * MarsRadius) / (MarsRadius);
EscapeVelocityJupiter =
(sqrt (2 * G * JupiterMass * JupiterRadius)) / (JupiterRadius);
EscapeVelocityUranus =
(sqrt (2 * G * UranusMass * UranusRadius)) / (UranusRadius);
EscapeVelocityPluto =
(sqrt (2 * G * PlutoMass * PlutoRadius)) / (PlutoRadius);
if (EscapeVelocityMars > EscVelEarth)
{
GreaterorLessMars = IsGreaterThan;
}
else
(EscapeVelocityMars < EscVelEarth);
{
GreaterorLessMars = IsLessThan;
}
if (EscapeVelocityJupiter > EscVelEarth)
{
GreaterorLessJupiter = IsGreaterThan;
}
else
(EscapeVelocityJupiter < EscVelEarth);
{
GreaterorLessJupiter = IsLessThan;
}
if (EscapeVelocityUranus > EscVelEarth)
{
GreaterorLessUranus = IsGreaterThan;
}
else
(EscapeVelocityUranus < EscVelEarth);
{
GreaterorLessUranus = IsLessThan;
}
if (EscapeVelocityPluto > EscVelEarth)
{
GreaterorLessPluto = IsGreaterThan;
}
else
(EscapeVelocityPluto < EscVelEarth);
{
GreaterorLessPluto = IsLessThan;
}
planets >> MarsMass >> JupiterMass >> UranusMass >> PlutoMass >> MarsDiameter >> JupiterDiameter >> UranusDiameter >> PlutoDiameter; // input next go back to while
} // end while
cout << "Planet Radii" << " " << "PLanet Masses (kg)" << " " <<
"Planet Escape Velocities" << " " <<
"Earth Escape Velocity (1.12*10^4m/s)" << GreaterorLess << endl;
cout <<
"___________________________________________________________________________________________________________________________________"
<< endl;
cout << MarsRadius << " " << MarsMass << " " << EscapeVelocityMars
<< " " << " " << GreaterorLessMars << endl;
cout << JupiterRadius << " " << JupiterMass << " " <<
EscapeVelocityJupiter << " " << " " <<
GreaterorLessJupiter << endl;
cout << UranusRadius << " " << UranusMass << " " <<
EscapeVelocityUranus << " " << " " <<
GreaterorLessUranus << endl;
cout << PlutoRadius << " " << PlutoMass << " " <<
EscapeVelocityPluto << " " << " " <<
GreaterorLessPluto << endl;
report.setf (ios::fixed);
report.setf (ios::showpoint);
report.precision (2);
// Print summary information to report file
// Close file and exit program.
report.close ();
planets.close ();
return 0;
} //end main
| |