diff --git a/Makefile b/Makefile index de6c409..a013895 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ TARGET = L1A -SRC_FILES = main.cpp +SRC_FILES = main.cpp math.cpp # NO EDITS BELOW THIS LINE CXX = g++ diff --git a/main.cpp b/main.cpp index f0fff37..b2201fe 100644 --- a/main.cpp +++ b/main.cpp @@ -9,67 +9,10 @@ * explicitly respond with a 0 or CTRL+C it, rather than just exit once the * equation is calculated. */ +#include "math.h" + #include - -#include - -double const MOLAR_GAS_CONSTANT = 8.314'462'618'153'24; -double const GRAVITATIONAL_CONSTANT = 0.000'000'000'066'740'8; -double const SPHERE_VOLUME_RATIO = 4.0 / 3; -double const VACUUM_PERMITTIVITY = 0.000'000'000'008'854'187'818'8; - -double ideal_gas_law(double moles, double gas_absolute_temperature, - double volume) { - return (moles * MOLAR_GAS_CONSTANT * gas_absolute_temperature) / volume; -} - -double average_acceleration(double pos_start, double pos_end, double time_start, - double time_end) { - return (pos_end - pos_start) / std::pow(time_end - time_start, 2); -} - -double ohms_law(double voltage, double resistance) { - return voltage / resistance; -} - -double universal_gravitation(double mass_one, double mass_two, - double distance) { - return GRAVITATIONAL_CONSTANT * - ((mass_one * mass_two) / std::pow(distance, 2)); -} - -double pythagorean_theorem(double x, double y) { - return std::sqrt(std::pow(x, 2) + std::pow(y, 2)); -} - -double sphere_volume(double radius) { - return SPHERE_VOLUME_RATIO * M_PI * std::pow(radius, 3); -} - -double deflection(double weight, double length, double elasticity_modulus, - double moment_of_inertia) { - return (weight * std::pow(length, 3)) / - (3 * elasticity_modulus * moment_of_inertia); -} - -double heat_transfer_rate(double transfer_coefficient, double surface_area, - double temperature_change) { - return transfer_coefficient * surface_area * temperature_change; -} - -double stress(double force, double area) { return force / area; } - -double shear_stress(double sigma_x, double sigma_y, double tau_xy, - double theta) { - return -0.5 * (sigma_x - sigma_y) * sin(2 * theta) + tau_xy * cos(2 * theta); -} - -double coulombs_law(double charge_1, double charge_2, - double relative_static_permittivity, double distance) { - return std::fabs(charge_1 * charge_2) / - (4 * M_PI * VACUUM_PERMITTIVITY * relative_static_permittivity * - std::pow(distance, 2)); -} +#include double input_double(std::string prompt) { double out; @@ -107,73 +50,72 @@ int main() { << "[9] Stress" << std::endl << "[10] Shear Stress" << std::endl << "[11] Coulomb's law" << std::endl; - int equation_choice = + int equationChoice = input_double("Which equation would you like to calculate?"); - double calculated_value; + double calculatedValue; - switch (equation_choice) { + switch (equationChoice) { case 0: return 0; case 1: - calculated_value = ideal_gas_law( + 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: - calculated_value = average_acceleration( + 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: - calculated_value = - ohms_law(input_double("Please input the voltage"), - input_double("Please input the resistance")); + calculatedValue = ohms_law(input_double("Please input the voltage"), + input_double("Please input the resistance")); break; case 4: - calculated_value = universal_gravitation( + 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: - calculated_value = + calculatedValue = pythagorean_theorem(input_double("Please input the x distance"), input_double("Please input the y distance")); break; case 6: - calculated_value = + calculatedValue = sphere_volume(input_double("Please input the sphere radius")); break; case 7: - calculated_value = + 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: - calculated_value = heat_transfer_rate( + 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: - calculated_value = + calculatedValue = stress(input_double("Please input the amount of force"), input_double("Please input the surface area")); break; case 10: - calculated_value = shear_stress( + 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: - calculated_value = coulombs_law( + 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"), @@ -184,7 +126,7 @@ int main() { << std::endl; continue; } - std::cout << "The result of that calculation is: " << calculated_value + std::cout << "The result of that calculation is: " << calculatedValue << std::endl; } } \ No newline at end of file diff --git a/math.cpp b/math.cpp new file mode 100644 index 0000000..9f1b284 --- /dev/null +++ b/math.cpp @@ -0,0 +1,56 @@ +#include + +double const MOLAR_GAS_CONSTANT = 8.314'462'618'153'24; +double const GRAVITATIONAL_CONSTANT = 0.000'000'000'066'740'8; +double const SPHERE_VOLUME_RATIO = 4.0 / 3; +double const VACUUM_PERMITTIVITY = 0.000'000'000'008'854'187'818'8; + +double ideal_gas_law(double moles, double gasAbsoluteTemperature, + double volume) { + return (moles * MOLAR_GAS_CONSTANT * gasAbsoluteTemperature) / volume; +} + +double average_acceleration(double posStart, double posEnd, double timeStart, + double timeEnd) { + return (posEnd - posStart) / std::pow(timeEnd - timeStart, 2); +} + +double ohms_law(double voltage, double resistance) { + return voltage / resistance; +} + +double universal_gravitation(double massOne, double massTwo, double distance) { + return GRAVITATIONAL_CONSTANT * ((massOne * massTwo) / std::pow(distance, 2)); +} + +double pythagorean_theorem(double x, double y) { + return std::sqrt(std::pow(x, 2) + std::pow(y, 2)); +} + +double sphere_volume(double radius) { + return SPHERE_VOLUME_RATIO * M_PI * std::pow(radius, 3); +} + +double deflection(double weight, double length, double elasticityModulus, + double momentOfInertia) { + return (weight * std::pow(length, 3)) / + (3 * elasticityModulus * momentOfInertia); +} + +double heat_transfer_rate(double transferCoefficient, double surfaceArea, + double temperatureChange) { + return transferCoefficient * surfaceArea * temperatureChange; +} + +double stress(double force, double area) { return force / area; } + +double shear_stress(double sigmaX, double sigmaY, double tauXY, double theta) { + return -0.5 * (sigmaX - sigmaY) * sin(2 * theta) + tauXY * cos(2 * theta); +} + +double coulombs_law(double charge1, double charge2, + double relativeStaticPermittivity, double distance) { + return std::fabs(charge1 * charge2) / + (4 * M_PI * VACUUM_PERMITTIVITY * relativeStaticPermittivity * + std::pow(distance, 2)); +} \ No newline at end of file diff --git a/math.h b/math.h new file mode 100644 index 0000000..32130f6 --- /dev/null +++ b/math.h @@ -0,0 +1,21 @@ +double ideal_gas_law(double, double, double); + +double average_acceleration(double, double, double, double); + +double ohms_law(double, double); + +double universal_gravitation(double, double, double); + +double pythagorean_theorem(double, double); + +double sphere_volume(double); + +double deflection(double, double, double, double); + +double heat_transfer_rate(double, double, double); + +double stress(double, double); + +double shear_stress(double, double, double, double); + +double coulombs_law(double, double, double, double); \ No newline at end of file