精華區beta NAOE-87 關於我們 聯絡資訊
<matrix.h> #include<fstream.h> #ifndef _DB_MATRIX_ #define _DB_MATRIX_ class Matrix { protected: unsigned m,n; double * mpt; public: Matrix(); //Constructor. Matrix(unsigned m_in, unsigned n_in); //Constructor. Giving numbers of row and column. //It will initial all elements, i.e. all elements=0. ~Matrix(); //Destructor. Free memory. Matrix(const Matrix & m2); //Copy constructor. double show_elem(unsigned i, unsigned j) const; //Show any one element's value. Giving subindex of one //element. double change_elem(unsigned i, unsigned j, double val); //Change any one element's value. Giving subindex of //one element. void set_all_elem(void); //The function will ask user the value of all elements //one by one. void set_all_elem(char * sourcefile); //The function will get all elements' value from "sourcefile". void exchange_row(unsigned r1, unsigned r2); //Exchanging row "r1" and row "r2". void exchange_column(unsigned c1, unsigned c2); //Exchanging column "c1" and column "c2". Matrix operator*(const Matrix & m2) const; //Multipling matrix with another matrix. Matrix operator*(double k) const; //Multipling matrix with constant number. friend Matrix operator*(double k, const Matrix & m2); //Multipling constant number with matrix. Matrix operator+(const Matrix & m2) const; //Adding matrix with another matrix. Matrix operator-(const Matrix & m2) const; //Decreasing matrix with another matrix. Matrix operator-() const; //Negative operator. Matrix operator=(const Matrix & m2); //Assign operator. void row_process(unsigned r1, double k1, unsigned r2, double k2); //Adding or decreasing row by row. Multipling "r1" by //a constant number "k1" and multipling "r2" by a //"k2" constant than adding "r1*k1+r2*k2". void row_process(unsigned r, double k); //Multipling k with all of elements in row "r". void column_process(unsigned c1, double k1, unsigned c2, double k2); //Adding or decreasing column by column. Multipling "c1" by //a constant number "k1" and multipling "c2" by a //"k2" constant than adding "c1*k1+c2*k2". void column_process(unsigned c, double k); //Multipling k with all of elements in column "c". Matrix transpose(void); //Transposing a matrix. It will return a matrix, //but don't do anything about source matrix. double determine(void) const; //Determining the value of matrix. //The source matrix should be a square matrix. void show_all(void) const; //Show all elements in matrix on display. void show_all(char * outfilename) const; //Show all elements in matrix on an file. friend ostream & operator<<(ostream & os, Matrix & ma); //Overload "<<" operator. It can use with ostream object, //e.g. cout << Matrix; friend fstream & operator<<(fstream & fs, Matrix & ma); //Overload "<<" operator. It can use with fstream object. //e.g. fout << Matrix; }; #endif <gauss1.h> #include"d:\tcplus\hw\ma\matrix.h" #ifndef _GAUSS_METHOD_ #define _GAUSS_METHOD_ class Gauss:public Matrix { private: double * solution; int index_of_eliminate; public: Gauss(); //Constructor. Gauss(unsigned i, unsigned j); //Constructor. Giving i,j to assing a matrix's m and n. ~Gauss(); //Destructor. void eliminate(void); //Eliminating elements in a matrix. When not using this //function, "void solve(void)" function can not act. void pivot(unsigned i); //Pivoting matrix. int is_eliminate(void) const; //If the matrix already eliminate, return 1. //Otherwise, return 0. void solve(void); //Solve all values in a matrix(i.e. "equation"). void show_solution(void) const; //Displaying all solution. }; #endif <gauss1.cpp> #include<math.h> #include<process.h> #include"d:\tcplus\hw\ma\gauss1.h" Gauss::Gauss():Matrix() { solution=NULL; index_of_eliminate=0; } Gauss::Gauss(unsigned i, unsigned j):Matrix(i,j) { solution=NULL; index_of_eliminate=0; } Gauss::~Gauss() { delete [] solution; } void Gauss::eliminate(void) { if ( is_eliminate() != 0 ) cerr << "Elimination is already.\n"; else { unsigned i,k,pivot_index; double temp; char ch; for (i=1; i<=m-1; i++) { pivot(i); while( !cin.get(ch) ); for (k=i+1; k<=m; k++) { if ( show_elem(i,i)*show_elem(k,i) > 0 ) temp=-show_elem(k,i)/show_elem(i,i); else if ( show_elem(k,i) < 0 ) temp=fabs( show_elem(k,i)/show_elem(i,i) ); else temp=-show_elem(k,i)/show_elem(i,i); row_process(i, temp, k, 1); } } index_of_eliminate=1; } } void Gauss::pivot(unsigned i) { int j,pivot_index,pivot_max_elem; pivot_index=i; pivot_max_elem=fabs(show_elem(i,i)); for (j=i+1; j<=m-1; j++) if ( fabs(show_elem(j,i)) > pivot_max_elem ) { pivot_index=j; pivot_max_elem=fabs(show_elem(j,i)); } if ( pivot_index != i ) exchange_row(i,pivot_index); } int Gauss::is_eliminate(void) const { return index_of_eliminate; } void Gauss::solve(void) { if ( is_eliminate() == 0 ) { cerr << "The matrix is not eliminated. Program terminate.\n"; exit(1); } solution=new double [m]; unsigned i,j,k; double temp_sum; *(solution+m-1)=show_elem(m,n)/show_elem(m,n-1); for (i=m-1; i>0; i--) { temp_sum=0; for (j=n-1,k=m-1; k>=i; j--,k--) temp_sum+=show_elem(i,j) * (*(solution+k)); *(solution+i-1) = (show_elem(i,n)-temp_sum) / show_elem(i,i); } } void Gauss::show_solution(void) const { unsigned i; cout.precision(4); cout.setf(ios::showpoint); cout.setf(ios::fixed, ios::floatfield); for (i=1; i<=m; i++) cout << 'x' << i << '=' << *(solution+i-1) << '\n'; } <main_2_1.cpp> #include<iostream.h> #include<math.h> #include<conio.h> #include"d:\tcplus\hw\ma\gauss1.h" int main(void) { clrscr(); Gauss temp(3,4); temp.set_all_elem("d:\\tcplus\\hw\\ma\\gauss1.dat"); temp.show_all(); temp.eliminate(); temp.solve(); temp.show_solution(); return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.twbbs.org) ◆ From: t195-250.dialup.seed.net.tw