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
|
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
#include<string.h>
ofstream output_file("out.5", ios::out);
class MATRIX: public PROGRAM_BANK{
public: // public interfaces for this class
MATRIX (int, int); // constructor;
// example: MATRIX X(d1,d2);
// creates a matrix object with
// private variables dim1 = d1 and dim2 = d2
// and initializes each element to 0;
MATRIX(int, int, char *);
// constructor;
// example: MATRIX X(d1,d2,"in_name");
// read the elements of the matrix
// from a given file in_name; d1 and d2 are
// the two dimensions of the matrix;
private:
int dim1, dim2; // dimensions of the matrix;
// if dim2 is 1, a vector (no need for special
// handling of vectors; everything should work
// for both matrices and vectors)
double A[20][20]; // 2 dimensional array for the matrix elements;
char in_file[11]; // hold the input file name
};
// constructor
MATRIX::MATRIX(int d1, int d2)
:PROGRAM_BANK(d1) // use the inheritance
{
int i, j;
dim1 = d1;
dim2 = d2;
if(d1 < 0 || d1 > 20 || d2< 0 || d2 >20)
{
exit(1);
}
for (i= 0; i<dim1; i++)
{
for (j= 0; j<dim2; j++)
{
A[i][j] = 0;
}
}
}
// constructor
MATRIX::MATRIX(int d1, int d2, char * file_name)
:PROGRAM_BANK(d1, file_name) // use the inheritance
{
int i, j;
output_file << "+++ P5 START +++++++++++++++++++++++++++++++++++++++++" << endl;
if(d1 < 0 || d1 > 20 || d2< 0 || d2 >20)
{
output_file <<"+++ P5_OUTPUT >>> INPUT ERROR"
<< endl
<<"+++ P5_OUTPUT >>> UNABLE TO CREATE MATRIX"
<< endl
<<"+++ P5_OUTPUT >>> EXITING PROGRAM"
<< endl
<< "+++ P5 END +++++++++++++++++++++++++++++++++++++++++++"
<< endl;
exit(1);
}
else
{
dim1 = d1;
dim2 = d2;
// copy the input file name to private variable in_file
strcpy(in_file, file_name);
//define the input_filein this current block
ifstream input_file(in_file, ios::in);
for (i =0; i<dim1; i++)
{
for(j = 0; j<dim2; j++)
{
input_file >> A[i][j];
}
}
output_file <<"+++ P5_OUTPUT >>> CREATED A " << dim1 <<" X " << dim2
<<" MATRIX FROM " << in_file << endl;
}
output_file << "+++ P5 END +++++++++++++++++++++++++++++++++++++++++++" << endl;
}
|