-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileReading.cpp
289 lines (247 loc) · 5.98 KB
/
FileReading.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//////////////////////////////////////////////////////////////
//
// Your #includes here; make sure you are allowed them ...
//
#include <fstream>
using namespace std;
//////////////////////////////////////////////////////////////
//
// #includes and function declarations; do not remove
//
#ifndef MARMOSET_TESTING
#include <iostream>
using namespace std;
#endif
//////////////////////////////////////////////////////////////
//
// Function declarations; do not remove
// Replace "..." in bisectionHelper() with any parameter(s)
// you need to ensure termination when precision and
// minIntervalSize are too tightly specified.
//
int parseline (char line[], int& value, int& grade);
int histogram(const char filename[], int histogram[10],
const int minAcceptableID, const int maxAcceptableID,
int*& rejects);
//////////////////////////////////////////////////////////////
//
// Your code here ...
//
// Return -1 if you cannot compute the histogram for any
// reason;
// Return 0 if the histogram is correct and there are no
// rejections;
// Return the number of rejects if there are rejected
// records from the file
//
int parseline (char line[], int& value, int& grade) // value is id
{
value = 0;
grade = 0;
int i = 0;
int x = 0;
enum state {START, WS1, STUDENTID, WS2, COMMA, WS3, GRADE, WS4, EOL};
state currentstate = START;
while (currentstate != EOL)
{
x = (line[i] - '0');
switch (currentstate)
{
case START:
if (line[i] == ' ' || line[i] == '\t')
currentstate = WS1;
if (line[i] >= '0' && line[i] <= '9')
{
value = x;
currentstate = STUDENTID;
}
break;
case STUDENTID:
if (line[i] >= '0' && line[i] <= '9')
value = value*10 + x;
if (line[i] == ' ' || line[i] == '\t')
currentstate = WS2;
if (line[i] == ',')
currentstate = COMMA;
//cout << "v: " << value << endl;
break;
case WS1:
if (line[i] == ' ' || line[i] == '\t')
currentstate = WS1;
if (line[i] >= '0' && line[i] <= '9')
{
value = value*10 + x;
currentstate = STUDENTID;
}
break;
case WS2:
if (line[i] == ' ' || line[i] == '\t')
currentstate = WS2;
if (line[i] == ',')
currentstate = COMMA;
break;
case COMMA:
if (line[i] == ' ' || line[i] == '\t')
currentstate = WS3;
if (line[i] >= '0' && line[i] <= '9')
{
grade = x;
currentstate = GRADE;
}
break;
case WS3:
if (line[i] == ' ' || line[i] == '\t')
currentstate = WS3;
if (line[i] >= '0' && line[i] <= '9')
{
grade = x;
currentstate = GRADE;
}
break;
case GRADE:
if (line[i] == 0)
currentstate = EOL;
if (line[i] == ' ' || line[i] == '\t')
currentstate = WS4;
if (line[i] >= '0' && line[i] <= '9')
{
grade = grade*10 + x;
currentstate = GRADE;
}
break;
case WS4:
if (line[i] == ' ' || line[i] == '\t')
currentstate = WS4;
if (line[i] == 0)
currentstate = EOL;
break;
case EOL:
currentstate = EOL;
break;
}
i++;
}
}
int histogram(const char filename[], int histogram[10],
const int minAcceptableID, const int maxAcceptableID,
int*& rejects) {
if(minAcceptableID>maxAcceptableID)
{
return -1;
}
if(histogram == NULL)
{
return -1;
}
for(int i = 0 ; i < 10 ; i++)
{
histogram[i] = 0;
}
bool check = true;
int index_rejects = 0;
rejects = new int[100];
const int maxLineLength = 100;
char line[maxLineLength];
ifstream infile;
infile.open(filename);
if (!infile.is_open())
return -1;
bool done = false;
int fileLineNumber = 0;
int i = 0;
int id = 0;
int grade = 0;
int count = 0;
int x = 0;
while (!done)
{
++fileLineNumber;
if (!infile.getline(line, maxLineLength))
{
if (infile.eof())
{
done = true;
}
else
return -1;
}
parseline(line, id, grade);
//out << "grade: " << grade << " id: " << id << endl;
if(line[0] != 0)
{
if(id < minAcceptableID || id > maxAcceptableID || grade < 0 || grade > 100 || check == false)
{
rejects[index_rejects] = fileLineNumber;
index_rejects++;
//fileLineNumber;
//increment the index
//increment the number of lines rejected
count++;
x = -1;
}
if (x == 0)
histogram[grade/10] += 1;
x = 0;
}
//out << "count: " << count << endl;
//ut << "student id: " << id << " grade: " << grade << endl;
/*
if (grade == 100)
{
histogram[9] += 1;
}
else{
histogram[grade/10] += 1;
} */
//doHistogramStuff();
i++;
}
infile.close();
return count;
if (count ==0)
return 0;
else if (count >10)
return -1;
else
return count;
}
//////////////////////////////////////////////////////////////
//
// Test Driver
//
// Do not remove the #ifndef and its associated #endif
// This is required so that when you submit your code,
// this test driver will be ignored by Marmoset
//
#ifndef MARMOSET_TESTING
#define isNaN(X) (X != X) // NaN is the only float that is not equal to itself
int main(const int argc, const char* const argv[]) {
// Some test driver code here ....
if (argc < 2) {
}
const int numBuckets = 10;
const int bucketRange = 100/numBuckets;
int buckets[numBuckets];
int* rejectedRecords;
int retCode = histogram(argv[1], buckets, 22200000, 22299999, rejectedRecords);
if (retCode < 0) {
cout << "Unable to compute histogram." << endl;
}
else {
for (int i = 0; i < numBuckets; ++i)
if (i != numBuckets-1)
cout << "Number [" << i*bucketRange << "," << (((i+1)*bucketRange)-1) << "]: " << buckets[i] << endl;
else
cout << "Number [" << i*bucketRange << "," << (((i+1)*bucketRange)) << "]: " << buckets[i] << endl;
if (retCode > 0) {
cout << "Number of rejected records: " << retCode << endl;
cout << "Rejected records are:";
for (int i = 0; i < retCode; ++i)
cout << " " << rejectedRecords[i];
cout << endl;
delete[] rejectedRecords;
}
}
return 0;
}
#endif