/* Written by Andre Jones under Creative Commons license Lean version, all in same file Revision by Walley on 13/6/05 --Added program loopability **Edited by Andre Jones on 14-15/6/05 -- --Added new formulae --Added variable name explanations **Edited by Andre Jones on 16/6/05 -- --Added new forumlae --"Prettycized" menu --Added documentation of variables --Fixed some print errors --Added speed of sound --cleaned menu **Edited 18/6/05 --Replaced macro's with single class and static functions **Edited 19/6/05 --Replaced classes with namespace, put headers into main file **Edited 19/6/05 --Added reverse functionality to physics functions */ #include #include using namespace std; int y = 0; string answer; string answer1; namespace physics { int work(int f, int x){ return f * x; } int av_speed(int d, int t) { int av_s = (d/t); cout << av_s << endl; } int av_velocity(int xf, int xi, int t) { int base_x = xf - xi; int av_v = base_x/t; cout << av_v << endl; } int av_acceleration(int vf, int vi, int t) { int base_v = vf - vi; int av_a = base_v/t; cout << av_a << endl; } int weightf(int m, int g){ return m * g; } int gpe(double m, double h){ cout << "Any undefined or missing?" << endl; cout << "(1 for yes):"; cin >> y; cout << "\n"; if (y == 1) { cout << "You must have the energy then?" << endl; cout << "GPE: " << endl; int gpe1 = 0; cin >> gpe1; float gpe2 = gpe1/9.8; //For height cout << "Have you got the mass? (Yes or no)" << endl; cin >> answer1; if (answer1 == "yes") { cout << "What is the mass: " << endl; cin >> m; float gpe3 = gpe2/m; cout << "The height will be: " << gpe3 << " Meters"; } //For Mass if (answer1 == "no") { cout << "What is the height: " << endl; cin >> h; float gpe4 = gpe2/h; cout << "The mass will be: " << gpe4 << " KG"; } } else { float gpe0 = m*9.8*h; return gpe0; } } int ke(int m, int v){ v = (v)*v; m = (m/2); return m * v; } int sound_speed(int dto, int et){ return (dto*2) * et; } } using namespace physics; //Forces int g,f; //Masses int m; //Measurable scalars int t,d,h,et,dto; //Displacement quantities int xf, xi,x; //Velocities int vf,vi,v; #define WEIGHT_FORCEP "( m*g )" #define WORKP "( f*x )" #define AVER_VELOCITYP "( (xf - xi) / t )" #define AVER_SPEEDP "( d / t )" #define AVER_ACCELERATIONP "(vf -vi) / t " #define GRAVITATIONAL_PEP "(m*g*h)" #define KINETIC_ENERGYP "(0.5*m*v^2)" #define SPEED_SOUNDP "((dto*2)*et)" #define VARS1 "m = mass, g = acceleration due to gravity, f = force \n" #define VARS2 "x = displacement, t = time \n" #define VARS3 "xf = final displacement, xi = initial displacement \n" #define VARS4 "d = distance travelled, v = velocity, vf = final velocity \n" #define VARS5 "vi = initial velocity, dto = distance to object \n" #define VARS6 "et = echo time \n" // MAIN int main() { //Class member int input = 0; bool repeatInput = true; while (repeatInput) { cout << "*********************************" << endl; cout << " Menu " << endl; cout << "*********************************" << endl; cout << "0: Variable meanings" << endl; cout << "1: Work(J)" << endl; cout << "2: Weight force(N) (On earth)" << endl; cout << "3: Average velocity(ms-1)" << endl; cout << "4: Average Speed(ms-1)" << endl; cout << "5: Average Acceleration(ms-2)" << endl; cout << "6: Gravitational Potential Energy(J)" << endl; cout << "7: Kinetic Energy(J)" << endl; cout << "8: Speed of sound(ms-1)" << endl; cout << "Note: Type 1 under variable y if you have an undefined " << endl; cout << "Or missing variable in your formula to be solved" << endl; cout << "**************************" << endl; cin >> input; if ( input > 10 ) { cout << "insuficient data, please choose again" << endl; cin >> input; } else { switch (input) { case 0: cout << VARS1 << VARS2 << VARS3 << VARS4 << VARS5 << endl; cout << VARS6 << endl; break; case 1: cout << "The formula for work done is " << WORKP << endl; cout << "What are your variables?"<< endl; cout << "f: "; cin >> f; cout << "x: "; cin >> x; cout << "The work done will be: " << physics::work(f,x); cout << " Joules."; cout << endl; break; case 2: cout << "The formula for weight force is " << WEIGHT_FORCEP<< endl; cout << "What are your variables?"<< endl; cout << "m: "; cin >> m; cout << "g: "; cin >> g; cout << "The weight will be " << physics::weightf(m,g); cout << " Newtons."; break; case 3: cout << "The formula for average velocity is"; cout << AVER_VELOCITYP << endl; cout << "What are your variables?"<< endl; cout << "Initial displacement: "; cin >> xi; cout << "Final displacement: "; cin >> xf; cout << "time taken: "; cin >> t; cout << "The velocity will be " << physics::av_velocity(xf,xi,t); cout << "ms-1 (Meters per second)" << endl; break; case 4: cout << "The formula for average speed is " << AVER_SPEEDP << endl; cout << "What are your variables?"<< endl; cout << "distance travelled: "; cin >> d; cout << "time taken: "; cin >> t; cout << "The average speed will be " << physics::av_speed(d,t); cout << "ms-1 (Meters per second)" << endl; break; case 5: cout << "The formula for average acceleration is "; cout << AVER_ACCELERATIONP << endl; cout << "What are your variables?"<< endl; cout << "Final velocity: "; cin >> vf; cout << "Initial Velocity: "; cin >> vi; cout << "time taken: "; cin >> t; cout << "The average acceleration will be "; cout << physics::av_acceleration(vf,vi,t); cout << "ms-2 (meters per second per second)" << endl; break; case 6: cout << "The formula for gravitational potential energy is "; cout << GRAVITATIONAL_PEP << endl; cout << "What are your variables?"<< endl; cout << "Mass(kg): "; cin >> m; cout << "Height(m): "; cin >> h; cout << physics::gpe(m,h); cout << "J Is the GPE value "; break; case 7: cout << "The formula for kinetic energy is "; cout << KINETIC_ENERGYP << endl; cout << "What are your variables?"<< endl; cout << "Mass(kg)"; cin >> m; cout << "Velocity: "; cin >> v; cout << "The kinetic energy will be "; cout << physics::ke(m,v); cout << " Joules" << endl; break; case 8: cout << "The formula for calculating th speed of sound is"; cout << SPEED_SOUNDP << endl; cout << "What are your variables?" << endl; cout << "Distance to object: "; cin >> dto; cout << "Echo time: "; cin >> et; cout << physics::sound_speed(dto,et); cout << "ms-1 (meters per second)" << endl; break; default: cout << "More forumale?" << endl; cout << "Please email andre@the-endless-abode.dhs.org" << endl; cout << "To suggest or add more" << endl; break; } } cout << "\n\n"; cout << "Is that all you want to do?" << endl; cout << "Yes or No" << endl; cout << "\n"; cin >> answer; if (answer == "dennis") { int be = 0; for (be = 0;be < 1000; be++) cout << "Goatse is fun" << "\n\n\n\n"; } if (answer == "yes") repeatInput = false; } return 0; }