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
|
#pragma once
#include <cctype> // Provides toupper
#include <cstdlib> // Provides EXIT_SUCCESS and size_t
#include <iostream> // Provides cin, cout
#include "table2.h" // Provides the table class
#include <fstream>
#include <cstring> // Provides strchr
#include <string>
#include <sstream>
using namespace std;
//get total weight
//preconditions: pass in preconstructed table, a string to parse and find weight of, and the start of the string
//postcondition: calculates one line from file and does appropriate math for molecular weights
double get_total_weight(table& hash_table, string line_input, int i);
//reads in the table
//precondition: pass in a preconstructed hash table by reference and a string to read in
//post condition: reads in one line of code and parses it and puts in the table
void read_in_table(table& hash_table, string line_input);
int main()
{
string line_input;
table hash_table;
string pathway = "PeriodicTableElements.txt";
ifstream input;
input.open(pathway);
if (input.fail( ))
{
cerr << "Could not open input file." << endl;
system("PAUSE");
exit(0);
}
while(input.peek()!= EOF)
{
getline(input, line_input, '\n');
read_in_table(hash_table, line_input);
}
input.close( );//end of inserting the table of elements into hash table.
//*************************************************************************************************************************************
//start of the formula section!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
string pathway1 = "formulas.txt";
ifstream input1;
input1.open(pathway1);
if (input1.fail( ))
{
cerr << "Could not open formula file." << endl;
system("PAUSE");
exit(0);
}
while(input1.peek()!=EOF)
{
getline(input1, line_input, '\n');
double total_weight = get_total_weight(hash_table, line_input, 0);
cout<<line_input<<"="<<total_weight<<endl;
}
input1.close();
cout<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
void read_in_table(table& hash_table, string line_input)
{
double weight;
int i = 0;
while(line_input[i] != ' ')
++i;
string element = line_input.substr (0,i);
int element_number = element[0]-0;
int weight_length = line_input.size() - i;
string weight_string = line_input.substr(i,weight_length);
istringstream convert(weight_string);
if ( !(convert >> weight) )
weight = 0;
hash_table.insert(element, weight, element_number);
}
double get_total_weight(table& hash_table, string line_input, int i)
{
int j;
int multiplier;
double total_weight=0.0;
double weight;
while(line_input[i] != '\0')
{
j=i;
if(line_input[i] == '(')
{
++i;
int k = i;
while(line_input[k+1]!=')')
k++;
string line_help = line_input.substr(i,k-i+1);
weight = get_total_weight(hash_table, line_help, 0);
i = k+2;
if(line_input[i] == '\0')
total_weight = total_weight+ weight*1;
}
else
{
while(islower(line_input[i+1]))
i++;
int k = i-j+1;
string element = line_input.substr (j,k);
double element_number = element[0]-0;
weight = hash_table.retrieve(element_number, element);
++i;
if(!(isdigit(line_input[i])))
total_weight = total_weight + weight*1;
}
j=i;
while(isdigit(line_input[i]))
i++;
int k = i-j;
string line_input_passer = line_input.substr(j,k);
istringstream convert(line_input_passer);
if ( !(convert >> multiplier) ) //give the value to weight using the characters in the stream
multiplier = 0;
total_weight = total_weight + weight*multiplier;
}
return total_weight;
}
|