-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.hpp
118 lines (91 loc) · 2.92 KB
/
main.hpp
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
#ifndef main_hpp
#define main_hpp
#include "ItemType.hpp"
#include "PriorityQueue.cpp"
#include <fstream>
#include <iomanip>
#include <iostream>
#define ARR_BOUND 256
static const std::string FILENAME = "data.txt";
NodeType* newNode(unsigned char ch, unsigned int wt, NodeType* left, NodeType* right)
{
NodeType* node = new NodeType;
node->ascii = ch;
node->weight = wt;
node->zero = left;
node->one = right;
return node;
}
std::ostream& operator<<(std::ostream& out, const NodeType& node)
{
return (out << node.ascii << '\t' << node.weight);
}
std::string fetchFilename()
{
std::string inputLine;
std::cout << "\t1) " << "Enter NOTHING to use default UTF-8 data file ( \"data.txt\" )" << std::endl;
std::cout << "\t2) " << "Provide name of .txt file to read (must be saved in 'HuffTree' directory)" << std::endl;
std::cout << "\nDATA FILE: "; std::getline(std::cin, inputLine);
std::cout << std::endl << std::endl;
return ( (inputLine.size())
? inputLine
: FILENAME );
}
void wait()
{
std::cout << "\n*** Program Paused ***\n\tPress ENTER to continue...";
std::cin.ignore();
std::cout << "*** Program Resumed ***\n" << std::endl;
}
void loadArr(ItemType* arr, std::string filename)
{
std::ifstream inFS;
try
{
inFS.open(filename, std::fstream::in);
if ( !inFS.is_open() | inFS.bad() )
{
throw std::runtime_error("Failed opening input data file : " + filename);
}
else
{
for(int i = 0; i < ARR_BOUND; i++) // Initialize array
{
arr[ i ].ascii = (char)i;
arr[ i ].weight = 0;
}
char ch;
while( inFS.get(ch) )
{
arr[(int)ch].weight++;
}
inFS.clear(); // Reset IO flags
inFS.close();
}
}
catch (const std::runtime_error& excpt)
{
std::cout << excpt.what() << std::endl;
}
inFS.close();
}
void printArr(ItemType* arr)
{
int chCount = 0;
std::cout << "*** Data Array Contents ***" << std::endl;
std::cout << '#' << std::setw(100) << std::setfill('#') << '#' << std::endl;
std::cout << " INDEX ASCII COUNT" << std::endl;
std::cout << "*---------------------------------------*" << std::endl;
for(int i = 0; i < ARR_BOUND; i++)
{
if ( arr[i].weight )
{
std::cout << ' ' << '\t' << i << '\t' << arr[i].ascii << '\t' << arr[i].weight << std::endl;
chCount += arr[i].weight;
}
}
std::cout << "*---------------------------------------*" << std::endl;
std::cout << " " << chCount << " total characters processed" << std::endl;
std::cout << '#' << std::setw(100) << std::setfill('#') << '#' << std::endl;
}
#endif // main_hpp //