【C++】模版矩阵类

来源:互联网 发布:1962年印度战争 知乎 编辑:程序博客网 时间:2024/05/18 00:14
功能要求:
1. 设计一个模板矩阵类,支持任意行数、列数的矩阵,且元素类型也是任意的(int、float、double 或其他自定义数据类型)。为该矩阵类设计下列操作:
 构造函数(提供多个构造函数,参数自行设计)
 拷贝构造、拷贝赋值
矩阵加法(需提供+操作与+=操作)
 矩阵乘法(需提供*操作与*=操作)
 矩阵自增(需提供前置版本与后置版本的++操作,矩阵的自增在这里被定义为每个元素的自增)
 矩阵比较(需提供==操作与!=操作)
 打印矩阵(首行显示矩阵的行数与列数,另起一行开始打印整个矩阵,同一行的矩阵元素之间以Tab 隔开)

 输入输出流操作(需提供<<操作与>>操作):

(1)输出格式参照上述的打印矩阵操作;

(2)输入格式:在一个字符流中,起始的2 个字符串(以空格、Tab 或回车换行符隔开)分别为矩阵的行数与列数(假设分别为m 和n),后续的m * n 个字符串(以空格、Tab 或回车换行符隔开)是矩阵的元素内容。

以形如(x, y)的方式访问矩阵元素(需提供读版本和写版本)
2. 在main()函数里,从三个输入文件读取三个矩阵的数据(元素类型为int),创建三个矩阵对象,并测试上述的全部操作,将操作结果打印到一个输出文件(每打印一个操作结果前应打印该操作的类型,相邻的操作结果以一个空行隔开)。
3. 设计一个自定义数据类型,提供加法、乘法、自增、比较、打印、输入输出、拷贝构造、拷贝赋值等操作,并以其作为元素类型构造一些矩阵对象,测试上述的全部矩阵操作。

输入文件:
C:\2_in_1.txt:矩阵1 的数据(文件首行有2 个数字,表示行数与列数,以空格隔开;后续
内容为矩阵元素,每一行的多个元素以空格或Tab 隔开)。
C:\2_in_2.txt:矩阵2 的数据(格式同上)。
C:\2_in_3.txt:矩阵3 的数据(格式同上)。
(自行设计与功能3 有关的输入文件,并在文档中注明文件格式。)
输出文件:
C:\2_out_1.txt:各种矩阵操作的结果。
(自行设计与功能3 有关的输出文件,并在文档中注明文件格式。)

