zoukankan      html  css  js  c++  java
  • C++面向对象编程解决三阶矩阵相加减

    /*此处用面向对象编程*/
    
    #include<iostream>
    #include<string.h>
    using namespace std;
    class Matrices
    {
    private:
        int mat[3][3];
    public:
        Matrices();
        void input()
        {
            for(int i=0; i<3; i++)
            {
                for(int j=0; j<3; j++)
                {
                    cin>>mat[i][j];
                }
            }
        }
        friend Matrices operator+(Matrices &,Matrices &);
        friend Matrices operator-(Matrices &a,Matrices &b);
        friend ostream &operator <<(ostream &output,Matrices &);
        friend istream &operator >>(istream &input,Matrices &);
    };
    
    Matrices ::Matrices()
    {
        memset(mat,0,sizeof(mat));
    }
    
    istream &operator >>(istream &input,Matrices &a)
    {
        for(int i=0; i<3; i++)
        {
            for(int j=0; j<3; j++)
            {
                input>>a.mat[i][j];
            }
        }
        return input;
    }
    
    ostream &operator <<(ostream &output,Matrices &a)
    {
        for(int i=0; i<3; i++)
        {
            for(int j=0; j<3; j++)
            {
                output<<a.mat[i][j]<<"   ";
            }
            cout<<endl;
        }
        return output;
    }
    
    Matrices operator+(Matrices &a,Matrices &b)
    {
        Matrices c;
        for(int i=0; i<3; i++)
        {
            for(int j=0; j<3; j++)
            {
                c.mat[i][j]=a.mat[i][j]+b.mat[i][j];
            }
        }
        return c;
    }
    
    Matrices operator-(Matrices &a,Matrices &b)
    {
        Matrices c;
        for(int i=0; i<3; i++)
        {
            for(int j=0; j<3; j++)
            {
                c.mat[i][j]=a.mat[i][j]-b.mat[i][j];
            }
        }
        return c;
    }
    
    int main()
    {
        cout<<"请输入需要进行的三阶加法运算(+)或减法运算(-)"<<endl;
        char c;
        Matrices A,B,C;
        while(cin>>c)
        {
            if(c=='+'||c=='-')
            {
                if(c=='+')
                {
                    cout<<"转换成功!即将进行加法运算"<<endl;
                    cout<<"请依次输入矩阵1,以回车换行(确认)"<<endl;
                    cin>>A;
                    cout<<"请依次输入矩阵2,以回车换行(确认)"<<endl;
                    cin>>B;
                    C=A+B;
                    cout<<"运算成功!两矩阵的和为:"<<endl;
                }
                else
                {
                    cout<<"转换成功!即将进行减法运算"<<endl;
                    cout<<"请依次输入矩阵1,以回车换行(确认)"<<endl;
    
                    cin>>A;
                    cout<<"请依次输入矩阵2,以回车换行(确认)"<<endl;
                    cin>>B;
                    C=A-B;
    
                    cout<<"运算成功!两矩阵的差为:"<<endl;
                }
                cout<<endl;
                cout<<C;
                cout<<"可输入'+'或'-'继续进行运算"<<endl;
            }
            else cout<<"数据格式错误!请重新输入:"<<endl;
        }
    }
    
    
    /*
    
    测试数据
    
    1 1 1
    2 2 2
    3 3 3
    
    1 1 1
    2 2 2
    3 3 3
    */
    
  • 相关阅读:
    [CF1355] Codeforces Round #643 (Div. 2)
    [ABC189] AtCoder Beginner Contest 189
    P3702 [SDOI2017]序列计数 (三模数NTT)
    P3321 [SDOI2015]序列统计 (NTT快速幂)
    洛谷P4157 [SCOI2006]整数划分
    洛谷P2553 [AHOI2001]多项式乘法
    洛谷P1919 (模板)A*B Problem升级版(FFT快速傅里叶)
    MySQL学习总结-详细版(包括下载安装)
    查看oracle数据库中表是否被锁
    SQL优化(面试题)
  • 原文地址:https://www.cnblogs.com/-beyond/p/5621225.html
Copyright © 2011-2022 走看看