forked from tscott8/cs235
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cs235_assign11.cpp
103 lines (93 loc) · 3.05 KB
/
cs235_assign11.cpp
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
/******************************************************************************
* Program:
* Assignment 11, Hashing
* Brother Ercanbrack, CS 235
* Author:
* Tyler Scott
* Summary:
* This program does hashing
*
* Estimated time: 5.0hrs
* Actual time: 2.0hrs
******************************************************************************/
#include <iostream>
#include <fstream>
#include <list>
#include <string>
using namespace std;
/******************************************************************************
* Index does the actual hashing.
*****************************************************************************/
int index(string word)
{
int x = 0;
for (int i = 0; i < word.length(); i++)
x += word[i]; // add up to a sum
return x % 67; // then divide by 67 and keep the remainder
}
/******************************************************************************
* Hashing pushes the indexed words onto the array
*****************************************************************************/
void hashing(string word, list < string > * array)
{
int y = index(word); // assign a holder for the indexed word
array[y].push_back(word); // push it onto the array
}
/******************************************************************************
* Iterates through the lists and displays properly
*****************************************************************************/
void display(list < string > * array)
{
for (int i = 0; i < 67; i++) // loop through to the end of the array
{
cout << "table[" << i << "]";
if (i < 10) // if the position is less than 10, you need an extra space
{
cout << " -> "; // display with the extra space
}
if (i >= 10)
{
cout << "-> ";
}
if (array[i].empty()) // check empty
cout << "(empty)" << endl;
else
{
for (list < string > ::iterator IT = array[i].begin();
IT != array[i].end();)
{
// iterate through the list in the specified array location
cout << *IT; // display the contents
IT++; // move to the next position
if (IT != array[i].end())
cout << ", ";
}
cout << endl;
}
}
}
/******************************************************************************
* Main is set up to read in the file and then call the necessary functions
* to run the program
*****************************************************************************/
int main(int argc, char* argv[])
{
// declare a new array of lists
list < string > array[67];
// open the file from command line
ifstream inFile;
inFile.open(argv[argc - 1]);
if (inFile.fail())
{
cout << "open file error " << argv[argc - 1] << endl;
return 0;
}
// read in the information and store it as "word"
string word;
while (inFile >> word)
{
hashing(word, array); // pass it to the hashing function
}
display(array); // call the display
return 0;
}