Hey guys, I have something where we have to solve how to read in chemical formulas and use a binary search to find the specific elements and multiply them to get the correct answer for atomic weight. Please please please be nice...I have gotten some rough people on here. This is only my second semester and I am trying to study as best I can and google things before coming to forums.
Instructions:
The Chemistry Dept. has asked you to develop a program that will calculate molecular weights of compounds given the chemical formula. For example H2O (water) would contain 2 hydrogen atoms weighting 1.008 and 1 oxygen weighting 15.999. The weight of water is 2 x 1.008 + 1 x 15.999 yielding 18.015 atomic weight.
The input to you program will be in the following form:
element ( number of atoms) element (number of atoms) ...
for example H2O would be represented H(2)O. If the ()'s are not present, assume one (1) atom. Another example acetic acid would be represented CH(3)COOH or C(2)H(4)O(2).
If an element has two letters symbol representation ie Silver is Ag, the second letter will be lower case indicating it is part of the symbol representation.
A list of all the chemical elements can be found in the data file 'Element.dat'. There is one input element per line where the element name appears first followed by its atomic weight.
Ex. Al 26.98
Sb 121.75
S 32.06
Ba 137.34
...
A second input file 'Formula.dat' contains the test formulae to use in testing your program. There will be one formula per line. For the output, print out the formula and its Molecular weight in a nice table form (ie line up the columns).
Restrictions: You are to use an array of structure to hold the Symbol and its weight. ‘Formula.txt’ file.
Use a Binary search to look up elements in the element table.
You are to use Functions/Procedures in your implementation.
Format your output in a table form (ie headings and straight columns)
Element.txt file:
H 1.008
C 12.011
N 14.0067
O 15.9994
F 18.9984
Na 22.9898
Mg 24.305
Al 26.9815
Si 28.086
P 30.9738
S 32.06
Cl 35.453
K 39.102
Ti 47.9
V 50.9414
Cr 51.996
Fe 55.847
Ni 58.71
Zn 65.37
As 74.9216
Se 78.96
Br 79.904
Ag 107.868
I 126.905
Te 127.6
Pt 195.09
Au 196.967
Tl 204.37
Th 232.038
U 238.029
|
formula text file:
Na(3)P(5)O(4)
TeCl(4)
V(2)O(5)
HCONHCH(2)CH(2)OH
ZnC(4)H(6)O(4)
UO(2)N(2)O(6)H(2)O
FeTiO(3)
MgC(4)H(6)O(4)
C(16)N(33)OH
AgSiF(6)MgOH(6)O(3)
NiSO(4)N(2)H(4)SO(4)H(12)O(6)
Tl(2)S(3)O(12)H(14)O(7)
CBr(4)
ZnCO(3)
UO(2)CO(3)
ThS(2)O(8)
H(4)P(2)O(5)
H(2)SeO(5)
K(2)Cr(2)O(7)
K(2)PtI(6)
|
***********************************************************************
You are to assume if there is no {} then it is considered one. We are supposed to do this as an array of structs. We HAVE NOT learned pointers, vectors, or other advanced code. We just learned structs this week.
I have already made a struct called element properties that stores the element name and atomic weight and it has already been sorted lowest to highest for the binary search. I also read each formula as a complete line and stored them in a string array just so they could be displayed in my table.
My problem is: we are supposed to read the file for the formula in one character at a time, some are one uppercase letter and some are an uppercase with a lowercase. Also I have to read the (num) inside the punctuation and be able to use that to know how many times to multiply. How do I read one at a time and flag it so that the system knows to either temporarily store it in an array (my thought) or as soon as its read, use the binary search to look up, but still be able to read the ().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
struct elementProp //element properties
{
string name;
float weight;
};
//writing all the formulas to an array
string formulas[25];
for(i=0;i<25;i++)
{
getline(infile2,formula);
formulas[i]=formula;
cout<<" "<<formulas[i]<<endl;
}
|
Someone else online shows doing this, but it only reads it as complete
strings. //someone else's code, not mine.
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
|
void StringMake()
{
string NEL;
char FR;
inputFile2 >> FR;
while(inputFile2.get(FR))
{
if (FR == '\n')
{
break;
}
else if (islower(FR))
{
NEL = NEL + FR;
}
if (isupper(FR))
{
NEL = NEL + FR;
}
if (isdigit(FR))
{
NEL = NEL + FR;
}
if (ispunct(FR))
{
NEL = NEL + FR;
}
}
}
|