-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompress.cpp
More file actions
56 lines (53 loc) · 1.89 KB
/
Compress.cpp
File metadata and controls
56 lines (53 loc) · 1.89 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
//
// Created by jpenguin on 1/30/22.
//
#include "Compress.h"
#include <iostream>
using std::cout;
void Compress::GetData(ifstream &toCompress, string *uniqueWords,
int *numUniqueWords, int *wordOccurrences, int *numWords)
{
int index; // position of word in list
string currentWord;
bool found;
// Put next word into currentWord; because we're using
// arrays and not vectors, stop at 1000 words
while (toCompress >> currentWord && *numWords < 1000) {
found = false;
for (int i = 0; i <= *numUniqueWords; i++) {
if (uniqueWords[i] == currentWord) {
found = true; // set to true if currentWord found in list
index = i; // set index to position found at
break; // if found, exit for loop
}
}
if (!found) {
uniqueWords[*numUniqueWords] =
currentWord; // if not found, add to list
index = *numUniqueWords; // set index to lat position on list
*numUniqueWords += 1; // next unique word will go in next spot
}
wordOccurrences[*numWords] =
index; // append position word was found or was placed at
*numWords += 1; // increment total word count
}
}
void Compress::Write(string *uniqueWords, int numUniqueWords,
int *wordOccurrences, int numWords)
{
ofstream output("output.jzip");
if (output.is_open()) {
for (int count = 0; count < numWords; count++) {
output << wordOccurrences[count] << " ";
}
output << "\n\n\n";
for (int count = 0; count < numUniqueWords; count++) {
output << uniqueWords[count] << " ";
}
output.close();
cout << "Saved to output.jzip";
} else {
cout << "\a\nUnable to create file\n";
throw 1; // Throw exception for main to handle
}
}