diff options
-rw-r--r-- | elements/Silver.h | 26 | ||||
-rw-r--r-- | entropy-calculator.cbp | 12 | ||||
-rw-r--r-- | headers/allElements.cpp | 2 | ||||
-rw-r--r-- | headers/baseElement.h | 161 | ||||
-rw-r--r-- | main.cpp | 91 |
5 files changed, 167 insertions, 125 deletions
diff --git a/elements/Silver.h b/elements/Silver.h new file mode 100644 index 0000000..bef246c --- /dev/null +++ b/elements/Silver.h @@ -0,0 +1,26 @@ +#ifndef SILVER_HPP_INCLUDED +#define SILVER_HPP_INCLUDED + +#include "../headers/baseElement.h" + +class Silver : public baseElement +{ +public: + Silver() + { + setElementName("Silver"); + + setSpecificHeatLiquid(240); // J/kg.K + setSpecificHeatSolid(240); // J/kg.K + + // No specific heat capacity for gas + + setLatentHeatOfFusion(104.72); // J/kg + setLatentHeatOfVaporization(2318.83); //J/kg + + setMeltingPoint(1234.8); // k + setBoilingPoint(2485); // k + } +}; + +#endif // SILVER_HPP_INCLUDED diff --git a/entropy-calculator.cbp b/entropy-calculator.cbp index 797dabf..0474bf0 100644 --- a/entropy-calculator.cbp +++ b/entropy-calculator.cbp @@ -31,16 +31,24 @@ <Compiler> <Add option="-Wall" /> <Add option="-fexceptions" /> + <Add directory="headers" /> </Compiler> + <Linker> + <Add library="gmp" /> + <Add library="gmpxx" /> + </Linker> <Unit filename="elements/water.h" /> <Unit filename="headers/allElements.cpp" /> <Unit filename="headers/allElements.h" /> + <Unit filename="headers/baseElement.h" /> <Unit filename="headers/readElementState.cpp" /> <Unit filename="headers/readElementState.h" /> - <Unit filename="headers/baseElement.h" /> <Unit filename="main.cpp" /> <Extensions> - <lib_finder disable_auto="1" /> + <lib_finder disable_auto="1"> + <lib name="gmp" /> + <lib name="gmpxx" /> + </lib_finder> </Extensions> </Project> </CodeBlocks_project_file> diff --git a/headers/allElements.cpp b/headers/allElements.cpp index 7c0e6a2..1576dfd 100644 --- a/headers/allElements.cpp +++ b/headers/allElements.cpp @@ -1,5 +1,6 @@ #include<vector> #include "../elements/water.h" +#include "../elements/Silver.h" #include "baseElement.h" @@ -7,5 +8,6 @@ vector<baseElement*> getAllElements() { vector<baseElement*> elements; elements.push_back(new water()); + elements.push_back(new Silver()); return elements; } diff --git a/headers/baseElement.h b/headers/baseElement.h index a3907fe..42fb8e5 100644 --- a/headers/baseElement.h +++ b/headers/baseElement.h @@ -3,21 +3,33 @@ #include <iostream> #include <math.h> +#ifdef _USE_GMP +# include <gmp.h> +# include <gmpxx.h> +#endif // _USE_GMP #include "readElementState.h" using namespace std; +#ifdef _USE_GMP + typedef mpf_class bigfloat; +# define bigfloat_unwrap(X) (bigfloat(X)).get_d() +#else + typedef double bigfloat; +# define bigfloat_unwrap(X) (X) +#endif // _USE_GMP + class baseElement { private: string elementName; - double latentHeatOfFusion; - double latentHeatOfVaporization; - double specificHeatSolid; - double specificHeatLiquid; - double specificHeatGas; - double meltingPoint; - double boilingPoint;
- string initialState = "";
+ bigfloat latentHeatOfFusion; + bigfloat latentHeatOfVaporization; + bigfloat specificHeatSolid; + bigfloat specificHeatLiquid; + bigfloat specificHeatGas; + bigfloat meltingPoint; + bigfloat boilingPoint; + string initialState = ""; string finalState = ""; protected: @@ -32,15 +44,15 @@ protected: public: string getElementName() { return elementName; } - double getLatentHeatOfFusion() { return latentHeatOfFusion; } - double getLatentHeatOfVaporization() { return latentHeatOfVaporization; } - double getSpecificHeatSolid() { return specificHeatSolid; } - double getSpecificHeatLiquid() { return specificHeatLiquid; } - double getSpecificHeatGas() { return specificHeatGas; } - double getMeltingPoint() { return meltingPoint; } - double getBoilingPoint() { return boilingPoint; } + bigfloat getLatentHeatOfFusion() { return latentHeatOfFusion; } + bigfloat getLatentHeatOfVaporization() { return latentHeatOfVaporization; } + bigfloat getSpecificHeatSolid() { return specificHeatSolid; } + bigfloat getSpecificHeatLiquid() { return specificHeatLiquid; } + bigfloat getSpecificHeatGas() { return specificHeatGas; } + bigfloat getMeltingPoint() { return meltingPoint; } + bigfloat getBoilingPoint() { return boilingPoint; } - double totalHeatNeeded(double mass, double fromTemp, double toTemp) + bigfloat totalHeatNeeded(double mass, double fromTemp, double toTemp) { if (fromTemp < 0 || toTemp < 0) { @@ -48,7 +60,7 @@ public: return 0; } - double totalHeat = 0; + bigfloat totalHeat = 0; if (fromTemp == meltingPoint || fromTemp == boilingPoint) { cout << "What is the state of the element at the initial temperature?" << endl; @@ -99,68 +111,65 @@ public: return totalHeat; } - double totalEntropyChange(double mass, double fromTemp, double toTemp) + bigfloat totalEntropyChange(double mass, double fromTemp, double toTemp) { - if (fromTemp < 0 || toTemp < 0)
- {
- cout << "Temperature cannot be less than 0 K" << endl;
- return 0;
- }
-
- double totalEntropy = 0;
- string initialState = "";
- string finalState = "";
- if (fromTemp == meltingPoint || fromTemp == boilingPoint)
- {
- cout << "What is the state of the element at the initial temperature?" << endl;
- if (initialState != "")
- initialState = readElementState();
- }
- if (toTemp == meltingPoint || toTemp == boilingPoint)
- {
- cout << "What is the state of the element at the final temperature?" << endl;
- if (finalState != "")
- finalState = readElementState();
- }
-
- if (toTemp <= meltingPoint)
- {
- totalEntropy += mass * specificHeatSolid * log(toTemp / fromTemp);
- if (toTemp == meltingPoint && finalState != "Solid")
- totalEntropy += mass * latentHeatOfFusion / meltingPoint;
- }
- else if (toTemp <= boilingPoint)
- {
- if (fromTemp < meltingPoint)
- totalEntropy += mass * specificHeatSolid * log(meltingPoint / fromTemp);
- if (fromTemp == meltingPoint && initialState != "Liquid")
- totalEntropy += mass * latentHeatOfFusion / meltingPoint;
- if (fromTemp <= meltingPoint)
- totalEntropy += mass * specificHeatLiquid * log(toTemp / meltingPoint);
- else
- totalEntropy += mass * specificHeatLiquid * log(toTemp / fromTemp);
- if (toTemp == boilingPoint && finalState != "Liquid")
- totalEntropy += mass * latentHeatOfVaporization / boilingPoint;
- }
- else
- {
- if (fromTemp < meltingPoint)
- totalEntropy += mass * specificHeatSolid * log(meltingPoint / fromTemp);
- if (fromTemp == meltingPoint && initialState != "Liquid")
- totalEntropy += mass * latentHeatOfFusion / meltingPoint;
-
- if (fromTemp < boilingPoint)
- totalEntropy += mass * specificHeatLiquid * log(fromTemp / meltingPoint);
- if (fromTemp == boilingPoint && initialState != "Gas")
- totalEntropy += mass * latentHeatOfVaporization / boilingPoint;
-
- if (fromTemp > boilingPoint)
- totalEntropy += mass * specificHeatGas * log(toTemp / fromTemp);
- else
- totalEntropy += mass * specificHeatGas * log(toTemp / boilingPoint);
+ if (fromTemp < 0 || toTemp < 0) + { + cout << "Temperature cannot be less than 0 K" << endl; + return 0; + } + + bigfloat totalEntropy = 0; + if (fromTemp == meltingPoint || fromTemp == boilingPoint) + { + cout << "What is the state of the element at the initial temperature?" << endl; + if (initialState != "") + initialState = readElementState(); + } + if (toTemp == meltingPoint || toTemp == boilingPoint) + { + cout << "What is the state of the element at the final temperature?" << endl; + if (finalState != "") + finalState = readElementState(); + } + + if (toTemp <= meltingPoint) + { + totalEntropy += mass * specificHeatSolid * log(bigfloat_unwrap(toTemp / fromTemp)); + if (toTemp == meltingPoint && finalState != "Solid") + totalEntropy += mass * latentHeatOfFusion / meltingPoint; + } + else if (toTemp <= boilingPoint) + { + if (fromTemp < meltingPoint) + totalEntropy += mass * specificHeatSolid * log(bigfloat_unwrap(meltingPoint / fromTemp)); + if (fromTemp == meltingPoint && initialState != "Liquid") + totalEntropy += mass * latentHeatOfFusion / meltingPoint; + if (fromTemp <= meltingPoint) + totalEntropy += mass * specificHeatLiquid * log(bigfloat_unwrap(toTemp / meltingPoint)); + else + totalEntropy += mass * specificHeatLiquid * log(bigfloat_unwrap(toTemp / fromTemp)); + if (toTemp == boilingPoint && finalState != "Liquid") + totalEntropy += mass * latentHeatOfVaporization / boilingPoint; + } + else + { + if (fromTemp < meltingPoint) + totalEntropy += mass * specificHeatSolid * log(bigfloat_unwrap(meltingPoint / fromTemp)); + if (fromTemp == meltingPoint && initialState != "Liquid") + totalEntropy += mass * latentHeatOfFusion / meltingPoint; + + if (fromTemp < boilingPoint) + totalEntropy += mass * specificHeatLiquid * log(bigfloat_unwrap(fromTemp / meltingPoint)); + if (fromTemp == boilingPoint && initialState != "Gas") + totalEntropy += mass * latentHeatOfVaporization / boilingPoint; + + if (fromTemp > boilingPoint) + totalEntropy += mass * specificHeatGas * log(bigfloat_unwrap(toTemp / fromTemp)); + else + totalEntropy += mass * specificHeatGas * log(bigfloat_unwrap(toTemp / boilingPoint)); } return totalEntropy; } }; - #endif // BASEELEMENT_H_INCLUDED @@ -1,47 +1,44 @@ -#include <iostream>
-#include <vector>
-#include "headers/allElements.h"
-#include "headers/baseElement.h"
-
-using namespace std;
-
-int main()
-{
- vector<baseElement*> elements = getAllElements();
- cout << "Choose an element: " << endl;
- for (int i = 0; i < elements.size(); i++)
- {
- cout << i + 1 << ". " << elements[i]->getElementName() << endl;
- }
- int choice;
- cout << "Write the number of the element: ";
- cin >> choice;
- cout << endl << elements[choice - 1]->getElementName() << " Selected" << endl;
-
- double mass, fromTemp, toTemp;
- cout << "Enter the mass of the element (in Kg): ";
- 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;
- 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;
-
- //thank you.. feel free to contribute here..:)
-
-}
+#define _USE_GMP +#include <iostream> +#include <vector> +#include "allElements.h" + +using namespace std; + +int main() +{ + vector<baseElement*> elements = getAllElements(); + cout << "Choose an element: " << endl; + for (auto i = 0UL; i < elements.size(); i++) { + cout << i + 1 << ". " << elements[i]->getElementName() << endl; + } + int choice; + cout << "Write the number of the element: "; + cin >> choice; + cout << endl << elements[choice - 1]->getElementName() << " Selected" << endl; + + double mass, fromTemp, toTemp; + cout << "Enter the mass of the element (in Kg): "; + 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; + if (fromTemp > toTemp) { + cout << "Initial temperature can not be greater than final temperature" << endl; + return 0; + } + + double totalHeat = bigfloat_unwrap(elements[choice - 1]->totalHeatNeeded(mass, fromTemp, toTemp)); + double totalEntropy = bigfloat_unwrap(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; + + //thank you.. feel free to contribute here..:) + +} |