From aaea366ec4101dd1e45a904b8dd46a18168e1ccd Mon Sep 17 00:00:00 2001 From: SharafatKarim Date: Thu, 7 Mar 2024 21:19:22 +0600 Subject: gitignore updated --- .gitignore | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index eff81b2..07c72b5 100644 --- a/.gitignore +++ b/.gitignore @@ -37,4 +37,10 @@ # Debug files bin/ -obj/ \ No newline at end of file +obj/ + +# executable files +main + +# VsCode +.vscode/ \ No newline at end of file -- cgit v1.2.3 From cd55d812c0728f5084d39856e692ad103615c3b8 Mon Sep 17 00:00:00 2001 From: SharafatKarim Date: Thu, 7 Mar 2024 21:23:43 +0600 Subject: Add build instructions for Visual Studio Code --- README.md | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index aa21190..baf6b11 100644 --- a/README.md +++ b/README.md @@ -12,13 +12,56 @@ This is a C++ project that calculates the entropy change of different elements i ## How to Build +### Code::Blocks + 1. Open the `entropy-calculator.cbp` file in Code::Blocks. 2. Click on the "Build" button or select "Build" from the "Build" menu. -## How to Run +#### How to Run After building the project, you can run the program by clicking on the "Run" button or selecting "Run" from the "Build" menu in Code::Blocks. +### Visual Studio Code + +1. Open the project in Visual Studio Code. +2. Press `F5` to build the project. +3. It will create a `task.json` file in the `.vscode` directory. +4. Configure the `task.json` file to build the project like this one, + +```cpp +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: g++ build active file", + "command": "/usr/bin/g++", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "./headers/allElements.cpp", // Add this line + "-o", + "${fileDirname}/${fileBasenameNoExtension}" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} +``` + +5. Press `F5` again to build and run the project. + ## Contributing Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. @@ -28,6 +71,7 @@ Pull requests are welcome. For major changes, please open an issue first to disc 1. Create a new file in the `elements/` directory with the name of the element (e.g. `water.h` for water element) 2. Define the element in the file. Declare a class with the name of the element which inherits from the `baseElement` class. 3. Write constructor and set the values of the properties of the element. + ```cpp // Example: water.h #ifndef WATER_HPP_INCLUDED @@ -51,7 +95,9 @@ public: }; #endif // WATER_HPP_INCLUDED ``` + 4. Include the header file in the `allElements.cpp` file located in the `headers/` directory. + ```cpp // include new element's header file #include "../elements/water.h" -- cgit v1.2.3 From 1609936fd30418bac47432df4e47c0c5142fb043 Mon Sep 17 00:00:00 2001 From: SharafatKarim Date: Thu, 7 Mar 2024 21:25:49 +0600 Subject: Update build and run instructions in README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index baf6b11..ef903b7 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ After building the project, you can run the program by clicking on the "Run" but } ``` -5. Press `F5` again to build and run the project. +And finally press `F5` again to build and run the project. ## Contributing -- cgit v1.2.3 From 6e2a2938c18ace9d12bc9135bba61bef20de44e4 Mon Sep 17 00:00:00 2001 From: Saifur Rahman Tamim <86656406+sr-tamim@users.noreply.github.com> Date: Fri, 8 Mar 2024 06:41:44 +0600 Subject: contributors in readme --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index ef903b7..5aff9db 100644 --- a/README.md +++ b/README.md @@ -120,3 +120,8 @@ If you find any issues with the project, please open an issue in the repository. [SR Tamim](https://sr-tamim.vercel.app) [![sr-tamim's Profilator](https://profilator.deno.dev/sr-tamim?v=1.0.0.alpha.4)](https://github.com/sr-tamim) + +## Contributors + +[![sr-tamim's Profilator](https://profilator.deno.dev/sr-tamim?v=1.0.0.alpha.4)](https://github.com/sr-tamim) +[![SharafatKarim's Profilator](https://profilator.deno.dev/SharafatKarim?v=1.0.0.alpha.4)](https://github.com/SharafatKarim) -- cgit v1.2.3 From c237e0c92ecee53ed7352b83c69da9aabafcd26b Mon Sep 17 00:00:00 2001 From: SR Tamim Date: Sat, 9 Mar 2024 00:29:25 +0600 Subject: calculation of total heat fixed --- entropy-calculator.cbp | 2 ++ headers/baseElement.h | 67 +++++++++++++++++++++++++++++--------------- headers/readElementState.cpp | 26 +++++++++++++++++ headers/readElementState.h | 10 +++++++ main.cpp | 24 ++++++++++++---- 5 files changed, 101 insertions(+), 28 deletions(-) create mode 100644 headers/readElementState.cpp create mode 100644 headers/readElementState.h 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 @@ + + 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 #include +#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 +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 +std::string readElementState(); + +#endif diff --git a/main.cpp b/main.cpp index 6b6fa8e..9b78f4f 100644 --- a/main.cpp +++ b/main.cpp @@ -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; } -- cgit v1.2.3 From f42ea7d2cab522de28fc63c599f905b787b7fc2d Mon Sep 17 00:00:00 2001 From: SR Tamim Date: Sun, 10 Mar 2024 06:28:31 +0600 Subject: total heat calculation bug fix element state condition was not correct --- headers/baseElement.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/headers/baseElement.h b/headers/baseElement.h index e6372a1..4285fc0 100644 --- a/headers/baseElement.h +++ b/headers/baseElement.h @@ -63,32 +63,32 @@ public: if (toTemp <= meltingPoint) { totalHeat += mass * specificHeatSolid * (toTemp - fromTemp); - if (toTemp == meltingPoint && initialState != finalState) + if (toTemp == meltingPoint && finalState != "Solid") totalHeat += mass * latentHeatOfFusion; } else if (toTemp <= boilingPoint) { if (fromTemp < meltingPoint) totalHeat += mass * specificHeatSolid * (meltingPoint - fromTemp); - if (fromTemp == meltingPoint && initialState != finalState) + if (fromTemp == meltingPoint && initialState != "Liquid") totalHeat += mass * latentHeatOfFusion; if (fromTemp <= meltingPoint) totalHeat += mass * specificHeatLiquid * (toTemp - meltingPoint); else totalHeat += mass * specificHeatLiquid * (toTemp - fromTemp); - if (toTemp == boilingPoint && initialState != finalState) + if (toTemp == boilingPoint && finalState != "Liquid") totalHeat += mass * latentHeatOfVaporization; } else { if (fromTemp < meltingPoint) totalHeat += mass * specificHeatSolid * (meltingPoint - fromTemp); - if (fromTemp == meltingPoint && initialState != finalState) + if (fromTemp == meltingPoint && initialState != "Liquid") totalHeat += mass * latentHeatOfFusion; if (fromTemp < boilingPoint) totalHeat += mass * specificHeatLiquid * (fromTemp - meltingPoint); - if (fromTemp == boilingPoint && initialState != finalState) + if (fromTemp == boilingPoint && initialState != "Gas") totalHeat += mass * latentHeatOfVaporization; if (fromTemp > boilingPoint) -- cgit v1.2.3 From a2d29e8b76d3969e7c5f4f4509abf92727585a1b Mon Sep 17 00:00:00 2001 From: SR Tamim Date: Sun, 10 Mar 2024 06:41:01 +0600 Subject: total entropy calculation fixed --- headers/baseElement.h | 95 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 60 insertions(+), 35 deletions(-) diff --git a/headers/baseElement.h b/headers/baseElement.h index 4285fc0..a3907fe 100644 --- a/headers/baseElement.h +++ b/headers/baseElement.h @@ -16,7 +16,9 @@ private: double specificHeatLiquid; double specificHeatGas; double meltingPoint; - double boilingPoint; + double boilingPoint; + string initialState = ""; + string finalState = ""; protected: void setElementName(string value) { elementName = value; } @@ -47,8 +49,6 @@ public: } double totalHeat = 0; - string initialState = ""; - string finalState = ""; if (fromTemp == meltingPoint || fromTemp == boilingPoint) { cout << "What is the state of the element at the initial temperature?" << endl; @@ -101,38 +101,63 @@ public: double totalEntropyChange(double mass, double fromTemp, double toTemp) { - double totalEntropy = 0; - if (fromTemp <= meltingPoint && toTemp <= meltingPoint) - { - totalEntropy = mass * specificHeatSolid * log(toTemp / fromTemp); - } - else if (fromTemp <= meltingPoint && toTemp >= meltingPoint && toTemp <= boilingPoint) - { - totalEntropy = mass * specificHeatSolid * log(meltingPoint / fromTemp); - totalEntropy += mass * latentHeatOfFusion / meltingPoint; - totalEntropy += mass * specificHeatLiquid * log(toTemp / meltingPoint); - } - else if (fromTemp <= meltingPoint && toTemp >= boilingPoint) - { - totalEntropy = mass * specificHeatSolid * log(meltingPoint / fromTemp); - totalEntropy += mass * latentHeatOfFusion; - totalEntropy += mass * specificHeatLiquid * log(boilingPoint / meltingPoint); - totalEntropy += mass * latentHeatOfVaporization; - totalEntropy += mass * specificHeatGas * log(toTemp / boilingPoint); - } - else if (fromTemp >= meltingPoint && fromTemp <= boilingPoint && toTemp >= meltingPoint && toTemp <= boilingPoint) - { - totalEntropy = mass * specificHeatLiquid * log(toTemp / fromTemp); - } - else if (fromTemp >= meltingPoint && fromTemp <= boilingPoint && toTemp >= boilingPoint) - { - totalEntropy = mass * specificHeatLiquid * log(boilingPoint / fromTemp); - totalEntropy += mass * latentHeatOfVaporization; - totalEntropy += mass * specificHeatGas * log(toTemp / boilingPoint); - } - else if (fromTemp >= boilingPoint && toTemp >= boilingPoint) - { - totalEntropy = mass * specificHeatGas * log(toTemp / fromTemp); + 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); } return totalEntropy; } -- cgit v1.2.3