兩個文件,壹個叫Matrix.h, 壹個叫Matrix.cpp
//Matrix.h
#ifndef _MATRIX_H_
#define _MATRIX_H_
#include <math.h>
#include <stdlib.h>
#include <float.h>
#include<iostream.h>
class matrixobj
{
float matrix[3][3];
float cofactor(int i,int j);
public:
void displayMatrix();
void readMatrix();
friend matrixobj operator+(matrixobj &x, matrixobj &y);
friend matrixobj operator-(matrixobj &x, matrixobj &y);
friend matrixobj operator*(matrixobj &x, matrixobj &y);
friend matrixobj operator/(matrixobj &x, float d);
float matrixDeterminent();
matrixobj matrixInverse();
matrixobj matrixTranspose();
};
void matrixobj::displayMatrix()
{
int i,j;
for (i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
cout<<matrix[i][j];
cout<<"\n"<<endl;
}
}
void matrixobj::readMatrix()
{
int i,j;
float x=0;
for (i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
{
cin>>x;
matrix[i][j] = x;
}
}
float matrixobj::cofactor(int i,int j)
{
float cofactorValue;
int a1,b1,c1,d1,a2,b2,c2,d2;
a1 = (i + 1) % 3;
b1 = (j + 1) % 3;
c1 = (i + 2) % 3;
d1 = (j + 2) % 3;
a2 = (i + 2) % 3;
b2 = (j + 1) % 3;
c2 = (i + 1) % 3;
d2 = (j + 2) % 3;
cofactorValue = matrix[a1][b1] * matrix[c1][d1] - matrix[a2][b2] * matrix[c2][d2];
return cofactorValue;
}
float matrixobj::matrixDeterminent()
{
float det;
det = matrix[0][0] * cofactor(0,0);
det += matrix[0][1] * cofactor(0,1);
det += matrix[0][2] * cofactor(0,2);
return det;
}
matrixobj matrixobj::matrixInverse()
{
float det;
int i,j;
matrixobj z,y;
det = matrixDeterminent();
for (i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
z.matrix[i][j] = cofactor(i,j);
z = z.matrixTranspose();
if (det == (float) 0)
{
exit(0);
}
y = z / det;
return y;
}
matrixobj matrixobj::matrixTranspose()
{
int i,j;
matrixobj z;
for (i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
z.matrix[j][i] = matrix[i][j];
return z;
}
matrixobj operator+(matrixobj &x, matrixobj &y)
{
matrixobj z;
int i,j;
for (i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
z.matrix[i][j] = x.matrix[i][j] + y.matrix[i][j];
return z;
}
matrixobj operator-(matrixobj &x, matrixobj &y)
{
matrixobj z;
int i,j;
for (i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
z.matrix[i][j] = x.matrix[i][j] - y.matrix[i][j];
return z;
}
matrixobj operator*(matrixobj &x, matrixobj &y)
{
matrixobj z;
int i,j,k;
for (i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
z.matrix[i][j] = 0;
for (i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
for(k = 0; k < 3; k++)
z.matrix[i][j] += x.matrix[i][k] * y.matrix[k][j];
return z;
}
matrixobj operator/(matrixobj &x, float d)
{
int i,j;
matrixobj z;
for (i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
z.matrix[i][j] = x.matrix[i][j] / d;
cout<<"\n"<<endl;
return z;
}
#endif
// Matrix.cpp
#include <stdio.h>
#include <conio.h>
#include "matrix.h"
#include <float.h>
#include<iostream.h>
void main()
{
matrixobj a,b,c;
float x;
cout<<"Enter a 3 by 3 matrix (A): \n";
a.readMatrix();
cout<<"Enter a 3 by 3 matrix (B): \n";
b.readMatrix();
cout<<"Matrices (A) + (B) is : \n";
c = a + b;
c.displayMatrix();
getch();
cout<<"Matrices (A) - (B) is : \n";
c = a - b;
c.displayMatrix();
getch();
cout<<"Matrices (A) * (B) is : \n";
c = a * b;
c.displayMatrix();
getch();
x = a.matrixDeterminent();
getch();
c = a.matrixInverse();
c.displayMatrix();
getch();
}