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 ...
随机推荐
- ayase系列
[冒个泡]技能视觉效果の自定义 by ayase [11-09 V2.4]任务数量转换属性点的lua脚本 [335]瞎倒腾的item.dbc生成工具..
- 【新手】python爬虫遍历贴吧用户
想法是遍历学校贴吧的用户,获取用户的数据用来分析,因为是初学python,就一点一点的写,变量命名也不规范,见谅 系统:windows 版本:python 3.5 #获取河北大学工商学院吧1000页以 ...
- Python 各进制间的转换(转)
转载自:http://blog.chinaunix.net/uid-21516619-id-1824975.html python 2.6以后内置函数#10进制转为2进制>>> bi ...
- 一个简易的反射类库NMSReflector
转自:http://blog.csdn.net/lanx_fly/article/details/53914338 背景简介 以前看过一些代码,是简单的读取SqlReader然后赋值给Model,我不 ...
- ecstore-lnmp环境下crontab不执行原因
因为lnmp.org默认禁止了proc_open函数,需要开启 开启后 lnmp restart ==== contab还是用crontab -e好,有些用www用户的似乎执行不了
- Rails : css或js文件无法成功预编译或调用jquery类插件时预编译问题
调用bootstrap css框架时,将bootstrap文件夹放入 vendor/assets/下 bootstrap文件结构如下: [shenma@localhost demo]$ ls v ...
- php 函数汇总
extract 从数组中将变量导入到当前的符号表 $arr['age'] = 30; $arr['name'] = 'bluesky'; $arr['sex'] = 'male'; var_dump( ...
- codeforces 645 E. Intellectual Inquiry
一个字符串,由前k个字母组成,长度为m + n,其中前m个字符已经确定,后面n个由你自由选择, 使得这个串的不同的子序列的个数最多,空串也算一个子序列. 1 <= m <= 10^6,0 ...
- linux下配置ip地址四种方法(图文方法)
主要是用第四种方法 (1)Ifconfig命令 第一种使用ifconfig命令配置网卡的ip地址.此命令通常用来零时的测试用,计算机启动后 ip地址的配置将自动失效.具体用法如下.Ipconfig ...
- linux mint 崩溃
换完linux mint 今天突然崩溃了.出现如下错误 因为是双屏.一个屏幕显示一般,这么不重要.搜了一下,找到解决方案 解决办法 ctrl+atl+f1 login sudo apt-get ins ...