This repository was archived by the owner on Mar 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDigitStat.cpp
More file actions
244 lines (204 loc) · 6.48 KB
/
DigitStat.cpp
File metadata and controls
244 lines (204 loc) · 6.48 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
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
/*** DigitStat.cpp v1.0 ***/
/*** (C) 2013 DigitStat Corp. ***/
/*** The Leading Name in Digit Statistics ***/
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <algorithm>
#include "DigitStat.h"
#include <list>
#include <locale>
#define ERROR_INVALID_ARGS 1
#define ERROR_INVALID_FILE 2
#define ERROR_BAD_FORMAT 3
using namespace std;
int main(int argc, char* argv[]) {
// Variables...
ofstream outFile;
ifstream inFile;
LinkedNode *head = nullptr;
int integers = 0, fractions = 0;
int firstDigCount[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
if (argc != 3) { // Check argument count...
cout << "Format: " << argv[0] << " infile outfile" << endl;
return ERROR_INVALID_ARGS;
}
else { // Check for good files...
inFile.open(argv[1]);
outFile.open(argv[2], ofstream::trunc);
if (!inFile.good() || !outFile.good()) {
cout << "Invalid file(s)! (In: " << argv[1] << ", out: " << argv[2] << ")" << endl;
return ERROR_INVALID_FILE;
}
}
// So now our input and output files are initialized, we need to read the numbers in one by one and insert them into a doubly linked list.
for (string line; getline(inFile, line); ) {
// Convert the string into a stringstream
istringstream ss(line);
// And read it into a double. Exit with a bad format if we can't read a double.
double val;
if (!(ss >> val))
return ERROR_BAD_FORMAT;
// Now we create a new LinkedNode to store our value...
LinkedNode *temp = new LinkedNode;
temp->value = line;
temp->actualValue = val;
temp->prev = nullptr;
temp->next = nullptr;
//Counting first digit occurrences.
firstDigCount[((int)line[0] - 48)]++;
//Checking whether or not the value is a fraction or an integer
if(val == (long long)val)
integers++;
else
fractions++;
// And we set up the DigitCounts struct.
// Note: b + 30 is converting byte b into a char with a value of '0', '1', etc
// "count" is from the algorithms import, and operates on templated collections.
// Since string is a templated collection of chars, this works.
for (unsigned char b = 0; b < 10; b++) { // Iterate 0-9
temp->digitCounts[b] = count(line.begin(), line.end(), (char)(b + 48));
}
// Now we need to insert this in the current array. In a sorted fashion, mind you.
if (head == nullptr) // CASE: list is empty.
head = temp;
else { // CASE: List is not empty.
// Determine the node we need to insert before.
LinkedNode *before = head;
while (before->actualValue < val && before->next != nullptr)
before = before->next;
if (before->actualValue < val && before->next == nullptr) { // CASE: insert on tail
before->next = temp;
temp->prev = before;
}
else { // CASE: insert in the middle or at the head.
temp->next = before;
temp->prev = before->prev;
if (before == head)
head = temp;
if (before->prev != nullptr)
before->prev->next = temp;
before->prev = temp;
}
}
}
/* Start of file output */
//Printing ordered list to the outfile.
LinkedNode* holder = head;
while(holder != nullptr) {
outFile << holder->value << "\n";
//Need to specify C++11 for compiling
outFile << "[0-9]: ";
for(int k = 0; k < 9; k++)
outFile << holder->digitCounts[k] << ", ";
outFile << holder->digitCounts[9] << "\n";
holder = holder->next;
}
//Printing integer value occurrence
outFile << "There were " << integers << " integer values in the data set.\n";
//Printing fraction occurrence
outFile << "There were " << fractions << " fractional values in the data set.\n";
//Printing first digit occurrence distrivbution.
outFile << "Distribution of first digit [0-9]: \n";
for(int j = 0; j < 9; j++)
outFile << firstDigCount[j] << ", ";
outFile << firstDigCount[9] << "\n";
outFile.close();
/*End of file output*/
//Calling the menu function
menu(head);
//Cleanup
LinkedNode* node = head;
while(node != nullptr) {
if(node->next == nullptr) {
delete node;
node = nullptr;
} else {
node = node->next;
delete node->prev;
}
}
}
void menu(LinkedNode* head) {
cout << "Entering menu, type 'q' to quit.\n" << endl;
string input;
do {
cout << "Input a set of digits: " << endl;
cin >> input;
//Checking for quit.
if(input.compare("q") == 0 || input.compare("Q") == 0) {
break;
}
//Then here we can call your function or algorithm.
//I don't know if we need to check the input, we probably should.
int total=0;
LinkedNode* holder=head;
std::vector<int> mylist;
// int t;
// istringstream three(input);
// int fiVal;
// if (!(three >> fiVal))
// return ERROR_BAD_FORMAT;
int t=0;
// while(input[t]){
// if (isdigit(input[t])){
/* for(int j=0; j<input.length(); j++){
mylist.push_back(stoi(input[t]));
cout<<stoi(input[t]);
}
}
else{
cout<<"not a number";
}*/
mylist.reserve(input.size());
std::transform(std::begin(input), std::end(input),
std::back_inserter(mylist), [](char c){
return c-'0';
});
// t++;
// }
LinkedNode* holder2=head;
// int nuTotal=0;
while(holder2!=nullptr){
int nuTotal=0;
for(unsigned int u=0; u<mylist.size(); u++){
//int nuTotal=0;
for(int z=0; z<10; z++){
//holder2->digitCounts[z];
//int nuTotal=0;
if(mylist[u]==z){
//nuTotal+=holder2->digitCounts[z];
nuTotal+=holder2->digitCounts[z];
// cout<<"number of instances of "<< mylist[u]<<" for actual value "<< holder2->actualValue<<" is "<< holder2->digitCounts[z]<<endl;
// cout<<"nuTotal is "<<nuTotal<<endl;
}
if(nuTotal>total){
total=nuTotal;
// cout<<"total for"<< holder2->actualValue<<" is "<<total<<endl;
}}}
holder2=holder2->next;
}
std::list<string> digiStatNum;
//I can completely see how inefficient this is
LinkedNode* holder3=head;
while(holder3 != nullptr){
int nunuTotal=0;
for(int u=0; u<mylist.size(); u++){
for(int z=0; z<10; z++){
if(mylist[u]==z){
nunuTotal+=holder3->digitCounts[z];
}
}
}
if(nunuTotal==total){
digiStatNum.push_back(holder3->value);
}
holder3=holder3->next;
}
digiStatNum.sort();
for(std::list<string>::iterator z=digiStatNum.begin(); z!=digiStatNum.end(); z++){
cout<<' '<<*z<<endl;
}
} while(true);
}