132 lines
No EOL
4.9 KiB
C++
132 lines
No EOL
4.9 KiB
C++
/* CSCI 200: Lab 1A (Math Equation Solver): Tyler Beckman
|
||
*
|
||
* Author: Tyler Beckman
|
||
*
|
||
* A C++ program to interactively solve 10 different mathmatical equations,
|
||
* using the appropriate constants where relevant. I decided to implement all 10
|
||
* rather than just 2 because it sounded interesting and I wanted to challenge
|
||
* myself. It also will keep prompting for the equation to solve until you
|
||
* explicitly respond with a 0 or CTRL+C it, rather than just exit once the
|
||
* equation is calculated.
|
||
*/
|
||
#include "math.h"
|
||
|
||
#include <iostream>
|
||
#include <limits>
|
||
|
||
double input_double(std::string prompt) {
|
||
double out;
|
||
while (true) {
|
||
std::cout << prompt + ": ";
|
||
std::cin >> out;
|
||
|
||
if (std::cin.fail()) {
|
||
// The clear and ignore are necessary because otherwise it seems to keep
|
||
// reusing the first input a person enters
|
||
std::cin.clear();
|
||
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
|
||
std::cout << "Invalid number, please make sure your number is formatted "
|
||
"correctly."
|
||
<< std::endl;
|
||
} else {
|
||
break;
|
||
}
|
||
}
|
||
|
||
return out;
|
||
}
|
||
|
||
int main() {
|
||
while (true) {
|
||
std::cout << "[0] Quit program" << std::endl
|
||
<< "[1] Ideal gas law" << std::endl
|
||
<< "[2] Average acceleration" << std::endl
|
||
<< "[3] Ohm's law" << std::endl
|
||
<< "[4] Universal gravitation" << std::endl
|
||
<< "[5] Pythagorean theorem" << std::endl
|
||
<< "[6] Sphere volume" << std::endl
|
||
<< "[7] Deflection" << std::endl
|
||
<< "[8] Heat transfer rate" << std::endl
|
||
<< "[9] Stress" << std::endl
|
||
<< "[10] Shear Stress" << std::endl
|
||
<< "[11] Coulomb's law" << std::endl;
|
||
int equationChoice =
|
||
input_double("Which equation would you like to calculate?");
|
||
|
||
double calculatedValue;
|
||
|
||
switch (equationChoice) {
|
||
case 0:
|
||
return 0;
|
||
case 1:
|
||
calculatedValue = ideal_gas_law(
|
||
input_double("Please enter amount of moles"),
|
||
input_double("Please enter the gas absolute temperate"),
|
||
input_double("Please enter the volume"));
|
||
break;
|
||
case 2:
|
||
calculatedValue = average_acceleration(
|
||
input_double("Please enter the starting position"),
|
||
input_double("Please enter the ending position"),
|
||
input_double("Please enter the starting time"),
|
||
input_double("Please enter the ending time"));
|
||
break;
|
||
case 3:
|
||
calculatedValue = ohms_law(input_double("Please input the voltage"),
|
||
input_double("Please input the resistance"));
|
||
break;
|
||
case 4:
|
||
calculatedValue = universal_gravitation(
|
||
input_double("Please input the mass of object 1"),
|
||
input_double("Please input the mass of object 2"),
|
||
input_double("Please input the distance between objects"));
|
||
break;
|
||
case 5:
|
||
calculatedValue =
|
||
pythagorean_theorem(input_double("Please input the x distance"),
|
||
input_double("Please input the y distance"));
|
||
break;
|
||
case 6:
|
||
calculatedValue =
|
||
sphere_volume(input_double("Please input the sphere radius"));
|
||
break;
|
||
case 7:
|
||
calculatedValue =
|
||
deflection(input_double("Please input the force of weight"),
|
||
input_double("Please input the length"),
|
||
input_double("Please input the elasticity modulus"),
|
||
input_double("Please input the moment of inertia"));
|
||
break;
|
||
case 8:
|
||
calculatedValue = heat_transfer_rate(
|
||
input_double("Please input the transfer coefficient"),
|
||
input_double("Please input the surface area"),
|
||
input_double("Please input the change in temperature"));
|
||
break;
|
||
case 9:
|
||
calculatedValue =
|
||
stress(input_double("Please input the amount of force"),
|
||
input_double("Please input the surface area"));
|
||
break;
|
||
case 10:
|
||
calculatedValue = shear_stress(
|
||
input_double("Please enter σ_x"), input_double("Please enter σ_y"),
|
||
input_double("Please enter τ_xy"),
|
||
input_double("Please enter θ (in radians)"));
|
||
break;
|
||
case 11:
|
||
calculatedValue = coulombs_law(
|
||
input_double("Please input the first charge"),
|
||
input_double("Please input the second charge"),
|
||
input_double("Please input the relative static permittivity"),
|
||
input_double("Please input the distance between charges"));
|
||
break;
|
||
default:
|
||
std::cout << "That is not a valid equation choice. Please try again."
|
||
<< std::endl;
|
||
continue;
|
||
}
|
||
std::cout << "The result of that calculation is: " << calculatedValue
|
||
<< std::endl;
|
||
}
|
||
} |