-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
94 lines (76 loc) · 2.24 KB
/
Copy pathmain.cpp
File metadata and controls
94 lines (76 loc) · 2.24 KB
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
#include "decoding.h"
#include <fstream>
#include <sstream>
using namespace std;
string readFile();
int main()
{
// Option selection for inputting text from the user
cout << "Huffman Coding" << endl;
cout << "-------------------------------------------------------------------" << endl;
cout << "Choose 0 to exit the program" << endl;
cout << "Choose 1 to input data from the console" << endl;
cout << "Choose 2 to read data from a file" << endl;
cout << "-------------------------------------------------------------------" << endl;
cout << "Option choice: ";
int option;
cin >> option;
string line;
switch (option)
{
case 0:
return 0;
case 1:
cout << "Enter text: " << endl;
getline(cin, line); // Consume newline character
getline(cin, line);
break;
case 2:
// Reading data from a text file
line = readFile();
break;
default:
cout << "Invalid command choice" << endl;
}
// Create a tree based on the input text
Node* root = createHuffmanTree(line);
cout << "\nHuffman Coding Table:\n" << endl;
map<char, string> encodedValues;
encodeNode(root, "", &encodedValues);
// Encode the text based on the created map
string encodedLine = "";
for (char c : line) {
encodedLine += encodedValues[c];
}
cout << "\nText after encoding:\n" << encodedLine << endl;
// Decode the text
cout << "\nText after decoding:\n" << decode(root, encodedLine) << endl;
delete root;
return 0;
}
string readFile()
{
string file_path = "data.txt";
cout << "\nEnter the file name (default is data.txt): ";
cin >> file_path;
ifstream file;
file.open(file_path);
ostringstream stream_content;
string text;
if (file.is_open())
{
while (getline(file, text))
{
stream_content << text;
}
file.close();
text = stream_content.str();
text.erase(remove(text.begin(), text.end(), '\n'), text.cend());
return text;
}
else
{
cout << "Unable to open the file" << endl;
exit(1);
}
}