L1A/main.cpp

132 lines
4.9 KiB
C++
Raw Normal View History

2024-08-27 09:23:56 -06:00
/* CSCI 200: Lab 1A (Math Equation Solver): Tyler Beckman
2024-08-25 19:11:50 -06:00
*
2024-08-27 09:23:56 -06:00
* Author: Tyler Beckman
2024-08-25 19:11:50 -06:00
*
2024-08-27 09:23:56 -06:00
* 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.
2024-08-25 19:11:50 -06:00
*/
2024-08-28 22:29:28 -06:00
#include "math.h"
2024-08-25 19:11:50 -06:00
2024-08-28 22:29:28 -06:00
#include <iostream>
#include <limits>
2024-08-25 19:11:50 -06:00
double input_double(std::string prompt) {
2024-08-27 09:23:56 -06:00
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;
2024-08-25 19:11:50 -06:00
}
int main() {
2024-08-27 09:23:56 -06:00
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;
2024-08-28 22:29:28 -06:00
int equationChoice =
2024-08-27 09:23:56 -06:00
input_double("Which equation would you like to calculate?");
2024-08-28 22:29:28 -06:00
double calculatedValue;
2024-08-27 09:23:56 -06:00
2024-08-28 22:29:28 -06:00
switch (equationChoice) {
2024-08-27 09:23:56 -06:00
case 0:
return 0;
case 1:
2024-08-28 22:29:28 -06:00
calculatedValue = ideal_gas_law(
2024-08-27 09:23:56 -06:00
input_double("Please enter amount of moles"),
input_double("Please enter the gas absolute temperate"),
input_double("Please enter the volume"));
break;
case 2:
2024-08-28 22:29:28 -06:00
calculatedValue = average_acceleration(
2024-08-27 09:23:56 -06:00
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:
2024-08-28 22:29:28 -06:00
calculatedValue = ohms_law(input_double("Please input the voltage"),
input_double("Please input the resistance"));
2024-08-27 09:23:56 -06:00
break;
case 4:
2024-08-28 22:29:28 -06:00
calculatedValue = universal_gravitation(
2024-08-27 09:23:56 -06:00
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:
2024-08-28 22:29:28 -06:00
calculatedValue =
2024-08-27 09:23:56 -06:00
pythagorean_theorem(input_double("Please input the x distance"),
input_double("Please input the y distance"));
break;
case 6:
2024-08-28 22:29:28 -06:00
calculatedValue =
2024-08-27 09:23:56 -06:00
sphere_volume(input_double("Please input the sphere radius"));
break;
case 7:
2024-08-28 22:29:28 -06:00
calculatedValue =
2024-08-27 09:23:56 -06:00
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:
2024-08-28 22:29:28 -06:00
calculatedValue = heat_transfer_rate(
2024-08-27 09:23:56 -06:00
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:
2024-08-28 22:29:28 -06:00
calculatedValue =
2024-08-27 09:23:56 -06:00
stress(input_double("Please input the amount of force"),
input_double("Please input the surface area"));
break;
case 10:
2024-08-28 22:29:28 -06:00
calculatedValue = shear_stress(
2024-08-27 09:23:56 -06:00
input_double("Please enter σ_x"), input_double("Please enter σ_y"),
input_double("Please enter τ_xy"),
input_double("Please enter θ (in radians)"));
break;
case 11:
2024-08-28 22:29:28 -06:00
calculatedValue = coulombs_law(
2024-08-27 09:23:56 -06:00
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;
}
2024-08-28 22:29:28 -06:00
std::cout << "The result of that calculation is: " << calculatedValue
2024-08-27 09:23:56 -06:00
<< std::endl;
}
2024-08-25 19:11:50 -06:00
}