-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse_csv.cpp
138 lines (130 loc) · 3.33 KB
/
parse_csv.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
#include <fstream>
<<<<<<< HEAD
using namespace std;
=======
#include <vector>
#include <iostream>
using namespace std;
int isNumber(string value) {
int result = 0;
vector<string> digits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
for (unsigned int i=0; i<value.size(); i++) {
bool passed = 0;
for (unsigned int j=0; j<digits.size(); j++) {
if (value.substr(i, 1)==digits[j]) {
if (result==0) result = 1;
passed = 1;
break;
} else if (result!=2) {
passed = 0;
}
}
if (passed==0) {
if (value.substr(i, 1)=="." && result!=2) {
result = 2;
} else {
result = 0;
break;
}
}
}
return result;
}
void printRow(string out_file, vector<vector<string>> data, int row) {
ofstream output_file;
output_file.open(out_file);
for (unsigned int i=0; i<data[row].size(); i++) {
output_file<<endl<<data[row][i];
}
output_file.close();
}
void printColumn(string out_file, vector<vector<string>> data, int column, string title) {
ofstream output_file;
output_file.open(out_file);
output_file<<title<<endl<<"-----------------------";
string test_value = data[0][column];
int result = isNumber(test_value);
vector<int> ints;
vector<float> floats;
for (unsigned int i=0; i<(data.size()-1); i++) {
string value = data[i][column];
if (result==1) ints.push_back(atoi(value.c_str()));
if (result==2) floats.push_back(stof(value.c_str()));
output_file<<endl<<value;
}
if (result!=0) {
float average;
output_file<<endl<<"-----------------------";
if (result==1) {
for (unsigned int i = 0; i<ints.size(); i++) {
average+=ints[i];
}
average/=ints.size();
} else {
for (unsigned int i = 0; i<floats.size(); i++) {
average+=floats[i];
}
average/=floats.size();
}
output_file<<endl<<"Average: "<<average;
}
output_file.close();
}
vector<string> parse_line(string line) {
vector<size_t> commas;
commas.push_back(0);
unsigned int place = 0;
while (place!=line.size()) {
size_t found = line.find(",", place);
if (found==string::npos) break;
commas.push_back(found+1);
place = found + 1;
}
vector<string> values;
for (unsigned int i = 0; i<commas.size(); i++) {
values.push_back(line.substr(commas[i], (commas[i+1]-(commas[i]+1))));
}
return values;
}
>>>>>>> 79756ff3e6c43937f24c9ffc902a09117d549e6d
int main() {
ifstream data_file;
data_file.open("NST_EST2014_ALLDATA.csv");
string line;
vector<vector<string>> region_data;
getline(data_file, line);
<<<<<<< HEAD
while (!data_file.eof()) {
getline(data_file, line);
vector<size_t> commas;
int place = 0;
while (place!=line.size()) {
size_t found = line.find(",", place);
if (found==string::npos) break;
commas.push_back(found);
place = found + 1;
}
vector<string> values;
for (int i = 0; i<commas.size(); i++) {
values.push_back(line.substr(commas[i], (commas[i+1]-commas[i])));
}
region_data.push_back(values);
}
data_file.close();
ofstream test_file;
test_file.open("test.txt");
for(int i=0, i<region_data[0]; i++) {
test_file<<region_data[0][i]<<endl;
}
test_file.close();
=======
vector<string> column_titles = parse_line(line);
while (!data_file.eof()) {
getline(data_file, line);
region_data.push_back(parse_line(line));
}
data_file.close();
printColumn("test.txt", region_data, 3, column_titles[3]);
>>>>>>> 79756ff3e6c43937f24c9ffc902a09117d549e6d
return 1;
}