cs11_c++_lab3
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的更多相关文章
- cs11_c++_lab7
wcount.cc #include <iostream> #include <map> #include <string> #include <algori ...
- cs11_c++_lab6
expressions.hh #ifndef EXPRESSIONS_HH #define EXPRESSIONS_HH #include "environment.hh" #in ...
- cs11_c++_lab5待修改
heap.hh #ifndef HEAP_HH #define HEAP_HH #include <iostream> #include <stdexcept> #includ ...
- cs11_c++_lab4b
SparseVector.hh class SparseVector { private: //结构体不一定会用到,不用初始化 struct node { int index; int value; ...
- cs11_c++_lab4a
SparseVector.hh class SparseVector { private: //结构体不一定会用到,不用初始化 struct node { int index; int value; ...
- cs11_c++_lab2
Matrix.hh class Matrix { int row; int col; int *p; public: Matrix(); Matrix(int x,int y); ~Matrix(); ...
- cs11_c++_lab1
lab1.cpp #include "Point.hh" #include <iostream> #include <cmath> using namesp ...
随机推荐
- [原创]cocos2d-x研习录-第三阶 多分辨率适配器
在移动终端(智能手机)平台下开发游戏一般都会涉及到屏幕多分辨率适配问题,原因是手机款式多种多样,不同的款式存在有不同的尺寸,即使尺寸相同又可能存在不同的分辨率. 手机屏幕尺寸:指手机屏幕对角线长度. ...
- floyd算法
求两个顶点间的最短距离,直觉是这样的问题可以用尝试和枚举的办法来求解,这显然可行,但是我们可以换个方式来看待这个问题,比如, 可以这样描述,“在给定的点集(编号为1~k,k=图中所有的顶点数量)中,i ...
- canvas关于getImageData跨域问题解决方法
一.问题:在使用html5的canvas是,当用到getImageData方法获取图片信息时,会碰到跨域无法获取的情况,代码如下: document.getElementById("pic& ...
- gzip压缩及测试方法【转载】
Nginx开启Gzip压缩大幅提高页面加载速度 http://www.veryhuo.com/a/view/51706.html 刚刚给博客加了一个500px相册插件,lightbox引入了很多js文 ...
- ubuntu包管理
查看某个文件属于哪个包dpkg -S add-apt-repository 包名software-properties-common命令名/usr/bin/add-apt-repository/usr ...
- ProcessOn
1.地址:http://www.processon.com/ 2.简介:在线创作流程图.BPMN.UML图.UI界面原型设计.iOS界面原型设计等. 3.优势:无需安装,简单易用.可以替代VISO,学 ...
- tar 命令详解
tar命令[root@Linux ~]# tar [-cxtzjvfpPN] 文件与目录 -C 目标目录(注:解压时)参数:-c :建立一个压缩文件的参数指令(create 的意思):-x :解开一个 ...
- coffeeScript中类的继承[学习篇]
只是在看深入浅出coffeescript中感觉真的很好,不光是coffe写法简单,生成的js也值得学习,废话不多说了,直接抄个书上的例子 class Pet constructor: -> @i ...
- CodeIgniter 开发,支付宝接口调用实例
准备:1.alipay官方下载最新接口类库2.解压后,将目录"\即时到账交易接口-create_direct_pay_by_user\demo\create_direct_pay_by_us ...
- MyBatis与Spring整合
1.单独使用MyBatis 单独使用MyBatis,不结合其他框架,主要步骤是: 1.创建SqlSessionFactory对象 创建方法是通过SqlSessionFactoryBuilder这个类从 ...