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 ...
随机推荐
- JS时间格式 GMT格式转换
JavaScript时间格式转换总结 1.当前系统区域设置格式(toLocaleDateString和toLocaleTimeString) 例子:(new Date()).toLocaleDateS ...
- 如何导出和导入mysql数据(数据迁移)
. 导出数据库数据 mysqldump -uroot -p dp_db > dumpout.sql 其中 root是账户名 dp_db是需要导出的数据库名称 dumpout.sql存储导出的数据 ...
- fastdfs-nginx扩展模块源码分析
FastDFS-Nginx扩展模块源码分析 1. 背景 在大多数业务场景中,往往需要为FastDFS存储的文件提供http下载服务,而尽管FastDFS在其storage及tracker都内置了htt ...
- 在Angular1.X中使用CSS Modules
在Angular1.5中,增加了一个Component方法,并且定义了组件的若干生命周期hook,在代码规范中也是推崇组件化开发,但是很遗憾的是,CSS模块化组件化的问题并没有得到解决,大部分项目的打 ...
- spring mvc fastJson 自定义类型转换(返回数据) 实现对ObjectId类型转换
json用的alibaba fastJson ValueFilter filter = new ValueFilter() { @Override public Object process(Obje ...
- 内网透过公网nginx和vpn实现微信接口调试
条件: 1.公网IP开放80/443端口 2.vpn,我熟悉openvpn 3.nginx反向代理 1.在公网服务器上装nginx和vpnserver 2.本地调试电脑装vpnclient,mac o ...
- [MySQL5.6 新特性] 全局事务标示符(GTID)
GTID的全称为 global transaction identifier , 可以翻译为全局事务标示符,GTID在原始master上的事务提交时被创建.GTID需要在全局的主-备拓扑结构中保持唯 ...
- HTML JavaScripts
JavaScript JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出相应的处理. ...
- eclipse的常用快捷键
Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加) Ctrl+Alt+↑ 复制当前行到上一行(复制增加) Alt+↓ 当前行和下面一行交互位置(特别实用,可以省去先剪切, ...
- 解决YII提交POST表单出现400错误,以及ajax post请求时出现400问题
POST表单400错误: 正确做法: Add this in the head section of your layout: <?= Html::csrfMetaTags() ?> -- ...