Matrix.hh

 class Matrix
{
int row;
int col;
int *p;
void copy(const Matrix &m);
void clearup();
public: Matrix();
Matrix(int x,int y);
~Matrix(); Matrix(const Matrix &m); const int getrows() const;
const int getcols() const; void setelem(int x,int y,int elem); const int getelem(int x,int y) const; Matrix & operator=(const Matrix &m);
Matrix & operator+=(const Matrix &m);
Matrix & operator-=(const Matrix &m);
Matrix & operator*=(const Matrix &m); const Matrix operator+(const Matrix &m) const;
const Matrix operator-(const Matrix &m) const;
const Matrix operator*(const Matrix &m) const; bool operator==(const Matrix &m) const;
bool operator!=(const Matrix &m) const; };

Matrix.cpp

 #include "Matrix.hh"
#include <cassert>
#include <iostream> using namespace std; Matrix::Matrix()
{
row=;
col=;
p=;
} Matrix::Matrix(int x,int y)
{
assert(x>=);
assert(y>=);
row=x;
col=y;
p=new int[row*col];
for(int i=;i<row*col;i++)
p[i]=;
} void Matrix::copy(const Matrix &m)
{
// row = m.row;
// col = m.row;
p = new int[row * col];
for(int i = ;i < row * col;i++)
p[i]=m.p[i];
} void Matrix::clearup()
{
delete[] p;
} Matrix::Matrix(const Matrix &m)
{
row = m.row;
col = m.col;
(*this).copy(m);
} Matrix::~Matrix()
{
(*this).clearup();
} const int Matrix::getrows() const
{
return row;
} const int Matrix::getcols() const
{
return col;
} const int Matrix::getelem(int x,int y) const
{
assert(x>=);
assert(y>=);
return p[x*col+y];
} void Matrix::setelem(int x,int y,int elem)
{
assert(x>=);
assert(y>=);
p[x*col+y]=elem;
} Matrix & Matrix:: operator=(const Matrix &m)
{
if(this!=&m)
{
clearup();
row = m.row;
col = m.col;
(*this).copy(m);
}
return *this;
} Matrix & Matrix:: operator+=(const Matrix &m)
{
assert(row==m.row);
assert(col==m.col);
for(int i=;i<row*col;i++)
p[i]+=m.p[i];
return *this;
} Matrix & Matrix:: operator-=(const Matrix &m)
{
assert(row==m.row);
assert(col==m.col);
for(int i=;i<row*col;i++)
p[i]-=m.p[i];
return *this;
} Matrix & Matrix:: operator*=(const Matrix &m)
{
// assert(col==m.row);
int *pp = new int[row * m.col];
int sum = ;
for(int i=;i<row;i++)
{
for(int j=;j<m.col;j++)
{
for(int k=;k<col;k++)
{
sum+=p[i * row + k] * m.p[k * m.row + j];
}
pp[i * m.col + j]=sum;
sum = ;
}
} delete[] p;
p = pp;
col = m.col;
return *this;
} const Matrix Matrix:: operator+(const Matrix &m) const
{
assert(row == m.row);
assert(col == m.col); Matrix other;
other = *this;
other += m;
return other;
} const Matrix Matrix:: operator-(const Matrix &m) const
{
assert(row == m.row);
assert(col == m.col); Matrix other;
other = *this;
other -= m;
return other;
} const Matrix Matrix:: operator*(const Matrix &m) const
{
assert(col==m.row);
Matrix other;
other = *this;
other *= m;
return other;;
} bool Matrix:: operator==(const Matrix &m) const
{
bool b = true;
// assert(row == m.row);
// assert(col == m.col);
if(row != m.row || col != m.col)
return false;
for(int i = ; i < row * col; i++)
{
if(p[i] != m.p[i])
{
b = false;
return b;
}
}
return b;
} bool Matrix:: operator!=(const Matrix &m) const
{
return !((*this)==m);
} /*
void Matrix:: add(Matrix &m)
{
assert( (row==m.row) || (col==m.col) );
for(int i=0;i<row*col;i++)
p[i]+=m.p[i];
} void Matrix::subtract(Matrix &m)
{
assert( (row==m.row) || (col==m.col) );
for(int i=0;i<row*col;i++)
p[i]-=m.p[i];
} bool Matrix::equals(Matrix &m)
{
bool b=true;
if( (row!=m.row) || (col!=m.col) )return false;
for(int i=0;i<row*col;i++)
if(p[i]!=m.p[i])
{ b=false;return b;}
return b;
}
*/