#include <iostream>#include <cstdlib>using namespace std;const int SIZE = 10;class Fruit{private:    double weight;    double price;public:Fruit(){weight = 0.0;price = 0.0;}Fruit(double a,double b){weight = a;price = b;}Fruit(const Fruit &v){weight = v.weight;price = v.price;}~Fruit(){}Fruit operator=(double c){weight = c;price = c;return *this;}Fruit operator+(const Fruit &r)const{    double tot1,tot2;    tot1 = weight+r.weight;tot2 = price+r.price;Fruit a(tot1,tot2);return a;    }Fruit operator+=(const Fruit &r){weight += r.weight;price  += r.price;return *this;}Fruit operator*(const Fruit &r)const{double pr1,pr2;pr1 = weight*price;pr2 = r.weight*r.price;Fruit b(pr1,pr2);return b;}Fruit operator++(){weight++;price++;}bool operator ==(const Fruit &r)const{if(weight == r.weight&&price==r.price)return true;elsereturn false;}bool operator !=(const Fruit &r)const{if(weight ==r.weight||price ==r.price)return false;elsereturn true;}friend std::ostream & operator <<(std::ostream & os,const Fruit &v){os<<"("<<v.weight<<","<<v.price<<")";return os;}            friend std::istream & operator >>(std::istream & is,Fruit &b){using std::cout;is >>b.weight;is >>b.price;return is;}};template <typename rtype>class Matrix{    private:        int row;        int rank;        rtype s[SIZE][SIZE];    public:        Matrix();        Matrix(int ro,int ran);        Matrix(const Matrix<rtype> &a);        ~Matrix();        Matrix<rtype> operator+(const Matrix<rtype> &r)const;        Matrix<rtype> operator+=(const Matrix<rtype> &r);        Matrix<rtype> operator*(const Matrix<rtype> &r)const;        void operator*=(const Matrix<rtype> &r);        Matrix<rtype> operator++();//前置++        Matrix<rtype> operator++(int);//后置++        bool operator==(const Matrix<rtype> &r)const;        bool operator!=(const Matrix<rtype> &r)const;        friend std::ostream & operator <<<>(std::ostream & os,const Matrix<rtype> &v);        friend std::istream & operator >><>(std::istream & is,Matrix<rtype> &b);        rtype& operator()(int a,int b);};template <typename rtype>Matrix<rtype>::Matrix(){    row = 0;    rank = 0;    for(int i=0;i<row;i++)    {        for(int j=0;j<rank;j++)            s[i][j]=0;    }}template <typename rtype>Matrix<rtype>::Matrix(int ro,int ran){    this->row = ro;    this->rank = ran;    for(int i=0;i<row;i++)    {        for(int j=0;j<rank;j++)            s[i][j]=0;    }}template <typename rtype>Matrix<rtype>::Matrix(const Matrix<rtype> &a){    row = a.row;    rank = a.rank;    for(int i = 0;i<row;i++)    {        for(int j=0;j<rank;j++)            s[i][j]=a.s[i][j];    }}template <typename rtype>Matrix<rtype>::~Matrix(){}template <typename rtype>Matrix<rtype> Matrix<rtype>::operator+(const Matrix<rtype> &r)const{    using std::cout;Matrix<rtype> tot(r.row,r.rank);    if(row == r.row&&rank == r.rank)    {        for(int i = 0;i<row;i++)        {            for(int j = 0;j<rank;j++)                tot.s[i][j]=s[i][j]+r.s[i][j];        }return tot;    }    else      cout <<"These two Matrix can not add!\n";}template <typename rtype>Matrix<rtype> Matrix<rtype>::operator+=(const Matrix<rtype> &r){    using std::cout;    if(row == r.row&&rank == r.rank)    {        for(int i = 0;i<row;i++)        {            for(int j = 0;j<rank;j++)                (*this).s[i][j]=(*this).s[i][j]+r.s[i][j];        }        return *this;    }    else      cout <<"These two Matrix can not add!\n";}template <typename rtype>Matrix<rtype> Matrix<rtype>::operator*(const Matrix<rtype> &r)const{    using std::cout;Matrix pro(row,r.rank);    if(rank!=r.row)    {        cout <<"The two matrix can not multiply!\n";    }    else    {       for(int a=0;a<row;a++)       {         for(int b=0;b<r.rank;b++)         {             for(int c=0;c<rank;c++)             {                 pro.s[a][b]+= s[a][c]*r.s[c][b];             }         }       }  return pro;    }}template <typename rtype>void Matrix<rtype>::operator*=(const Matrix<rtype> &r){Matrix temp(row,rank);    using std::cout;    if(rank!=r.row)    {        cout <<"The two matrix can not multiply!\n";    }    else    {       for(int a=0;a<row;a++)       {         for(int b=0;b<r.rank;b++)         {             for(int c=0;c<rank;c++)             {                 temp.s[a][b]+= (*this).s[a][c]*r.s[c][b];             }         }       }   for(int a = 0;a<row;a++)   {   for(int b = 0;b<rank;b++)   {             (*this).s[a][b]=temp.s[a][b];              }   }     }}template <typename rtype>Matrix<rtype> Matrix<rtype>::operator++()//前置++{    for(int i = 0;i<row;i++)    {       for(int j = 0;j<rank;j++)           (*this).s[i][j]++;    }    return *this;}template <typename rtype>Matrix<rtype> Matrix<rtype>::operator++(int)//后置++{    ++(*this);    return *this;}template <typename rtype>bool Matrix<rtype>::operator==(const Matrix<rtype> &r)const{    if(row ==r.row&&rank==r.rank)    {        for(int i = 0;i<row;i++)        {            for(int j = 0;j<rank;j++)            if(s[i][j]!=r.s[i][j])               return false;        }         return true;    }    else        return false;}template <typename rtype>bool Matrix<rtype>::operator!=(const Matrix<rtype> &r)const{    if(row!=r.row||rank!=r.rank)       return true;    else    {        for(int i = 0;i<row;i++)        {            for(int j = 0;j<rank;j++)            if(s[i][j]!=r.s[i][j])               return true;        }        return false;    }}template <typename rtype>std::ostream & operator <<(std::ostream & os,const Matrix<rtype> &v){    os<<v.row<<" "<<v.rank<<'\n';    for(int i = 0;i<v.row;i++)    {        for(int j = 0;j<v.rank;j++)        {            os<<v.s[i][j]<<"    ";        }        os<<'\n';    }    return os;}template <typename rtype>std::istream & operator >>(std::istream & is,Matrix<rtype> &b){    is >>b.row;    is >>b.rank;    for(int i=0;i<b.row;i++)    {        for(int j = 0;j<b.rank;j++)            is >>b.s[i][j];    }    return is;}template <typename rtype>rtype&  Matrix<rtype>::operator()(int a,int b){    return s[a-1][b-1];}

