zoukankan      html  css  js  c++  java
  • cs11_c++_lab3

    Matrix.hh

     1 class Matrix
     2 {
     3     int row;
     4     int col;
     5     int *p;
     6     void copy(const Matrix &m);
     7     void clearup();
     8 public:
     9 
    10     Matrix();
    11     Matrix(int x,int y);
    12     ~Matrix();
    13 
    14     Matrix(const Matrix &m);
    15 
    16     const int getrows() const;
    17     const int getcols() const;
    18 
    19     void setelem(int x,int y,int elem);
    20 
    21     const int getelem(int x,int y) const;
    22 
    23     Matrix & operator=(const Matrix &m);
    24     Matrix & operator+=(const Matrix &m);
    25     Matrix & operator-=(const Matrix &m);
    26     Matrix & operator*=(const Matrix &m);
    27 
    28     const Matrix operator+(const Matrix &m) const;
    29     const Matrix operator-(const Matrix &m) const;
    30     const Matrix operator*(const Matrix &m) const;
    31 
    32     bool operator==(const Matrix &m) const;
    33     bool operator!=(const Matrix &m) const;
    34 
    35 };
    View Code

    Matrix.cpp

      1 #include "Matrix.hh"
      2 #include <cassert>
      3 #include <iostream>
      4 
      5 using namespace std;
      6 
      7 Matrix::Matrix()
      8 {
      9     row=0;
     10     col=0;
     11     p=0;
     12 }
     13 
     14 Matrix::Matrix(int x,int y)
     15 {
     16     assert(x>=0);
     17     assert(y>=0);
     18     row=x;
     19     col=y;
     20     p=new int[row*col];
     21     for(int i=0;i<row*col;i++)
     22         p[i]=0;
     23 }
     24 
     25 void Matrix::copy(const Matrix &m)
     26 {
     27 //    row = m.row;
     28 //    col = m.row;
     29     p = new int[row * col];
     30     for(int i = 0;i < row * col;i++)
     31         p[i]=m.p[i];
     32 }
     33 
     34 void Matrix::clearup()
     35 {
     36     delete[] p;
     37 }
     38 
     39 Matrix::Matrix(const Matrix &m)
     40 {
     41     row = m.row;
     42     col = m.col;
     43     (*this).copy(m);
     44 }
     45 
     46 Matrix::~Matrix()
     47 {
     48     (*this).clearup();
     49 }
     50 
     51 const int Matrix::getrows() const
     52 {
     53     return row;
     54 }
     55 
     56 const int Matrix::getcols() const
     57 {
     58     return col;
     59 }
     60 
     61 const int Matrix::getelem(int x,int y) const
     62 {
     63     assert(x>=0);
     64     assert(y>=0);
     65     return p[x*col+y];
     66 }
     67 
     68 void Matrix::setelem(int x,int y,int elem)
     69 {
     70     assert(x>=0);
     71     assert(y>=0);
     72     p[x*col+y]=elem;
     73 }
     74 
     75 Matrix & Matrix:: operator=(const Matrix &m)
     76 {
     77     if(this!=&m)
     78     {
     79         clearup();
     80         row = m.row;
     81         col = m.col;
     82         (*this).copy(m);        
     83     }
     84     return *this;
     85 }
     86 
     87 Matrix & Matrix:: operator+=(const Matrix &m)
     88 {
     89     assert(row==m.row);
     90     assert(col==m.col);
     91     for(int i=0;i<row*col;i++)
     92         p[i]+=m.p[i];
     93     return *this;
     94 }
     95 
     96 Matrix & Matrix:: operator-=(const Matrix &m)
     97 {
     98     assert(row==m.row);
     99     assert(col==m.col);
    100     for(int i=0;i<row*col;i++)
    101         p[i]-=m.p[i];
    102     return *this;
    103 }
    104 
    105 Matrix & Matrix:: operator*=(const Matrix &m)
    106 {
    107 //    assert(col==m.row);
    108     int *pp = new int[row * m.col];
    109     int sum = 0;
    110     for(int i=0;i<row;i++)
    111     {
    112         for(int j=0;j<m.col;j++)
    113         {
    114             for(int k=0;k<col;k++)
    115             {
    116                 sum+=p[i * row + k] * m.p[k * m.row + j];
    117             }
    118             pp[i * m.col + j]=sum;
    119             sum = 0;
    120         }
    121     }
    122     
    123     delete[] p;
    124     p = pp;
    125     col = m.col; 
    126     return *this;
    127 }
    128 
    129 const Matrix Matrix:: operator+(const Matrix &m) const
    130 {
    131     assert(row == m.row);
    132     assert(col == m.col);
    133     
    134     Matrix other;
    135     other = *this;
    136     other += m;
    137     return other;
    138 }
    139 
    140 const Matrix Matrix:: operator-(const Matrix &m) const
    141 {
    142     assert(row == m.row);
    143     assert(col == m.col);
    144 
    145     Matrix other;
    146     other = *this;
    147     other -= m;
    148     return other; 
    149 }
    150 
    151 const Matrix Matrix:: operator*(const Matrix &m) const
    152 {
    153     assert(col==m.row);
    154     Matrix other;
    155     other = *this;
    156     other *= m;
    157     return other;;
    158 }
    159 
    160 bool Matrix:: operator==(const Matrix &m) const
    161 {
    162     bool b = true;
    163 //    assert(row == m.row);
    164 //    assert(col == m.col);
    165     if(row != m.row || col != m.col)
    166          return false;
    167     for(int i = 0; i < row * col; i++)
    168     {
    169         if(p[i] != m.p[i])
    170         {
    171             b = false;
    172             return b;
    173         }
    174     }
    175     return b;
    176 }
    177 
    178 bool Matrix:: operator!=(const Matrix &m) const
    179 {
    180     return !((*this)==m);
    181 }
    182 
    183 /*
    184 void Matrix:: add(Matrix &m)
    185 {
    186     assert(    (row==m.row) || (col==m.col) );
    187     for(int i=0;i<row*col;i++)
    188     p[i]+=m.p[i];
    189 }
    190 
    191 void Matrix::subtract(Matrix &m)
    192 {
    193     assert( (row==m.row) || (col==m.col) );
    194     for(int i=0;i<row*col;i++)
    195     p[i]-=m.p[i];
    196 }
    197 
    198 bool Matrix::equals(Matrix &m)
    199 {
    200     bool b=true;
    201     if( (row!=m.row) || (col!=m.col) )return false;
    202     for(int i=0;i<row*col;i++)
    203         if(p[i]!=m.p[i])
    204         {    b=false;return b;}
    205     return b;
    206 }
    207 */
    View Code
  • 相关阅读:
    ES 使用小结
    TruncateATable 清除一张表
    js 排序,去重
    读高性能JavaScript编程 第四章 Conditionals
    读高性能JavaScript编程 第四章 Duff's Device
    c# AOP 文章地址
    String、StringBuffer与StringBuilder之间区别
    批处理命令
    C#中的is和as操作符
    c# 入门
  • 原文地址:https://www.cnblogs.com/amdb/p/4468196.html
Copyright © 2011-2022 走看看