-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPCOPY.CPP
More file actions
293 lines (256 loc) · 7.94 KB
/
Copy pathPCOPY.CPP
File metadata and controls
293 lines (256 loc) · 7.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#include "atomicmass.h" //This file defines elements[] and numElements
// Class to handle atomic mass retrieval from header file
class Molecule
{
public:
double getAtomicMass(const char* element)
{
// Loop through all elements to find the atomic mass for the given symbol
for (int i = 0; i < numElements; i++)
{
if (strcmp(elements[i].symbol, element) == 0)
{
return elements[i].mass;
}
}
return 0.0; // Return 0 if element not found (error case)
}
};
// Class to calculate molar mass of a chemical formula
class MolarMassCalculator
{
private:
Molecule M;
// Recursively calculates molar mass, handling parentheses for grouping
double calculateMolarMass(const char* formula, int& i)
{
double totalMass = 0.0;
double groupMass = 0.0;
while (formula[i] != '\0')
{
// Handle grouped elements inside parentheses
if (formula[i] == '(')
{
i++; // Skip '('
groupMass = calculateMolarMass(formula, i); // Recursively calculate the group mass
totalMass += groupMass;
}
else if (formula[i] == ')')
{
i++; // Skip ')'
int groupMultiplier = 0;
// Process multiplier after ')'
while (isdigit(formula[i]))
{
groupMultiplier = groupMultiplier * 10 + (formula[i] - '0');
i++;
}
if (groupMultiplier == 0)
{
groupMultiplier = 1; // If no multiplier, default to 1
}
return totalMass * groupMultiplier; // Return mass of the group
}
else
{
// Extract element symbol (e.g., "H" or "Na")
char element[3] = {0}; // Initialize empty string
element[0] = formula[i];
i++;
// If the next character is lowercase, it's part of the symbol (e.g., "Na")
if (islower(formula[i]))
{
element[1] = formula[i];
i++;
}
// Get atomic mass of the element
double atomicMass = M.getAtomicMass(element);
int subscript = 0;
// Extract subscript if present (e.g., "2" in H2O)
while (isdigit(formula[i]))
{
subscript = subscript * 10 + (formula[i] - '0');
i++;
}
if (subscript == 0)
{
subscript = 1; // Default subscript to 1 if not present
}
// Add the element's mass to the total mass
totalMass += atomicMass * subscript;
}
}
return totalMass;
}
public:
// Claculating molar mass of input
double getMolarMass(const char* formula)
{
int i = 0;
return calculateMolarMass(formula, i);
}
// Display calculated molar mass to the user
void DisplayMolarMass()
{
char formula[100];
cout << "\n======================== Molar Mass Calculator ========================\n";
cout << "\nEnter the chemical formula (e.g., H2O, CO2, Ca(NO3)2): ";
cin >> formula;
double MolarMass = getMolarMass(formula); // Calculate molar mass
cout << "\nCalculated Molar Mass:\n";
cout << "------------------------------------------------\n";
cout << "Formula: " << formula << "\n";
cout << "Molar Mass: " << MolarMass << " g/mol\n";
cout << "------------------------------------------------\n";
}
};
// Class to calculate the mass of solute needed to prepare a solution
class SolutionMaker
{
public:
void calculateMassOfSolute()
{
double molarity, volume, MolarMass;
char formula[100];
MolarMassCalculator MM;
int choice;
cout << "\n======================== Solute Mass Calculator ========================\n";
cout << "\nEnter the molarity of the solution (mol/L): ";
cin >> molarity;
cout << "Enter the volume of the solution (L): ";
cin >> volume;
cout << "\nWould you like to:\n";
cout << "1. Enter the molar mass directly.\n";
cout << "2. Calculate molar mass from the chemical formula.\n";
cout << "\nPlease enter your choice (1 or 2): ";
cin >> choice;
if (choice == 1)
{
cout << "\nPlease enter the molar mass of the solute (g/mol): ";
cin >> MolarMass;
}
else if (choice == 2)
{
cout << "\nEnter the chemical formula (e.g., H2O, CO2, Ca(NO3)2): ";
cin >> formula;
MolarMass= MM.getMolarMass(formula); // Use molar mass calculator if option 2 is selected
}
else
{
cout << "\n[Error] Invalid choice! Please restart the program and try again.\n";
return;
}
// Calculate and display the mass of solute
double massOfSolute = molarity * volume * MolarMass;
cout << "\nCalculated Mass of Solute:\n";
cout << "------------------------------------------------\n";
cout << "Molarity (M): " << molarity << " mol/L\n";
cout << "Volume (V): " << volume << " L\n";
cout << "Molar Mass (M_m): " << MolarMass << " g/mol\n";
cout << "Mass of Solute: " << massOfSolute << " grams\n";
cout << "------------------------------------------------\n";
}
};
// Class to perform dilution calculations (M1V1 = M2V2)
class DilutionCalculator
{
public:
void calculateDilution()
{
double M1 = -1, V1 = -1, M2 = -1, V2 = -1;
cout << "\n========================= Dilution Calculator =========================\n";
cout << "Enter the three known values and -1 for the unknown one.\n";
cout << "\nEnter M1 (initial concentration in Molar): ";
cin >> M1;
cout << "Enter V1 (initial volume in Liters): ";
cin >> V1;
cout << "Enter M2 (final concentration in Molar): ";
cin >> M2;
cout << "Enter V2 (final volume in Liters): ";
cin >> V2;
// Calculate unknown value based on -1 added to it
if (M1 == -1)
{
M1 = (M2 * V2) / V1;
cout << "\nThe initial concentration (M1) is: " << M1 << " M\n";
}
else if (V1 == -1)
{
V1 = (M2 * V2) / M1;
cout << "\nThe initial volume (V1) is: " << V1 << " L\n";
}
else if (M2 == -1)
{
M2 = (M1 * V1) / V2;
cout << "\nThe final concentration (M2) is: " << M2 << " M\n";
}
else if (V2 == -1)
{
V2 = (M1 * V1) / M2;
cout << "\nThe final volume (V2) is: " << V2 << " L\n";
}
else
{
cout << "\n[Error] Three values must be entered, with one left as -1.\n";
}
}
};
int main()
{
clrscr(); // Clear screen before starting
MolarMassCalculator MM;
SolutionMaker SM;
DilutionCalculator DC;
int option;
char calculateAgain;
do
{
// Display main menu options
cout << "\n============================== ChemCalC++ ==============================\n";
cout << "Please select an option:\n";
cout << "1. Calculate Molar Mass of a Compound\n";
cout << "2. Calculate Mass of Solute for Solution Preparation\n";
cout << "3. Calculate Dilution (M1V1 = M2V2)\n";
cout << "\nEnter your choice (1, 2, or 3): ";
cin >> option;
// Input validation - check if option is between 1 and 3
if (cin.fail() || option < 1 || option > 3)
{
// If input is invalid, clear the input and prompt again
cin.clear(); // Clear the error flag
cin.ignore(10000, '\n'); // Discard invalid input
cout << "\n[Error] Invalid choice! Please enter a valid option (1, 2, or 3).\n";
continue; // Skip to the next iteration of the loop
}
// Perform the selected calculation
if (option == 1)
{
MM.DisplayMolarMass();
}
else if (option == 2)
{
SM.calculateMassOfSolute();
}
else if (option == 3)
{
DC.calculateDilution();
}
// Ask if the user wants to calculate more
cout << "\nDo you want to perform another calculation? (y/n): ";
cin >> calculateAgain;
// Input validation for continue option
while (calculateAgain != 'y' && calculateAgain != 'Y' && calculateAgain != 'n' && calculateAgain != 'N')
{
cout << "\n[Error] Invalid choice! Please enter 'y' to continue or 'n' to exit: ";
cin >> calculateAgain;
}
}
while (calculateAgain == 'y' || calculateAgain == 'Y');
cout << "\nThank you for using the ChemCalC++.\nGoodbye!\n";
getch();
return 0;
}