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
|
#include <cctype> // Provides toupper
#include <cstdlib> // Provides EXIT_SUCCESS
#include <cstring> // Provides strchr
#include <fstream>
#include <iostream>
#include <string>
#include <sstream> // Operate on strings
#include "hTable.h"
using namespace std;
// get total weight
// Precon: pass in preconstructed table, a string to parse and find weight, and the start of the string
// Postcon: calculates one line from the file and does the appropriate math for molecular weights
double getTWeight(hTable& hashTable, string line, int i);
// reads the table
// Precon: pass in a preconstructed hash table by reference and a string
// Postcon: reads in one line of code and parses it and puts it in the table
void readHTable(hTable& hashTable, string line);
int main()
{
string line;
hTable hashTable;
string source = "PeriodicTableElements.txt";
ifstream file_input;
file_input.open(source);
if(file_input.fail())
{
cerr<<"Could not open input file."<<endl;
system("PAUSE");
exit(0);
}
while(file_input.peek()!=EOF)
{
getline(file_input, line, '\n');
readHTable(hashTable, line);
}
file_input.close(); // End of insertion of elements into the hash table
//**********************************************************************//
string source1 = "formulas.txt";
ifstream file_input1;
file_input1.open(source1);
if(file_input1.fail())
{
cerr<<"Could not open formula file."<<endl;
system("PAUSE");
exit(0);
}
while(file_input1.peek()!=EOF)
{
getline(file_input1, line, '\n');
double tWeight=getTWeight(hashTable, line, 0);
cout<<line<<"="<<tWeight<<endl;
}
file_input1.close();
cout<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
void readHTable(hTable& hashTable, string line)
{
double weight;
int i=0;
while(line[i] != ' ')
++i;
string element=line.substr(0, i);
int elementNumber = element[0]-0;
int weightLength=line.size()-i;
string weightString=line.substr(i,weightLength);
istringstream convert(weightString);
if( !(convert >> weight) )
weight=0;
hashTable.insert(element, weight, elementNumber);
}
double getTWeight(hTable& hashTable, string line, int i)
{
int j;
int multiply;
double tWeight=0.0;
double weight;
while(line[i] != '\0')
{
j=i;
if(line[i]=='(')
{
++i;
int k = i;
while(line[k+1] != ')')
k++;
string lineHelp=line.substr(i,(k-i+1));
weight=getTWeight(hashTable, lineHelp, 0);
i=(k+2);
if(line[i] == '\0')
tWeight = (tWeight + (weight*1));
}
else
{
while(islower(line[i+1]))
i++;
int k=(i-(j+1));
string element = line.substr(j, k);
double elementNumber = element[0]-0;
weight = hashTable.retrieve(elementNumber, element);
++i;
if( !(isdigit(line[i])) )
tWeight = (tWeight + (weight*1));
}
j=i;
while(isdigit(line[i]))
i++;
int k = (i-j);
string inputPasser = line.substr(j, k);
istringstream convert(inputPasser);
if( !(convert>>multiply))
multiply = 0;
tWeight = (tWeight + (weight*multiply));
}
return tWeight;
}
|