Last commit probably
This commit is contained in:
parent
4247e4fecb
commit
2470b9b1b8
2 changed files with 162 additions and 130 deletions
24
Makefile
Normal file
24
Makefile
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
TARGET = L1A
|
||||||
|
SRC_FILES = main.cpp
|
||||||
|
|
||||||
|
# NO EDITS BELOW THIS LINE
|
||||||
|
CXX = g++
|
||||||
|
OBJECTS = $(SRC_FILES:.cpp=.o)
|
||||||
|
|
||||||
|
ifeq ($(shell echo "Windows"), "Windows")
|
||||||
|
TARGET := $(TARGET).exe
|
||||||
|
DEL = del
|
||||||
|
else
|
||||||
|
DEL = rm -f
|
||||||
|
endif
|
||||||
|
|
||||||
|
all: $(TARGET)
|
||||||
|
|
||||||
|
$(TARGET): $(OBJECTS)
|
||||||
|
$(CXX) -std=c++17 -o $@ $^
|
||||||
|
|
||||||
|
%.o: %.cpp
|
||||||
|
$(CXX) -std=c++17 -o $@ -c $<
|
||||||
|
|
||||||
|
clean:
|
||||||
|
$(DEL) $(TARGET) $(OBJECTS)
|
68
main.cpp
68
main.cpp
|
@ -1,8 +1,13 @@
|
||||||
/* CSCI 200: Lab XX (_INSERT_LAB_HERE_): XXXX (_GIVE_BRIEF_DESCRIPTION_HERE_)
|
/* CSCI 200: Lab 1A (Math Equation Solver): Tyler Beckman
|
||||||
*
|
*
|
||||||
* Author: XXXX (_INSERT_YOUR_NAME_HERE_)
|
* Author: Tyler Beckman
|
||||||
*
|
*
|
||||||
* More complete description here...
|
* 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 <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
@ -10,7 +15,7 @@
|
||||||
|
|
||||||
double const MOLAR_GAS_CONSTANT = 8.314'462'618'153'24;
|
double const MOLAR_GAS_CONSTANT = 8.314'462'618'153'24;
|
||||||
double const GRAVITATIONAL_CONSTANT = 0.000'000'000'066'740'8;
|
double const GRAVITATIONAL_CONSTANT = 0.000'000'000'066'740'8;
|
||||||
double const SPHERE_VOLUME = 4.0 / 3;
|
double const SPHERE_VOLUME_RATIO = 4.0 / 3;
|
||||||
double const VACUUM_PERMITTIVITY = 0.000'000'000'008'854'187'818'8;
|
double const VACUUM_PERMITTIVITY = 0.000'000'000'008'854'187'818'8;
|
||||||
|
|
||||||
double ideal_gas_law(double moles, double gas_absolute_temperature,
|
double ideal_gas_law(double moles, double gas_absolute_temperature,
|
||||||
|
@ -38,7 +43,7 @@ double pythagorean_theorem(double x, double y) {
|
||||||
}
|
}
|
||||||
|
|
||||||
double sphere_volume(double radius) {
|
double sphere_volume(double radius) {
|
||||||
return SPHERE_VOLUME * M_PI * std::pow(radius, 3);
|
return SPHERE_VOLUME_RATIO * M_PI * std::pow(radius, 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
double deflection(double weight, double length, double elasticity_modulus,
|
double deflection(double weight, double length, double elasticity_modulus,
|
||||||
|
@ -56,8 +61,7 @@ double stress(double force, double area) { return force / area; }
|
||||||
|
|
||||||
double shear_stress(double sigma_x, double sigma_y, double tau_xy,
|
double shear_stress(double sigma_x, double sigma_y, double tau_xy,
|
||||||
double theta) {
|
double theta) {
|
||||||
return -0.5 * (sigma_x - sigma_y) * sin(2 * theta) +
|
return -0.5 * (sigma_x - sigma_y) * sin(2 * theta) + tau_xy * cos(2 * theta);
|
||||||
tau_xy * cos(2 * theta);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
double coulombs_law(double charge_1, double charge_2,
|
double coulombs_law(double charge_1, double charge_2,
|
||||||
|
@ -74,9 +78,13 @@ double input_double(std::string prompt) {
|
||||||
std::cin >> out;
|
std::cin >> out;
|
||||||
|
|
||||||
if (std::cin.fail()) {
|
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.clear();
|
||||||
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
|
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
|
||||||
std::cout << "Invalid number, Please try again" << std::endl;
|
std::cout << "Invalid number, please make sure your number is formatted "
|
||||||
|
"correctly."
|
||||||
|
<< std::endl;
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -99,84 +107,84 @@ int main() {
|
||||||
<< "[9] Stress" << std::endl
|
<< "[9] Stress" << std::endl
|
||||||
<< "[10] Shear Stress" << std::endl
|
<< "[10] Shear Stress" << std::endl
|
||||||
<< "[11] Coulomb's law" << std::endl;
|
<< "[11] Coulomb's law" << std::endl;
|
||||||
int equation =
|
int equation_choice =
|
||||||
input_double("Which equation would you like to calculate?");
|
input_double("Which equation would you like to calculate?");
|
||||||
|
|
||||||
double output;
|
double calculated_value;
|
||||||
|
|
||||||
switch (equation) {
|
switch (equation_choice) {
|
||||||
case 0:
|
case 0:
|
||||||
return 0;
|
return 0;
|
||||||
case 1:
|
case 1:
|
||||||
output = ideal_gas_law(
|
calculated_value = ideal_gas_law(
|
||||||
input_double("Please enter amount of moles"),
|
input_double("Please enter amount of moles"),
|
||||||
input_double("Please enter the gas absolute temperate"),
|
input_double("Please enter the gas absolute temperate"),
|
||||||
input_double("Please enter the volume"));
|
input_double("Please enter the volume"));
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
output = average_acceleration(
|
calculated_value = average_acceleration(
|
||||||
input_double("Please enter the starting position"),
|
input_double("Please enter the starting position"),
|
||||||
input_double("Please enter the ending position"),
|
input_double("Please enter the ending position"),
|
||||||
input_double("Please enter the starting time"),
|
input_double("Please enter the starting time"),
|
||||||
input_double("Please enter the ending time"));
|
input_double("Please enter the ending time"));
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
output = ohms_law(input_double("Please input the voltage"),
|
calculated_value =
|
||||||
|
ohms_law(input_double("Please input the voltage"),
|
||||||
input_double("Please input the resistance"));
|
input_double("Please input the resistance"));
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 4:
|
||||||
output = universal_gravitation(
|
calculated_value = universal_gravitation(
|
||||||
input_double("Please input the mass of object 1"),
|
input_double("Please input the mass of object 1"),
|
||||||
input_double("Please input the mass of object 2"),
|
input_double("Please input the mass of object 2"),
|
||||||
input_double("Please input the distance between objects"));
|
input_double("Please input the distance between objects"));
|
||||||
break;
|
break;
|
||||||
case 5:
|
case 5:
|
||||||
output = pythagorean_theorem(
|
calculated_value =
|
||||||
input_double("Please input the x distance"),
|
pythagorean_theorem(input_double("Please input the x distance"),
|
||||||
input_double("Please input the y distance"));
|
input_double("Please input the y distance"));
|
||||||
break;
|
break;
|
||||||
case 6:
|
case 6:
|
||||||
output = sphere_volume(
|
calculated_value =
|
||||||
input_double("Please input the sphere radius"));
|
sphere_volume(input_double("Please input the sphere radius"));
|
||||||
break;
|
break;
|
||||||
case 7:
|
case 7:
|
||||||
output = deflection(
|
calculated_value =
|
||||||
input_double("Please input the force of weight"),
|
deflection(input_double("Please input the force of weight"),
|
||||||
input_double("Please input the length"),
|
input_double("Please input the length"),
|
||||||
input_double("Please input the elasticity modulus"),
|
input_double("Please input the elasticity modulus"),
|
||||||
input_double("Please input the moment of inertia"));
|
input_double("Please input the moment of inertia"));
|
||||||
break;
|
break;
|
||||||
case 8:
|
case 8:
|
||||||
output = heat_transfer_rate(
|
calculated_value = heat_transfer_rate(
|
||||||
input_double("Please input the transfer coefficient"),
|
input_double("Please input the transfer coefficient"),
|
||||||
input_double("Please input the surface area"),
|
input_double("Please input the surface area"),
|
||||||
input_double("Please input the change in temperature"));
|
input_double("Please input the change in temperature"));
|
||||||
break;
|
break;
|
||||||
case 9:
|
case 9:
|
||||||
output =
|
calculated_value =
|
||||||
stress(input_double("Please input the amount of force"),
|
stress(input_double("Please input the amount of force"),
|
||||||
input_double("Please input the surface area"));
|
input_double("Please input the surface area"));
|
||||||
break;
|
break;
|
||||||
case 10:
|
case 10:
|
||||||
output = shear_stress(input_double("Please enter σ_x"),
|
calculated_value = shear_stress(
|
||||||
input_double("Please enter σ_y"),
|
input_double("Please enter σ_x"), input_double("Please enter σ_y"),
|
||||||
input_double("Please enter τ_xy"),
|
input_double("Please enter τ_xy"),
|
||||||
input_double("Please enter θ (in radians)"));
|
input_double("Please enter θ (in radians)"));
|
||||||
break;
|
break;
|
||||||
case 11:
|
case 11:
|
||||||
output = coulombs_law(
|
calculated_value = coulombs_law(
|
||||||
input_double("Please input the first charge"),
|
input_double("Please input the first charge"),
|
||||||
input_double("Please input the second charge"),
|
input_double("Please input the second charge"),
|
||||||
input_double(
|
input_double("Please input the relative static permittivity"),
|
||||||
"Please input the relative static permittivity"),
|
|
||||||
input_double("Please input the distance between charges"));
|
input_double("Please input the distance between charges"));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
std::cout << "That is not a valid choice. Please try again."
|
std::cout << "That is not a valid equation choice. Please try again."
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
std::cout << "The result of that calculation is: " << output
|
std::cout << "The result of that calculation is: " << calculated_value
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue