diff options
-rw-r--r-- | entropy-calculator.cbp | 2 | ||||
-rw-r--r-- | headers/baseElement.h | 67 | ||||
-rw-r--r-- | headers/readElementState.cpp | 26 | ||||
-rw-r--r-- | headers/readElementState.h | 10 | ||||
-rw-r--r-- | main.cpp | 24 |
5 files changed, 101 insertions, 28 deletions
diff --git a/entropy-calculator.cbp b/entropy-calculator.cbp index daf78a5..797dabf 100644 --- a/entropy-calculator.cbp +++ b/entropy-calculator.cbp @@ -35,6 +35,8 @@ <Unit filename="elements/water.h" /> <Unit filename="headers/allElements.cpp" /> <Unit filename="headers/allElements.h" /> + <Unit filename="headers/readElementState.cpp" /> + <Unit filename="headers/readElementState.h" /> <Unit filename="headers/baseElement.h" /> <Unit filename="main.cpp" /> <Extensions> diff --git a/headers/baseElement.h b/headers/baseElement.h index fd77194..e6372a1 100644 --- a/headers/baseElement.h +++ b/headers/baseElement.h @@ -1,9 +1,9 @@ #ifndef BASEELEMENT_H_INCLUDED #define BASEELEMENT_H_INCLUDED - #include <iostream> #include <math.h> +#include "readElementState.h" using namespace std; class baseElement @@ -40,38 +40,61 @@ public: double totalHeatNeeded(double mass, double fromTemp, double toTemp) { - double totalHeat = 0; - if (fromTemp <= meltingPoint && toTemp <= meltingPoint) + if (fromTemp < 0 || toTemp < 0) { - totalHeat = mass * specificHeatSolid * (toTemp - fromTemp); + cout << "Temperature cannot be less than 0 K" << endl; + return 0; } - else if (fromTemp <= meltingPoint && toTemp >= meltingPoint && toTemp <= boilingPoint) + + double totalHeat = 0; + string initialState = ""; + string finalState = ""; + if (fromTemp == meltingPoint || fromTemp == boilingPoint) { - totalHeat = mass * specificHeatSolid * (meltingPoint - fromTemp); - totalHeat += mass * latentHeatOfFusion / meltingPoint; - totalHeat += mass * specificHeatLiquid * (toTemp - meltingPoint); + cout << "What is the state of the element at the initial temperature?" << endl; + initialState = readElementState(); } - else if (fromTemp <= meltingPoint && toTemp >= boilingPoint) + if (toTemp == meltingPoint || toTemp == boilingPoint) { - totalHeat = mass * specificHeatSolid * (meltingPoint - fromTemp); - totalHeat += mass * latentHeatOfFusion; - totalHeat += mass * specificHeatLiquid * (boilingPoint - meltingPoint); - totalHeat += mass * latentHeatOfVaporization; - totalHeat += mass * specificHeatGas * (toTemp - boilingPoint); + cout << "What is the state of the element at the final temperature?" << endl; + finalState = readElementState(); } - else if (fromTemp >= meltingPoint && fromTemp <= boilingPoint && toTemp >= meltingPoint && toTemp <= boilingPoint) + + if (toTemp <= meltingPoint) { - totalHeat = mass * specificHeatLiquid * (toTemp - fromTemp); + totalHeat += mass * specificHeatSolid * (toTemp - fromTemp); + if (toTemp == meltingPoint && initialState != finalState) + totalHeat += mass * latentHeatOfFusion; } - else if (fromTemp >= meltingPoint && fromTemp <= boilingPoint && toTemp >= boilingPoint) + else if (toTemp <= boilingPoint) { - totalHeat = mass * specificHeatLiquid * (boilingPoint - fromTemp); - totalHeat += mass * latentHeatOfVaporization; - totalHeat += mass * specificHeatGas * (toTemp - boilingPoint); + if (fromTemp < meltingPoint) + totalHeat += mass * specificHeatSolid * (meltingPoint - fromTemp); + if (fromTemp == meltingPoint && initialState != finalState) + totalHeat += mass * latentHeatOfFusion; + if (fromTemp <= meltingPoint) + totalHeat += mass * specificHeatLiquid * (toTemp - meltingPoint); + else + totalHeat += mass * specificHeatLiquid * (toTemp - fromTemp); + if (toTemp == boilingPoint && initialState != finalState) + totalHeat += mass * latentHeatOfVaporization; } - else if (fromTemp >= boilingPoint && toTemp >= boilingPoint) + else { - totalHeat = mass * specificHeatGas * (toTemp - fromTemp); + if (fromTemp < meltingPoint) + totalHeat += mass * specificHeatSolid * (meltingPoint - fromTemp); + if (fromTemp == meltingPoint && initialState != finalState) + totalHeat += mass * latentHeatOfFusion; + + if (fromTemp < boilingPoint) + totalHeat += mass * specificHeatLiquid * (fromTemp - meltingPoint); + if (fromTemp == boilingPoint && initialState != finalState) + totalHeat += mass * latentHeatOfVaporization; + + if (fromTemp > boilingPoint) + totalHeat += mass * specificHeatGas * (toTemp - fromTemp); + else + totalHeat += mass * specificHeatGas * (toTemp - boilingPoint); } return totalHeat; } diff --git a/headers/readElementState.cpp b/headers/readElementState.cpp new file mode 100644 index 0000000..494594a --- /dev/null +++ b/headers/readElementState.cpp @@ -0,0 +1,26 @@ +#include <iostream> +using namespace std; + +string readElementState() +{ + string state; + cout << "1. Solid\n2. Liquid\n3. Gas" << endl; + int choice; + cin >> choice; + switch (choice) + { + case 1: + state = "Solid"; + break; + case 2: + state = "Liquid"; + break; + case 3: + state = "Gas"; + break; + default: + cout << "Invalid choice" << endl; + break; + } + return state; +} diff --git a/headers/readElementState.h b/headers/readElementState.h new file mode 100644 index 0000000..5e0a2fa --- /dev/null +++ b/headers/readElementState.h @@ -0,0 +1,10 @@ +// get all elements from elements folder + +#pragma once +#ifndef READ_ELEMENT_STATE +#define READ_ELEMENT_STATE + +#include <iostream> +std::string readElementState(); + +#endif @@ -20,13 +20,25 @@ int main() double mass, fromTemp, toTemp; cout << "Enter the mass of the element (in Kg): "; - cin >> mass; + cin >> mass;
+ if (mass <= 0)
+ {
+ cout << "Mass must be a positive value" << endl;
+ return 0;
+ } cout << "Enter the initial temperature of the element (in K): "; cin >> fromTemp; cout << "Enter the final temperature of the element (in K): "; - cin >> toTemp; - - cout << "The total heat needed is: " << elements[choice - 1]->totalHeatNeeded(mass, fromTemp, toTemp) << " J" << endl; - - cout << "The total entropy change is: " << elements[choice - 1]->totalEntropyChange(mass, fromTemp, toTemp) << " J/K" << endl; + cin >> toTemp;
+ if (fromTemp > toTemp)
+ {
+ cout << "Initial temperature can not be greater than final temperature" << endl;
+ return 0;
+ } +
+ double totalHeat = elements[choice - 1]->totalHeatNeeded(mass, fromTemp, toTemp);
+ double totalEntropy = elements[choice - 1]->totalEntropyChange(mass, fromTemp, toTemp); + cout << "The total heat needed is: " << totalHeat << " J" << endl; + cout << "The total entropy change is: " << totalEntropy << " J/K" << endl;
+ return 0; } |