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
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class houseData {
public:
//id,value,bedrooms,bathrooms,sqft,yr_built,zipcode
long int id = 0;
int price = 0;
int bedrooms = 0;
int bathrooms = 0;
int sqft = 0;
int yr_built = 0;
int zipcode = 0;
houseData *nextZip = nullptr;
void printShort();
void printRecord();
};
void ProcHeader(ifstream &file);
houseData* ReadRecord(ifstream &file);
houseData* Scanlist(houseData* head, int Zipcode);
void DelRecord(houseData *record);
void bsort(int list[], int length);
void swap (int &a, int &b);
void printAllHousesByZip(houseData* head);
int main(int argc, char* argv[]) {
houseData* head = nullptr;
houseData* tail = nullptr;
ifstream fin;
if(argc > 1) {
fin.open(argv[1]); // Small file to test with
} else {
//fin.open("./house-info-v4-short.csv"); // Small file to test with
fin.open("./house-info-v4.csv"); // Use this file for final
}
if(fin.is_open()) {
ProcHeader(fin); // Just dumps header
int numRec = 0;
while(!fin.eof()) {
houseData* tempHouse = ReadRecord(fin);
if(tempHouse != nullptr) {
if (head == nullptr) { //Make head of list if one does not exist
head=tempHouse;
tail=tempHouse;
tempHouse=nullptr;
} else {
tail->nextZip=tempHouse;
tail=tempHouse;
}
//tempHouse->printRecord();
//tempHouse->printShort();
//initHouseData[numRec] = tempHouse; // DO NOT store data like this!
numRec++;
if (numRec%1000==0 && numRec!=0) {
cout << "Still working...num records processed: " << numRec << endl;
}
}
}
houseData* currentZipFind = head;
int zipSort[1000]; bool newZip=true; int currentUnique=0;
for (int i=0; i<999; i++) {zipSort[i]=0;}
while(currentZipFind!=nullptr) {
for (int i=0; i<999; i++) {
//Find a new zipcode to average
if (currentZipFind->zipcode==zipSort[i]) {
//Only find unique ZIPs
newZip=false;
break;
}
newZip=true;
}
if ( (newZip) ) {
zipSort[currentUnique]=currentZipFind->zipcode;
//cout << zipSort[currentUnique] << endl;
currentUnique++;
}
currentZipFind = currentZipFind->nextZip;
}
bsort(zipSort, 1000); //Sort array of zipcodes by bubblesort.
houseData* findAvg = head;
int costPerZip[1000]={0}, totalPerZip[1000]={0};
//Set all of the values to zero
for (int i=0; i<999; i++) {costPerZip[i]=0; totalPerZip[i]=0;}
while (findAvg!=nullptr) {
for(int j=0; j<999; j++) {
if (zipSort[j]==findAvg->zipcode) {
costPerZip[j]+=findAvg->price;
totalPerZip[j]++;
break; //Break for loop
}
} //end for
findAvg=findAvg->nextZip;
} //end while
for (int k=0; k<999; k++) {
if (zipSort[k]!=0) {
cout << zipSort[k] << ":average price=" << costPerZip[k]/totalPerZip[k] << endl;
}
}
//printAllHousesByZip(head);
//This loop will also have to change in your
// final program (see HINT 1).
DelRecord(head);
fin.close();
} else {
cout << "File missing!" << endl;
return 1;
}
return 0;
}
void ProcHeader(ifstream &file) {
int endOfHeader = 0;
char curChar;
while(!endOfHeader) {
curChar = file.get();
if (curChar == '\n') {
//End of line found
endOfHeader = 1;
}
}
}
//This function is the algorithm.
void bsort(int list[], int length) {
for (int i=0; i<length; i++) {
for (int j=0; j<length-1; j++) {
if (list[j]>list[j+1]) {
//This is so the bubble can happen
swap(list[j], list[j+1]);
}
}
}
}
//This function simply swaps two values so the bubbling can commence
void swap (int &a, int &b) {
int temp;
temp=a; a=b; b=temp;
}
houseData* ReadRecord(ifstream &file) {
int b = 0;
int endOfHeader = 0;
int c = 0;
long int tempData[7] = {0};
houseData* localHouseData = new houseData();
string tempString = " ";
while(!endOfHeader) {
char curChar;
//Get the next character in file
curChar = file.get();
//Check what the character is
if (curChar == EOF) {
return nullptr;
} else if (curChar == '\n') {
//Final symbol of row found: put in array and end loop
tempString.append(1,'\0');
tempData[c++] = stol(tempString);
endOfHeader = 1;
} else if (curChar == ',') {
//End of current symbol found: put in array
tempString.append(1,'\0');
tempData[c++] = stol(tempString);
tempString.clear();
} else {
// Append to array
tempString.append(1,curChar);
b++;
}
}
localHouseData->id = tempData[0];
localHouseData->price = tempData[1];
localHouseData->bedrooms = tempData[2];
localHouseData->bathrooms = tempData[3];
localHouseData->sqft = tempData[4];
localHouseData->yr_built = tempData[5];
localHouseData->zipcode = tempData[6];
return localHouseData;
}
houseData* Scanlist(houseData* head, int Zipcode) {
houseData* previous; houseData* current;
previous=head;
current=head->nextZip;
while ((current!=NULL) && !(current->zipcode >= Zipcode)) {
previous=current;
current=current->nextZip;
}
return previous;
}
void houseData::printRecord() {
cout << "Printing House Record:" << endl;
cout << "ID:" << id << endl;
cout << "Price:" << price << endl;
cout << "Bedrooms:" << bedrooms << endl;
cout << "Bathrooms:" << bathrooms << endl;
cout << "Square Feet:" << sqft << endl;
cout << "Year Built:" << yr_built << endl;
cout << "Zipcode:" << zipcode << endl;
}
void DelRecord(houseData *record) {
houseData* next;
while (record!=NULL) {
next=record->nextZip;
delete record;
record = next;
}
}
void houseData::printShort() {
cout << id << "," << zipcode << endl;
}
void printAllHousesByZip(houseData* head){
cout << endl << "Printing List of All Houses by Zip:" << endl;
houseData* current = head;
while(current != nullptr) {
current->printShort();
current = current->nextZip;
}
}
|