• cs11_c++_lab2


    Matrix.hh

     1 class Matrix
     2 {
     3     int row;
     4     int col;
     5     int *p;
     6 public:
     7 
     8     Matrix();
     9     Matrix(int x,int y);
    10     ~Matrix();
    11 
    12     Matrix(Matrix &m);
    13 
    14     int getrows();
    15     int getcols();
    16 
    17     void setelem(int x,int y,int elem);
    18 
    19     int getelem(int x,int y);
    20 
    21     void add(Matrix &m);
    22 
    23     void subtract(Matrix &m);    
    24 
    25     bool equals(Matrix &m);
    26 };
    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 Matrix::Matrix(Matrix &m)
    26 {
    27     row=m.row;
    28     col=m.col;
    29     p=new int[row*col];
    30     for(int i=0;i<row*col;i++)
    31     p[i]=m.p[i];
    32 }
    33 
    34 Matrix::~Matrix()
    35 {
    36     delete[] p;
    37 }
    38 
    39 int Matrix::getrows()
    40 {
    41     return row;
    42 }
    43 
    44 int Matrix::getcols()
    45 {
    46     return col;
    47 }
    48 
    49 int Matrix::getelem(int x,int y)
    50 {
    51     assert(x>=0);
    52     assert(y>=0);
    53     return p[x*col+y];
    54 }
    55 
    56 void Matrix::setelem(int x,int y,int elem)
    57 {
    58     assert(x>=0);
    59     assert(y>=0);
    60     p[x*col+y]=elem;
    61 }
    62 
    63 void Matrix::add(Matrix &m)
    64 {
    65     assert(    (row==m.row) || (col==m.col) );
    66     for(int i=0;i<row*col;i++)
    67     p[i]+=m.p[i];
    68 }
    69 
    70 void Matrix::subtract(Matrix &m)
    71 {
    72     assert( (row==m.row) || (col==m.col) );
    73     for(int i=0;i<row*col;i++)
    74     p[i]-=m.p[i];
    75 }
    76 
    77 bool Matrix::equals(Matrix &m)
    78 {
    79     bool b=true;
    80     if( (row!=m.row) || (col!=m.col) )return false;
    81     for(int i=0;i<row*col;i++)
    82         if(p[i]!=m.p[i])
    83         {    b=false;return b;}
    84     return b;
    85 }
    View Code
  • 相关阅读:
    软件层次结构
    PHP 配合Cross-Origin Resource Sharing实现跨域 使用心得
    C语言 标准I/O库函数 fgets 使用心得
    PHP 逗号运算符 的作用
    PHP 函数 array_map 使用心得
    PHP 函数 htmlspecialchars 使用心得
    Go语言特性学习
    curl文件上传类
    php 协程理解
    php 分词扩展 scws
  • 原文地址:https://www.cnblogs.com/amdb/p/4468187.html
Copyright © 2020-2023  润新知