patterncppMinor
Calculators for gas law equations
Viewed 0 times
gasequationscalculatorsforlaw
Problem
I've made a C++ program that calculates a missing variable in an equation for one of the five following laws of gas:
The way I made it includes a lot of repetitive if statement checking and different functions with small variations. I feel like there is a cleaner and more efficient way of doing this, but I'm not sure on how to go about making that happen.
Here's the code:
```
#include
#include
using namespace std;
string x;
bool invalid = false;
double boyOperation();
double charOperation();
double lusOperation();
double avogOperation();
double idealOperation();
int selectLaw() {
system("clear");
cout > x;
system("clear");
invalid = false;
if (x == "1") {
cout > variableStr;
system("clear");
if (variableStr == "P1") {
cout > P2;
cout > V1;
cout > V2;
P1 = (P2 * V2)/V1;
return P1;
} else if (variableStr == "P2") {
cout > P1;
cout > V1;
cout > V2;
P2 = (P1 * V1)/V2;
return P2;
} else if (variableStr == "V1") {
cout > P1;
cout > P2;
cout > V2;
V1 = (P2 * V2)/P1;
return V1;
} else if (variableStr == "V2") {
cout > P1;
cout > P2;
cout > V1;
V2 = (P1 * V1)/P2;
return V2;
} else {
cout > variableStr;
system("clear");
if (variableStr == "V1") {
cout > T1;
cout > V2;
cout > T2;
V1 = (V2/T2)*T1;
return V1;
} else if (variableStr == "V2") {
cout > T1;
cout > T2;
cout > V1;
V2 = (V1/T1)*T2;
return V2;
} else if (variableStr == "T1") {
cout > T2;
cout > V1;
cout > V2;
T1 = V1/(V2/T2);
return T1;
} else if (variableStr == "T2") {
cout > T1;
cout > V1;
cout > V2;
T2 = V2/(V1/T1);
return T2
- Boyle's Law
- Charles' Law
- Gay-Lussac's Law
- Avogadro's Principle
- Ideal Gas Law
The way I made it includes a lot of repetitive if statement checking and different functions with small variations. I feel like there is a cleaner and more efficient way of doing this, but I'm not sure on how to go about making that happen.
Here's the code:
```
#include
#include
using namespace std;
string x;
bool invalid = false;
double boyOperation();
double charOperation();
double lusOperation();
double avogOperation();
double idealOperation();
int selectLaw() {
system("clear");
cout > x;
system("clear");
invalid = false;
if (x == "1") {
cout > variableStr;
system("clear");
if (variableStr == "P1") {
cout > P2;
cout > V1;
cout > V2;
P1 = (P2 * V2)/V1;
return P1;
} else if (variableStr == "P2") {
cout > P1;
cout > V1;
cout > V2;
P2 = (P1 * V1)/V2;
return P2;
} else if (variableStr == "V1") {
cout > P1;
cout > P2;
cout > V2;
V1 = (P2 * V2)/P1;
return V1;
} else if (variableStr == "V2") {
cout > P1;
cout > P2;
cout > V1;
V2 = (P1 * V1)/P2;
return V2;
} else {
cout > variableStr;
system("clear");
if (variableStr == "V1") {
cout > T1;
cout > V2;
cout > T2;
V1 = (V2/T2)*T1;
return V1;
} else if (variableStr == "V2") {
cout > T1;
cout > T2;
cout > V1;
V2 = (V1/T1)*T2;
return V2;
} else if (variableStr == "T1") {
cout > T2;
cout > V1;
cout > V2;
T1 = V1/(V2/T2);
return T1;
} else if (variableStr == "T2") {
cout > T1;
cout > V1;
cout > V2;
T2 = V2/(V1/T1);
return T2
Solution
First things first:
-
Do not use namespace std; This is bad style.
-
Create a class to properly encapsulate your code and simplify passing of variables.
-
I would suggest, that you use enumerations to simplify your code. Basically, if you have 4 possible inputs and you only want 3 of them, create a for loop where you split the appropriate input.
-
Obviously, you would need a suitable class structure so that you can set the other variables easily.
-
You should also perform real error checking, aka "what happens when the user enters a letter?"
-
Do not use namespace std; This is bad style.
-
Create a class to properly encapsulate your code and simplify passing of variables.
-
I would suggest, that you use enumerations to simplify your code. Basically, if you have 4 possible inputs and you only want 3 of them, create a for loop where you split the appropriate input.
for (size_t input = 0; input < 4; ++input) {
if (input == YOUR_ENUM_COICE) {
continue;
}
GetInput(input);
}-
Obviously, you would need a suitable class structure so that you can set the other variables easily.
YourClass::GetInput(const size_t input) {
switch(input) {
case (ENUM_ALTERNATIVE1):
std::cout > P;
break;
case (ENUM_ALTERNATIVE2):
...
}
}-
You should also perform real error checking, aka "what happens when the user enters a letter?"
Code Snippets
for (size_t input = 0; input < 4; ++input) {
if (input == YOUR_ENUM_COICE) {
continue;
}
GetInput(input);
}YourClass::GetInput(const size_t input) {
switch(input) {
case (ENUM_ALTERNATIVE1):
std::cout << "What is the value of P?" << std::endl;
std::cin >> P;
break;
case (ENUM_ALTERNATIVE2):
...
}
}Context
StackExchange Code Review Q#156741, answer score: 8
Revisions (0)
No revisions yet.