Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
157 lines (141 loc) · 4.61 KB
/
Copy pathmain.cpp
File metadata and controls
157 lines (141 loc) · 4.61 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
#include <algorithm>
#include <cmath>
#include <exception>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <sstream>
#include <string>
#include <vector>
static std::vector<std::string> split_commas(const std::string &line) {
std::vector<std::string> out;
std::string cur;
std::istringstream ss(line);
while (std::getline(ss, cur, ',')) {
// trim spaces
size_t start = cur.find_first_not_of(" \t\r\n");
size_t end = cur.find_last_not_of(" \t\r\n");
if (start == std::string::npos) {
out.emplace_back("");
} else {
out.emplace_back(cur.substr(start, end - start + 1));
}
}
// if line ends with comma, std::getline drops last empty field; compensate
if (!line.empty() && line.back() == ',') out.emplace_back("");
return out;
}
struct Stats {
std::size_t count = 0;
double mean = std::numeric_limits<double>::quiet_NaN();
double median = std::numeric_limits<double>::quiet_NaN();
double min = std::numeric_limits<double>::quiet_NaN();
double max = std::numeric_limits<double>::quiet_NaN();
double stddev = std::numeric_limits<double>::quiet_NaN();
std::size_t invalid = 0;
};
static Stats compute_stats(const std::vector<double>& v) {
Stats s;
s.count = v.size();
if (v.empty()) return s;
double sum = 0.0;
s.min = std::numeric_limits<double>::infinity();
s.max = -std::numeric_limits<double>::infinity();
for (double x : v) {
sum += x;
if (x < s.min) s.min = x;
if (x > s.max) s.max = x;
}
s.mean = sum / static_cast<double>(v.size());
std::vector<double> w = v;
std::sort(w.begin(), w.end());
if (w.size() % 2 == 1) {
s.median = w[w.size()/2];
} else {
s.median = 0.5 * (w[w.size()/2 - 1] + w[w.size()/2]);
}
// population standard deviation
double sq = 0.0;
for (double x : v) {
double d = x - s.mean;
sq += d * d;
}
s.stddev = std::sqrt(sq / static_cast<double>(v.size()));
return s;
}
int main(int argc, char** argv) {
std::string csv_path = "data/sample.csv";
std::string col_name = "value";
if (argc >= 2) csv_path = argv[1];
if (argc >= 3) col_name = argv[2];
std::ifstream fin(csv_path);
if (!fin) {
std::cerr << "Error: cannot open file: " << csv_path << "\n";
std::cerr << "Usage: " << argv[0] << " <csv_path> [column_name]\n";
return 1;
}
std::string header;
if (!std::getline(fin, header)) {
std::cerr << "Error: empty file or cannot read header\n";
return 1;
}
auto headers = split_commas(header);
int target_idx = -1;
for (int i = 0; i < static_cast<int>(headers.size()); ++i) {
if (headers[i] == col_name) {
target_idx = i;
break;
}
}
if (target_idx < 0) {
std::cerr << "Error: column '" << col_name << "' not found in header.\n";
std::cerr << "Columns found:";
for (auto& h : headers) std::cerr << " [" << h << "]";
std::cerr << "\n";
return 1;
}
std::vector<double> values;
std::size_t line_no = 1; // header is line 1
std::size_t invalid = 0;
std::string line;
while (std::getline(fin, line)) {
++line_no;
if (line.size() == 0) continue; // skip blank
auto cols = split_commas(line);
if (target_idx >= static_cast<int>(cols.size())) {
++invalid;
continue;
}
const std::string& cell = cols[target_idx];
try {
size_t pos = 0;
double val = std::stod(cell, &pos);
// if trailing junk exists, count invalid
if (pos != cell.size()) {
++invalid;
continue;
}
values.push_back(val);
} catch (const std::exception&) {
++invalid;
}
}
Stats s = compute_stats(values);
s.invalid = invalid;
std::cout << "CSV: " << csv_path << "\n";
std::cout << "Column: " << col_name << "\n\n";
std::cout << std::fixed << std::setprecision(4);
std::cout << "Count (valid): " << s.count << "\n";
std::cout << "Count (invalid rows): " << s.invalid << "\n";
if (s.count > 0) {
std::cout << "Min: " << s.min << "\n";
std::cout << "Max: " << s.max << "\n";
std::cout << "Mean: " << s.mean << "\n";
std::cout << "Median:" << s.median << "\n";
std::cout << "StdDev:" << s.stddev << "\n";
} else {
std::cout << "No valid numeric data found for column '" << col_name << "'.\n";
}
return 0;
}