[cpp]
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
class KanhaSum
{
public:
double sum;
KanhaSum()
{
sum = 0;
}
void reset()
{
sum = 0;
}
void add(double value)
{
sum += value;
}
double get_sum()
{
return sum;
}
};
class Median
{
public:
int n;
double median;
double sum;
public:
Median()
{
n = 0;
median = 0.0;
sum = 0.0;
}
void reset()
{
sum = 0;
}
void add(double value)
{
n++;
sum += value;
}
double get_median()
{
return sum/n;
}
};
class Vector
{
public:
int N;
double *DMVector;
public:
Vector(int n)
{
N = n;
DMVector = (double *)malloc(sizeof(double)*N);
}
~Vector()
{
free(DMVector);
}
void fill(double value)
{
for(int i=0; i<N; i++)
{
DMVector[i] = value;
}
}
double get(int i)
{
return DMVector[i];
}
void set(int i, double value)
{
DMVector[i] = value;
}
void add(const Vector &v)
{
for(int i=0; i<N; i++)
{
DMVector[i] += v.DMVector[i];
}
}
void sub(const Vector& v)
{
for(int i=0; i<N; i++)
{
DMVector[i] -= v.DMVector[i];
}
}
void mul(double k)
{
for(int i=0; i<N; i++)
{
DMVector[i] = DMVector[i]*k;
}
}
void inc_mul(const Vector &v, int k)
{
for(int i=0; i<N; i++)
{
DMVector[i] += k*v.DMVector[i];
}
}
double dot(const Vector &v)
{
double sum = 0.0;
for(int i=0; i<N; i++)
{
sum += DMVector[i]*v.DMVector[i];
}
return sum;
}
double square_norm()
{
double square = 0.0;
for(int i=0; i<N; i++)
{
square += DMVector[i]*DMVector[i];
}
return square;
}
double norm()
{
double ret = 0.0;
ret = sqrt(square_norm());
return ret;
}
double sum()
{
double sum = 0.0;
for(int i=0; i<N; i++)
{
sum += DMVector[i];
}
return sum;
}
double min_vector()
{
double minv = 0.0;
for(int i=0; i<N; i++)
{
if(DMVector[i] < minv)
minv = DMVector[i];
}
return minv;
}
double max_vector()
{
double maxv = 0.0;
for(int i=0; i<N; i++)
{
if(DMVector[i] > maxv)
maxv = DMVector[i];
}
return maxv;
}
};
double vector_mean(vector<Vector> veclist)
{
double sum = 0.0;
int c = 0;
for(unsigned int i=0; i<veclist.size(); i++)
{
for(int j=0; j<veclist[i].N; j++)
{
sum += veclist[i].get(j);
c ++;
}
}
return sum/c;
}
double vector_median(vector<Vector> veclist)
{
vector<double> vec;
for(unsigned int i=0; i<veclist.size(); i++)
{
for(int j=0; j<veclist[i].N; j++)
vec.push_back(veclist[i].get(j));
}
sort(vec.begin(), vec.end());
return vec.size()%2==0 ? vec[vec.size()-1/2] : vec[(vec.size())/2];
}
vector<Vector> read_data(string filename)
{
vector<Vector> vlist;
string tmps;
ifstream finput(filename.c_str(), ios::in);
//ifstream finput;
//finput.open(filename.c_str());
int i, size;
if( !finput )
{
cerr << "Open file Error!" << endl;
}
// check the dimension of vector
size = 1;
if( !finput.eof() )
{
getline(finput, tmps, '\n');
i = 0;
while( !(tmps[i] == '\0' || tmps[i] == '\n') )
{
if(tmps[i] == ' ')
size++;
i++;
}
}
finput.seekg( 0, ios::beg);
while( !finput.eof() )
{
Vector dmvector(size);
for ( i = 0; i < size; ++i )
{
finput >> tmps;
if ( tmps.length() == 0 )
return vlist;
dmvector.set( i, atof(tmps.c_str()));
}
vlist.push_back( dmvector );
}
finput.close();
return vlist;
}
void write_data(vector<Vector> veclist, string filename)
{
FILE *fp = NULL;
fp = fopen(filename.c_str(), "w");
for(unsigned int i=0; i<veclist.size(); i++)
{
for(int j=0; j<veclist[i].N; j++)
{
fprintf(fp, "%f ", veclist[i].get(j));
}
fprintf(fp, "%c", '\n');
}
}
int main()
{
//printf("%d\n", -1/2);//0
vector<Vector> veclist;
veclist = read_data("data.dm");
write_data(veclist, "outdata.dm");
system("pause");
return 0;
}
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
class KanhaSum
{
public:
double sum;
KanhaSum()
{
sum = 0;
}
void reset()
{
sum = 0;
}
void add(double value)
{
sum += value;
}
double get_sum()
{
return sum;
}
};
class Median
{
public:
int n;
double median;
double sum;
public:
Median()
{
n = 0;
median = 0.0;
sum = 0.0;
}
void reset()
{
sum = 0;
}
void add(double value)
{
n++;
sum += value;
}
double get_median()
{
return sum/n;
}
};
class Vector
{
public:
int N;
double *DMVector;
public:
Vector(int n)
{
N = n;
DMVector = (double *)malloc(sizeof(double)*N);
}
~Vector()
{
free(DMVector);
}
void fill(double value)
{
for(int i=0; i<N; i++)
{
DMVector[i] = value;
}
}
double get(int i)
{
return DMVector[i];
}
void set(int i, double value)
{
DMVector[i] = value;
}
void add(const Vector &v)
{
for(int i=0; i<N; i++)
{
DMVector[i] += v.DMVector[i];
}
}
void sub(const Vector& v)
{
for(int i=0; i<N; i++)
{
DMVector[i] -= v.DMVector[i];
}
}
void mul(double k)
{
for(int i=0; i<N; i++)
{
DMVector[i] = DMVector[i]*k;
}
}
void inc_mul(const Vector &v, int k)
{
for(int i=0; i<N; i++)
{
DMVector[i] += k*v.DMVector[i];
}
}
double dot(const Vector &v)
{
double sum = 0.0;
for(int i=0; i<N; i++)
{
sum += DMVector[i]*v.DMVector[i];
}
return sum;
}
double square_norm()
{
double square = 0.0;
for(int i=0; i<N; i++)
{
square += DMVector[i]*DMVector[i];
}
return square;
}
double norm()
{
double ret = 0.0;
ret = sqrt(square_norm());
return ret;
}
double sum()
{
double sum = 0.0;
for(int i=0; i<N; i++)
{
sum += DMVector[i];
}
return sum;
}
double min_vector()
{
double minv = 0.0;
for(int i=0; i<N; i++)
{
if(DMVector[i] < minv)
minv = DMVector[i];
}
return minv;
}
double max_vector()
{
double maxv = 0.0;
for(int i=0; i<N; i++)
{
if(DMVector[i] > maxv)
maxv = DMVector[i];
}
return maxv;
}
};
double vector_mean(vector<Vector> veclist)
{
double sum = 0.0;
int c = 0;
for(unsigned int i=0; i<veclist.size(); i++)
{
for(int j=0; j<veclist[i].N; j++)
{
sum += veclist[i].get(j);
c ++;
}
}
return sum/c;
}
double vector_median(vector<Vector> veclist)
{
vector<double> vec;
for(unsigned int i=0; i<veclist.size(); i++)
{
for(int j=0; j<veclist[i].N; j++)
vec.push_back(veclist[i].get(j));
}
sort(vec.begin(), vec.end());
return vec.size()%2==0 ? vec[vec.size()-1/2] : vec[(vec.size())/2];
}
vector<Vector> read_data(string filename)
{
vector<Vector> vlist;
string tmps;
ifstream finput(filename.c_str(), ios::in);
//ifstream finput;
//finput.open(filename.c_str());
int i, size;
if( !finput )
{
cerr << "Open file Error!" << endl;
}
// check the dimension of vector
size = 1;
if( !finput.eof() )
{
getline(finput, tmps, '\n');
i = 0;
while( !(tmps[i] == '\0' || tmps[i] == '\n') )
{
if(tmps[i] == ' ')
size++;
i++;
}
}
finput.seekg( 0, ios::beg);
while( !finput.eof() )
{
Vector dmvector(size);
for ( i = 0; i < size; ++i )
{
finput >> tmps;
if ( tmps.length() == 0 )
return vlist;
dmvector.set( i, atof(tmps.c_str()));
}
vlist.push_back( dmvector );
}
finput.close();
return vlist;
}
void write_data(vector<Vector> veclist, string filename)
{
FILE *fp = NULL;
fp = fopen(filename.c_str(), "w");
for(unsigned int i=0; i<veclist.size(); i++)
{
for(int j=0; j<veclist[i].N; j++)
{
fprintf(fp, "%f ", veclist[i].get(j));
}
fprintf(fp, "%c", '\n');
}
}
int main()
{
//printf("%d\n", -1/2);//0
vector<Vector> veclist;
veclist = read_data("data.dm");
write_data(veclist, "outdata.dm");
system("pause");
return 0;
}