#include <iostream>#include <cstdlib>#include <fstream>#include "Matrix.h"using namespace std;int main(){    ifstream fp1,fp2,fp3;    ofstream op;    Matrix <int> a,b,c;    fp1.open("C:\\2_in_1.txt");    if(!fp1.is_open())    {        cout <<"Could not open the file"<<endl;        exit(EXIT_FAILURE);    }    else        fp1>>a;    fp2.open("C:\\2_in_2.txt");    if(!fp2.is_open())    {        cout <<"Could not open the file"<<endl;        exit(EXIT_FAILURE);    }    else        fp2 >> b;    op.open("C:\\2_out_1.txt");Matrix<int>d(a);    op <<"矩阵1和矩阵2是否相等?"<<endl;    if(a==b)       op <<"相等。"<<endl;    else       op <<"不相等。"<<endl;    op <<"矩阵1与矩阵2相乘的结果为:"<<endl;    op << a*b<<'\n';op <<"矩阵1与矩阵2相加的结果为:"<<endl;    op << a+b<<'\n';    op << "矩阵1每个元素自增之后的结果为: "<<endl;    a++;    op <<a<<'\n';    op <<"矩阵2每个元素自增之后的结果为: "<<endl;    ++b;    op <<b<<'\n';    fp3.open("C:\\2_in_3.txt");    if(!fp3.is_open())    {        cout <<"Could not open the file"<<endl;        exit(EXIT_FAILURE);    }    else        fp3 >> c;    op <<"矩阵1与矩阵3相加的结果为:"<<endl;d+=c;    op << d<<'\n';op <<"\n矩阵1与矩阵3的乘积为: "<<endl;    c*=a;    op << c<<'\n';op <<"矩阵3第2行第2列的元素为:"<<endl;op <<c(2,2)<<'\n';op <<"在窗口输入一个值以改变矩阵3第2行第2列的值:"<<endl;cout <<"在窗口输入一个值以改变矩阵3第2行第2列的值:"<<endl;int k;cin >> k;c(2,2) = k;op <<"现在,矩阵3第2行第2列元素的值为:"<<endl;op <<c(2,2)<<'\n';    fp1.close();    fp2.close();    fp3.close();Matrix<Fruit> ba,ap;ifstream fp4,fp5;fp4.open("C:\\2_in_4.txt");    if(!fp4.is_open())    {        cout <<"Could not open the file"<<endl;        exit(EXIT_FAILURE);    }    else{        fp4 >> ba;fp4 >> ap;}ofstream sp;sp.open("C:\\2_out_2.txt");    sp <<"第一个矩阵的值:"<<endl;sp << ba;sp <<"第二个矩阵的值:"<<endl;sp << ap;Matrix<Fruit>e(ba);sp <<"两个矩阵相加的值为:"<<endl;sp <<e+ap<<endl;sp <<"两个矩阵相乘的值为:"<<endl;sp <<ba*ap<<endl;sp <<"两个矩阵相加的值为:"<<endl;ba += ap;sp <<ba<<endl;sp <<"ba为ba与ap的和之后两个矩阵相乘的值为:"<<endl;ba *= ap;sp <<ba<<endl;    return 0;}



0 0
原创粉丝点击