真不知怎么说,写了挺久的。
写不写文档以后再说了。
我自己贴出代码来。想必也很多多余不之处。
基本上就是:
自定义整型=》 自定义浮点数=》 自定义的整合数型(继承了以上的两个类)=》自定义向量类 =》 矩阵类
#include<iostream.h>
#include<stdlib.h>
#include<math.h>
class integer{
public:
integer(int inter0=0){inter=inter0;};
integer operator +(integer int2){
integer temp;
temp.inter=inter+int2.inter;
return temp;
};
integer operator - (integer int2){
integer temp;
temp.inter=inter-int2.inter;
return temp;
};
integer operator *(integer int2){
integer temp;
temp.inter=inter*int2.inter;
return temp;
};
integer operator /(integer int2){
integer temp;
temp.inter=inter/int2.inter;
return temp;
};
void print(){
if(inter< -32768||inter>32767) cout<<"/t overflow, now stop this operate."<<endl;
else cout<<inter<<endl;
};
protected:
int inter;
};
class Real{
public:
Real(double doub0=0.0){doub=doub0;};
Real operator +(Real doub2){
Real temp;
temp.doub=doub+doub2.doub;
return temp;
};
Real operator - (Real doub2){
Real temp;
temp.doub=doub-doub2.doub;
return temp;
};
Real operator *(Real doub2){
Real temp;
temp.doub=doub*doub2.doub;
return temp;
};
Real operator /(Real doub2){
Real temp;
temp.doub=doub/doub2.doub;
return temp;
};
void print(){
if(doub< -32768.0||doub>32767.0) cout<<"/t overflow, now stop this operate."<<endl;
else cout<<doub<<endl;
};
protected:
double doub;
};
class IntReal: public integer,public Real{
public:
IntReal(int i=0,double d=0.0):integer(i),Real(d){};
IntReal operator +(IntReal IR){
IntReal temp;
temp.doub=doub+IR.doub;
temp.inter=inter+IR.inter;
return temp;
};
IntReal operator -(IntReal IR){
IntReal temp;
temp.doub=doub-IR.doub;
temp.inter=inter-IR.inter;
return temp;
};
IntReal operator *(IntReal IR){
IntReal temp;
temp.doub=doub*IR.doub;
temp.inter=inter*IR.inter;
return temp;
};
IntReal operator /(IntReal IR){
IntReal temp;
temp.doub=doub/IR.doub;
temp.inter=inter/IR.inter;
return temp;
};
IntReal operator =(IntReal IR){
inter=IR.inter;
doub=IR.doub;
return *this;
};
friend ostream& operator<<(ostream& output,IntReal& R){
cout<<R.doub<<" "; //测试时可用cout<<R.inter<<"/"<<R.doub<<" ";
return output;
};
friend istream& operator>>(istream& input,IntReal& R){ //第一次写成了 iostream,错误啊
double org_num;
cin>>org_num;
IntReal Temp(org_num,org_num);
R=Temp;
return input;
};
};
class Vector
{
public:
Vector(int=1); //预定义长度为 1 的向量
Vector(IntReal*,int);
Vector(Vector&);
~Vector();
IntReal&operator[](int);
int operator()();
Vector& operator=(Vector&);
friend Vector operator+(Vector&,Vector&);
friend Vector operator-(Vector&,Vector&);
friend IntReal operator*(Vector&,Vector&);
friend ostream& operator<<(ostream& output,Vector&);
friend istream& operator>>(istream& input,Vector&);
void amount(){
IntReal amount;
for(int i=0;i<len;i++)
amount=amount+v[i];
amount.Real::print();
};
private:
IntReal *v; //has class IntReal
int len;
};
Vector::Vector(int size)
{
if(size<=0||size>100)
{
cout<<"The size of"<<size<<"is fail!/n";
exit(0);
}
v=new IntReal[size];
for(int i=0;i<size;i++)
v[i]=0;
len=size;
}
Vector::Vector(Vector& A)
{v=new IntReal(A());
len=A();
for(int i=1;i<A.len;i++)
v[i]=A[i];
}
Vector::Vector(IntReal*B,int size)
{
if(size<=0||size>100)
{cout<<"The size of"<<size<<"is fail!/n";
exit(0);}
v=new IntReal[size];
len=size;
for(int i=0;i<size;i++)
v[i]=B[i];
}
Vector::~Vector()
{
delete[]v;
len=0;
}
IntReal& Vector::operator[](int i)
{
if(i>=0&&i<len)
return v[i];
cout<<"The subscript "<<i<<" is outside!/n";
exit(0);
}
int Vector::operator()()
{
return len;
}
Vector operator+(Vector& A,Vector& B)
{
int size=A();
IntReal* T=new IntReal[size];
if(size==B())
{
for(int i=0;i<size;i++)
T[i]=A[i]+B[i];
return Vector(T,size);
}
else
{
cout<<"Operator+fail!/n";
exit(0);
}
}
Vector operator-(Vector& A,Vector& B)
{
int size=A();
IntReal* T=new IntReal[size];
if(size==B())
{
for(int i=0;i<size;i++)
T[i]=A[i]-B[i];
return Vector(T,size);
}
else
{
cout<<"Operator+fail!/n";
exit(0);
}
}
IntReal operator*(Vector& A,Vector& B)
{
if(A()==B())
{ IntReal T;
for(int i=0;i<A();i++)
T=A[i]*B[i]+T;
return T;
}
else
{
cout<<"Operator /"*/" fail!/n";
exit(0);
}
}
Vector& Vector::operator=(Vector& B)
{ v=new IntReal[B.len];
len=B.len;
for(int i=0;i<B.len;i++)
v[i]=B[i];
return *this;
}
ostream& operator<<(ostream& output,Vector& A)
{
for(int i=0;i<A.len;i++)
output<<A[i]<<" ";
return output;
}
istream& operator>>(istream& input,Vector& A)
{
for(int i=0;i<A();i++)
input>>A[i];
return input;
}
class Matrix:public Vector{
public:
Matrix(int=1,int=1);
Matrix(Matrix&);
Matrix(Vector*,int=1,int=1);
friend ostream& operator<<(ostream& output,Matrix&);
friend istream& operator>>(istream& input,Matrix&);
Matrix& operator=(Matrix& );
Matrix transpose(); //转置
friend Matrix operator-(Matrix&,Matrix&);
friend Matrix operator+(Matrix&,Matrix&);
friend Matrix operator*(Matrix&,Matrix&);
protected:
Vector* Row_vector;
int Row_num,Column_num;
};
Matrix operator+(Matrix &A,Matrix& B){
if(A.Row_num==B.Row_num&&A.Column_num==B.Column_num){
Vector* temp=new Vector[A.Row_num];
for (int i=0;i<A.Row_num;i++){
temp[i]=A.Row_vector[i]+B.Row_vector[i];
}
return Matrix(temp,A.Row_num,A.Column_num);
}
else
{
cout<<"Operator /"+/" fail! now return the first one/n";
return A;
}
}
Matrix operator-(Matrix &A,Matrix& B){
if(A.Row_num==B.Row_num&&A.Column_num==B.Column_num){
Vector* temp=new Vector[A.Row_num];
for (int i=0;i<A.Row_num;i++){
temp[i]=A.Row_vector[i]-B.Row_vector[i];
}
return Matrix(temp,A.Row_num,A.Column_num);
}
else
{
cout<<"Operator /"-/" fail! now return the first one/n";
return A;
}
}
Matrix operator*(Matrix &A,Matrix& B){
if(A.Column_num==B.Row_num){
Matrix temp(A.Row_num,B.Column_num);
B=B.transpose();
for (int i=0;i<A.Row_num;i++){
for(int j=0;j<B.Row_num;j++)
temp.Row_vector[i][j]=A.Row_vector[i]*B.Row_vector[j];
}
B=B.transpose(); //还原B矩阵
return temp;
}
else
{
cout<<"Operator /"*/" fail! now return the first one/n";
return A;
}
}
istream& operator>>(istream& input,Matrix& A){
for(int i=0;i<A.Row_num;i++) //Vector* Row_vector;
input>>A.Row_vector[i];
return input;
}
ostream& operator<<(ostream& output,Matrix& A){
for(int i=0;i<A.Row_num;i++)
output<<A.Row_vector[i]<<endl;
return output;
}
//int Matrix::operator()(){
//return
//}
Matrix& Matrix::operator=(Matrix& A)
{
// 为什么可以忽略这句呢:Row_vector=new Vector[A.Row_num];
Row_num=A.Row_num;Column_num=A.Column_num;
for(int i=0;i<A.Row_num;i++)
Row_vector[i]=A.Row_vector[i];
return *this;
}
Matrix Matrix::transpose() //转置
{ Matrix temp(Column_num,Row_num);
for(int i=0;i<Column_num;i++)
for(int j=0;j<Row_num;j++)
temp.Row_vector[i][j]=Row_vector[j][i];
return temp;
}
Matrix::Matrix(int Row,int Column){
if(Row<=0||Row>=100||Column<=0||Column>=100){
cout<<"The size of"<<Row<<"or"<<Column<<"overflows";
exit(0);
}
Row_num=Row;Column_num=Column;
Row_vector=new Vector[Row];
Vector Row_temp(Column);
for(int i=0;i<Row;i++){
Row_vector[i]=Row_temp;
}
}
Matrix::Matrix(Matrix& A){
Row_vector=new Vector[A.Row_num];
Row_num=A.Row_num;Column_num=A.Column_num;
for (int i=0;i<Row_num;i++){
Row_vector[i]=A.Row_vector[i];
}
}
Matrix::Matrix(Vector* Row,int num,int len){
Row_vector=new Vector[num];
Row_num=num;Column_num=len;
for(int i=0;i<num;i++)
Row_vector[i]=Row[i];
}
测试主函数如下:
void main(){
int r1,r2,c1,c2;
cout<<"Input the numble of row in Matrix A:/n";
cin>>r1;
cout<<"Input the numble of column in Matrix A:/n";
cin>>c1;
Matrix A(r1,c1);
cout<<"Input the elements of Matrix A:/n";
cin>>A;
cout<<"Input the numble of row in Matrix B:/n";
cin>>r2;
cout<<"Input the numble of column in Matrix B:/n";
cin>>c2;
Matrix B(r2,c2);
cout<<"Input the elements of Matrix B:/n";
cin>>B;
Matrix C;
cout<<"相加为:/n"<<endl;
C=A+B;
cout<<C<<endl;
cout<<"相减为:/n"<<endl;
C=A-B;
cout<<C<<endl;
cout<<"相乘为:/n"<<endl;
C=A*B;
cout<<C<<endl;
}