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
|
#include <cctype> // Provides toupper
#include <cstdlib> // Provides EXIT_SUCCESS and size_t
#include <iostream> // Provides cin, cout
#include "table.h" // Provides the table class
#include <fstream>
#include <cstring> // Provides strchr
#include <string>
#include <sstream>
using namespace std;
void read_in_table(table& hash_table, string line_input);
double get_total_weight(table& hash_table, string line_input, int i);
int main()
{
string line_input;
table hash_table;
char dataFileName[] = "PeriodicTableElements.txt";
// I've got the file name, now try to open the file for reading
ifstream fileData;
fileData.open(dataFileName, 0);
if (fileData.good() == false)
{
cout << "ERROR: can't open data file: " << dataFileName << endl;
// wait for the user to press enter to quit
cout << endl << "Press the [Enter] key to quit...";
getchar();
return -1;
}
char dataFileName1[] = "formulas.txt";
// I've got the file name, now try to open the file for reading
ifstream fileData1;
fileData1.open(dataFileName1, 0);
if (fileData1.good() == false)
{
cout << "ERROR: can't open data file: " << dataFileName1 << endl;
// wait for the user to press enter to quit
cout << endl << "Press the [Enter] key to quit...";
getchar();
return -1;
}
// I've got the data file open.
// read and use the data
string hash;
while (getline(fileData, hash))
{
cout << "" << hash << endl;
cout << endl;
}
fileData1.close();
fileData1.open(dataFileName1, 0);
while (getline(fileData1, hash))
{
cout << hash << endl;
getline(fileData, line_input, '\n');
double total_weight = get_total_weight(hash_table, line_input, 0);
cout << line_input << "=" << " " << total_weight << endl;
}
fileData.close();
cout << endl << "Press the [Enter] key to quit...";
getchar();
}
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;
}
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;
}
//table.cpp
#include "table.h"
#include <iostream>
table::table()
{
for (int i = 0; i < SIZE; i++)
{
array[i] = new node(-1, "");
}
}
void table::decon_help(node * n)
{
if (n->next != NULL)
{
decon_help(n->next);
}
delete n;
return;
}
table::~table()
{
for (int i = 0; i < SIZE; i++)
{
if (array[i]->next != NULL)
{
decon_help(array[i]->next);
}
delete array[i];
}
}
void table::insert(string passed_ele, double passed_weight, double key)
{
int index = hash(key);
if (array[index]->data == -1 || array[index]->data == -2)
{
array[index]->data = passed_weight;
array[index]->element = passed_ele;
}
else
{
insert(array[index]->next, passed_ele, passed_weight);
}
}
void table::insert(node *& n, string passed_ele, double passed_weight)
{
if (n == NULL)
{
n = new node(passed_weight, passed_ele);
}
else
{
insert(n->next, passed_ele, passed_weight);
}
}
double table::retrieve(double d, string element)
{
int index = hash(d);
if (array[index]->element == element)
{
return array[index]->data;
}
else
{
return retrieve(d, element, array[index]->next);
}
}
double table::retrieve(double d, string element, node * n)
{
if (n->element == element)
{
return n->data;
}
return retrieve(d, element, n->next);
}
int table::hash(double d)
{
int ret = (int)(d / .05555) % SIZE;
return ret;
}
//table.h
#pragma once
#include <cstdlib> // Provides size_t
#include <string>
using namespace std;
struct node
{
double data;
string element;
node* next;
node(double d, string ele)
{
data = d;
element = ele;
next = NULL;
}
};
const int SIZE = 50;
class table
{
node* array[SIZE];
public:
//constructor
//pre:none
//post: creates array of nodes of size 50 and initialized each value to -1
table();
//deconstructor
//deletes all dynamic memory
~table();
//helps to delete dynamic alloc memory
void decon_help(node* n);
//inserts an element from periodic table into a hash table
void insert(string, double, double);
//inserts an element from periodic table into hash table when there is a collision
void insert(node*& n, string passed_ele, double passed_weight);
//returns the value for the element
double retrieve(double d, string element);
//returns value for element when there is a collision
double retrieve(double d, string element, node* n);
//hash function to spread out the entries
int hash(double);
//overload the [] brackets
table operator[](int);
//Clear (remove all from hash table)
void clear(node* array[]) { table::~table(); }
};
|