zoukankan      html  css  js  c++  java
  • 重载矩阵加法运算 代码参考

     1 #include <iostream>
     2 
     3 using namespace std;
     4 
     5 class Matrix
     6 {
     7     private:
     8         int a,b;
     9         int arr[100][100];
    10     public:
    11         Matrix()
    12         {
    13             for(int i=0;i<100;i++)
    14             {
    15                 for(int j=0;j<100;j++)
    16                 {
    17                     arr[i][j]=0;
    18                 }
    19             }
    20         }
    21         void Gethl(int a, int b){this->a=a;this->b=b;}
    22         void Getnum()
    23         {
    24             for(int i=0;i<a;i++)
    25             {
    26                 for(int j=0;j<b;j++)
    27                 {
    28                     cin>>arr[i][j];
    29                 }
    30             }
    31         }
    32         Matrix operator+(Matrix &c)
    33         {
    34             Matrix temp;
    35             for(int i=0;i<a;i++)
    36             {
    37                 for(int j=0;j<b;j++)
    38                 {
    39                     temp.arr[i][j]=this->arr[i][j]+c.arr[i][j];
    40                 }
    41             }
    42             return temp;
    43         }
    44         void operator=(Matrix c)
    45         {
    46               for(int i=0;i<a;i++)
    47             {
    48                 for(int j=0;j<b;j++)
    49                 {
    50                     arr[i][j]=c.arr[i][j];
    51                 }
    52             }
    53         }
    54         void show()
    55         {
    56             for(int i=0;i<a;i++)
    57             {
    58                 for(int j=0;j<b;j++)
    59                 {
    60                     if(j!=0)    cout<<" "<<arr[i][j];
    61                     else    cout<<arr[i][j];
    62                     if(j==b-1)  cout<<endl;
    63                 }
    64             }
    65         }
    66 };
    67 
    68 int main()
    69 {
    70     int a,b;
    71     cin>>a>>b;
    72     Matrix one,two,three;
    73     one.Gethl(a,b);
    74     two.Gethl(a,b);
    75     three.Gethl(a,b);
    76     one.Getnum();
    77     two.Getnum();
    78     three=one+two;
    79     three.show();
    80     return 0;
    81 }
  • 相关阅读:
    数字以万做单位——Java
    创建二维码工具类——Java
    Java 截取指定长度的字符串
    堆排序
    context:component-scan 的使用说明
    @Autowired @Resource @Qualifier的区别
    声明对象和创建对象的区别
    maven 命令
    maven 创建
    maven pom.xml文件
  • 原文地址:https://www.cnblogs.com/Conan-jine/p/12804743.html
Copyright © 2011-2022 走看看