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
|
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
struct Stats
{
double xmin, xmax, ymin, ymax, zmin, zmax;
double sumx, sumy, sumz;
int num = 0;
Stats( double x, double y, double z );
Stats( const string &csvfile );
};
Stats::Stats( double x, double y, double z )
{
xmin = xmax = sumx = x;
ymin = ymax = sumy = y;
zmin = zmax = sumz = z;
num = 1;
}
Stats::Stats( const string &csvfile )
{
ifstream in( csvfile );
bool first = true;
char comma;
for ( double x, y, z; in >> x >> comma >> y >> comma >> z; )
{
if ( first )
{
*this = Stats( x, y, z );
}
else
{
xmin = min( x, xmin );
xmax = max( x, xmax );
ymin = min( y, ymin );
ymax = max( y, ymax );
zmin = min( z, zmin );
zmax = max( z, zmax );
sumx += x;
sumy += y;
sumz += z;
num++;
}
first = false;
}
}
int main()
{
Stats S( "test.csv" );
if ( S.num )
{
cout << "Coordinates read: " << S.num << '\n';
cout << "Bounds:\n"
<< "x: " << S.xmin << " - " << S.xmax << '\n'
<< "y: " << S.ymin << " - " << S.ymax << '\n'
<< "z: " << S.zmin << " - " << S.zmax << '\n';
cout << "Averages:\n"
<< "x: " << S.sumx / S.num << '\n'
<< "y: " << S.sumy / S.num << '\n'
<< "z: " << S.sumz / S.num << '\n';
}
else
{
cerr << "No meaningful data read\n";
}
}
| |