- #1
madtraveller
- 28
- 0
Homework Statement
I have:
- 1 class to read a data series (see text file attached) (series.h and series.cpp)
- 1 class for a function (function.h and function.cpp)
I'm trying to create another class which has input from 2 above class (as constant references) and produce output which has the same length as data series. Therefore, I create an object based on series class to store the result. But the program keep saying that the syntax wasn't correct despite I tried all methods I knew
Plz help me. Any suggestion would be appreciated.
Homework Equations
The Attempt at a Solution
* Main
Code:
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;
#include "function.h"
#include "series.h"
#include "integral.h"
// Main program
int main ()
{
function function1;
series in1; // input data
series out1; // used to store result
integral integral1; // used input from series and function to generate output
// read input data
in1.read_data(string("data_test.txt"));
in1.print_data();
// test if function is correct
function1.give_value( 5000.0 );
// do integral from series and function to generate output
integral1.do_integral( in1, function, &out1 );
return 0;
}
* Series class:
- header file
Code:
#ifndef SERIES
#define SERIES
class series
{
private:
int length; // number of points
double* data;
struct Time_read // structure used to store time (format: xx/yy/zz)
{
unsigned int year, month, day;
};
vector<Time_read> time_vector; // vector to store date
public:
series();
bool read_data(const string &filename);
void print_data();
};
#endif
- series.cpp
Code:
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;
#include "series.h"
// constructor initializes each data member to zero
series::series()
{
length = 0;
data = NULL;
}
// Read data from file
bool series::read_data(const string &filename)
{
ifstream inputdata(filename.c_str());
// check if file couldn't be opened
if ( !inputdata )
{
cerr << "Error: file could not be opened" << endl;
return false;
}
string dummy_string; // temporary string used to read words
float temp;
// read and set length value
inputdata >> dummy_string >> dummy_string >> dummy_string >> temp;
length = temp;
cout << "Length= " << length << endl;
// read through "DATA" and "DATA" letters
inputdata >> dummy_string >> dummy_string;
data = new double [ length ]; // allocate space for an array
// Read date (xx/yy/zz)
Time_read time_input;
for (int i = 0; i < length; ++i)
{
inputdata >> setw(2) >> time_input.day; // read 2 char and store them in time_input.day
inputdata.ignore(1); // ignore 1 char. in this case "/" char
inputdata >> setw(2) >> time_input.month; // read 2 char and store in time_input.month
inputdata.ignore(1); // ignore 1 char
inputdata >> setw(2) >> time_input.year; // read 2 char and store in time_input.year
// store the reading result in time_vector
time_vector.push_back(time_input);
// read data
inputdata >> data[i];
}
return true;
}
void series::print_data()
{
cout << "--------------------------------" << endl;
for (int i = 0; i < length; ++i)
{
cout << time_vector[i].day << "/" << time_vector[i].month << "/" << time_vector[i].year << " ";
cout << setw(10) << data[i];
cout << endl; // new line at the end of each row
}
cout << "--------------------------------" << endl;
}
* function class
- header file
Code:
// prevent multiple inclusion of header file
#ifndef FUNCTION
#define FUNCTION
class function
{
private:
double trans;
public:
function();
double give_value( const double &time );
};
#endif
- function.cpp
Code:
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;
#include "function.h"
// constructor initializes each data member to zero
function::function()
{
trans = 0.0;
}
// Calculate function
double function::give_value( const double &time )
{
const double PI = 3.14159;
double trans = 0.0;
double diffuse = 0.0;
double c = 2.0;
double D = 10.0;
double dx = 10000.0;
trans = dx / ( 2.0 * sqrt( PI * D * pow(time, 3)));
diffuse = dx - c * time;
diffuse = exp ( - pow (diffuse, 2) / ( 4.0 * D * time ));
trans *= diffuse;
cout << setw(3) << time << setw(18) << trans << endl;
return trans;
}
* Integral class
- header file
Code:
// Declaration the integral class
// Member functions are defined in integral.cpp
// used input from series and function to generate output
// prevent multiple inclusion of header file
#ifndef INTEGRAL
#define INTEGRAL
class integral
{
private:
double* result;
public:
integral();
void do_integral( const series &input, const function &func, double* result )
};
#endif
- integral.cpp
Code:
// used input from series and function to generate output
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;
#include "integral.h"
integral::integral()
{
result = NULL;
}
// Function for calculating convoluted integral
void integral::do_integral( const series &input, const function &func, double* result )
{
const series &input;
const function &func;
series* result;
// Loop for calculating integral
for ( int i = 1; i < series.length; ++i ) // take length value from class series
{
for (int j = 0; j <= i; ++j )
{
result += series.data[i-j] * function.give_value[(3600*j)];
}
}
}
- sample data series
Number of points 18
DATE DATA
11/11/02 6.903
11/11/02 6.903
11/11/02 6.930
11/11/02 6.903
11/11/02 6.930
11/11/02 6.957
11/11/02 6.984
11/11/02 6.984
11/11/02 6.957
11/11/02 6.957
11/11/02 6.984
11/11/02 6.984
12/11/02 7.012
12/11/02 7.067
12/11/02 7.122
12/11/02 7.150
12/11/02 7.205
12/11/02 7.261
madtraveller