cs11_c++_lab3的更多相关文章

  1. cs11_c++_lab7

    wcount.cc #include <iostream> #include <map> #include <string> #include <algori ...

  2. cs11_c++_lab6

    expressions.hh #ifndef EXPRESSIONS_HH #define EXPRESSIONS_HH #include "environment.hh" #in ...

  3. cs11_c++_lab5待修改

    heap.hh #ifndef HEAP_HH #define HEAP_HH #include <iostream> #include <stdexcept> #includ ...

  4. cs11_c++_lab4b

    SparseVector.hh class SparseVector { private: //结构体不一定会用到,不用初始化 struct node { int index; int value; ...

  5. cs11_c++_lab4a

    SparseVector.hh class SparseVector { private: //结构体不一定会用到,不用初始化 struct node { int index; int value; ...

  6. cs11_c++_lab2

    Matrix.hh class Matrix { int row; int col; int *p; public: Matrix(); Matrix(int x,int y); ~Matrix(); ...

  7. cs11_c++_lab1

    lab1.cpp #include "Point.hh" #include <iostream> #include <cmath> using namesp ...

随机推荐

  1. Objective-C数据类型之id,SEL,BOOL,nil,NULL和NSNull

     id id是指向Objective-C对象的指针,等价于C语言中的void*,可以映射任何对象指针指向他,或者映射它指向其他的对象.常见的id类型就是类的delegate属性. SEL SEL类型是 ...

  2. 1月12日,HTML学习笔记2

    妈蛋,这两天看HTML看上瘾了,感觉这玩意有点简单,反馈期太短了,我的python都荒废了/(ㄒoㄒ)/~~. 不多说了,把记录贴上来,到时过几天再拿出来整理一下,写上注释,顺便当做复习 去研究css ...

  3. python运算符重载

    python运算符重载就是在解释器使用对象内置操作前,拦截该操作,使用自己写的重载方法. 重载方法:__init__为构造函数,__sub__为减法表达式 class Number: def __in ...

  4. mvc路由,mvc区域

    1.路由在进行匹配时,会默认匹配第一个路由信息 2.路由规则的{control}和{action}时不能改变的 3.路由规则可以中间字符可以随便定义,但是{control}和{action}必须使用一 ...

  5. EntityFrameWork使用

    1.简单查询: SQL: ? 1 SELECT * FROM [Clients] WHERE Type=1 AND Deleted=0 ORDER BY ID EF: ? 1 2 3 4 5 6 7 ...

  6. Today I learnt

    2015-May-22 In Oracle database, dropping a table don't free up the space directly. You'll need to di ...

  7. CentOS 6.5下配置iSCSI网络存储

    一.简介 iSCSI(internet SCSI)技术由IBM公司研究开发,是一个供硬件设备使用的.可以在IP协议的上层运行的SCSI指令集,这种指令集合可以实现在IP网络上运行 SCSI协议,使其能 ...

  8. winform基本控件的使用2(用户登录界面的设计)

    首先还是通过例子来讲解,这次的实验主要是使用messageBox的使用方法,关于messageBox的使用方法在我的博客里面有相关的转载,请自己查看,下面说一下实验要求. 功能要求:模拟一个用户登陆的 ...

  9. HackerRank "Flatland Space Stations"

    A bit Greedy can achieve O(m) - the mid station between 2 adjacent cities has the longest distance w ...

  10. MySql启动,提示:Plugin 'FEDERATED' is disabled....Cannot allocate memory for the buffer pool

    2016-05-27 09:25:01 31332 [Note] Plugin 'FEDERATED' is disabled. 2016-05-27 09:25:01 31332 [Note] In ...