Matrix Calculator

//matrix.h #ifndef __MATRIX_H__
#define __MATRIX_H__ #include <vector> using std::vector; template <class T>
class MATRIX{
public:
explicit MATRIX(): R(0), C(0), V(0){} MATRIX(int _R, int _C): R(_R), C(_C), V(_R){
for (int i = 0; i < R; ++i)
V[i].resize(C);
} MATRIX(const MATRIX<T> &m){*this = m;} ~MATRIX(){V.clear();} void resize(int _R, int _C);
bool push_back(const vector<T>& _V);
void swap_row(int R1, int R2);
//void swap_col(int C1, int C2); inline int rows() const{return R;}
inline int cols() const{return C;}
inline bool empty() const{return !R||!C;}
inline bool square() const{return (!empty() && R == C);} inline T&operator ()(int i, int j){return V[i][j];}
const vector<T>& operator[](int _R) const { return V[_R]; }
vector<T>& operator[](int _R){ return V[_R]; }
protected:
vector<vector<T> > V;
int R, C;
}; template <class T>
void MATRIX<T>::resize(int _R, int _C){
if (R == _R && C == _C)
return;
else if (R == _R && C != _C){
for (int i = 0; i < R; ++i)
V[i].resize(_C);
C = _C;
}
else if (R != _R && C == _C){
V.resize(_R);
for (int i = R; i < _R; ++i)
V[i].resize(C);
R = _R;
}
else {
V.resize(_R);
for (int i = 0; i < _R; ++i)
V[i].resize(_C);
R = _R, C = _C;
}
} template <class T>
bool MATRIX<T>::push_back(const vector<T> &_V){
if (!R || C == (int)_V.size())
V.push_back(_V);
else
return false;
return true;
} template <class T>
void MATRIX<T>::swap_row(int R1, int R2){
if (R1 != R2 && R1 > 0 && R1 <= R && R2 > 0 && R2 <= R){
vector<T> t = V[R1];
V[R1] = V[R2];
V[R2] = t;
}
} template <class T>
const MATRIX<T> trans(const MATRIX<T> &m){
MATRIX<T> ret;
if (m.empty()) return ret;
int R = m.cols();
int C = m.rows();
ret.resize(R, C); for (int i = 0; i < R; ++i)
for (int j = 0; j < C; ++j)
ret[i][j] = m[j][i];
return ret;
} inline static int max(int a, int b){
return a > b ? a : b;
} inline static int min(int a, int b){
return a < b ? a : b;
} class matrix:public MATRIX<double>{
public:
matrix(): MATRIX<double>(){}
matrix(int _C, int _R): MATRIX<double>(_C, _R){}
matrix(const matrix &m){*this = m;} matrix &operator += (const matrix &m);
matrix &operator -= (const matrix &m);
matrix &operator *= (const matrix &m);
matrix &operator *= (const double k);
//const matrix &operator /= (const matrix &m); friend matrix operator ~ (const matrix &m);
friend bool operator == (const matrix &m1, const matrix &m2);
friend bool operator != (const matrix &m1, const matrix &m2);
friend matrix operator + (const matrix &m1, const matrix & m2);
friend matrix operator - (const matrix &m1, const matrix & m2);
friend matrix operator * (const matrix &m1, const matrix & m2);
friend matrix operator * (const matrix &m, const double k);
friend matrix operator * (const double k, const matrix &m);
//const matrix operator / (const matrix &m1, const matrix & m2); //matrix LU();
double norm();
matrix sub(int i, int j, int _R, int _C);
matrix row(int _R);
matrix col(int _C);
double &maxelem();
double &minelem(); virtual bool input(int _R, int _C);
virtual void output(int digit);
virtual void output();
}; #include "matrix.cpp" #endif
//calc.h #ifndef __CALC_H__
#define __CALC_H__ #include <vector>
#include <cstring> class STATPARSER{
public:
STATPARSER(): ptr(0), stat(0){}
STATPARSER(char STAT[]): ptr(0), stat(0){SetStatement(STAT);}
~STATPARSER(){} void SetStatement(char STAT[]);
bool InputFromKeybord(); inline bool StatEnabled(){return stat.size();} protected:
//char stat[], *ptr;
std::vector<char>::iterator ptr;
std::vector<char> stat;
//virtual int GetToken() = 0;
//virtual bool TokenInSet() = 0;
}; template <class T>
class CALCULATOR:public STATPARSER{
public:
CALCULATOR():STATPARSER(){memset(registered, 0, sizeof(registered));} T &Calc();
protected:
T reg[30]; // T[0..25] for identifiers // T[26..29] for register
int token;
bool registered[30];
inline int id(char ch);
inline int allocreg();
inline int isreg(int __id);
inline virtual int gettoken();
virtual int generate() = 0;
}; #include "calc.cpp" #endif
//matrix_calculator.h #include "calc.h"
#include "matrix.h" #define ID_DOUBLE (0)
#define ID_MATRIX (1) class pan_matrix:public matrix{
public:
pan_matrix():matrix(), f_val(0), id_val(ID_DOUBLE){}
pan_matrix(const matrix &m):matrix(m), f_val(0), id_val(ID_MATRIX){}
pan_matrix(const pan_matrix &m){*this = m;} pan_matrix(double _f_val):matrix(), f_val(_f_val), id_val(ID_DOUBLE){}
pan_matrix(int _C, int _R):matrix(_C, _R), f_val(0), id_val(ID_MATRIX){} pan_matrix &operator += (const pan_matrix &m);
pan_matrix &operator -= (const pan_matrix &m);
pan_matrix &operator *= (const pan_matrix &m);
pan_matrix &operator /= (const pan_matrix &m);
//const matrix &operator /= (const matrix &m); friend pan_matrix operator ~ (const pan_matrix &m);
friend bool operator == (const pan_matrix &m1, const pan_matrix &m2);
friend bool operator != (const pan_matrix &m1, const pan_matrix &m2);
friend pan_matrix operator + (const pan_matrix &m1, const pan_matrix & m2);
friend pan_matrix operator - (const pan_matrix &m1, const pan_matrix & m2);
friend pan_matrix operator * (const pan_matrix &m1, const pan_matrix & m2);
friend pan_matrix operator / (const pan_matrix &m1, const pan_matrix & m2); double f_val;
int id_val; virtual bool input();
virtual void output(int digit);
virtual void output();
}; class matrix_calculator:public CALCULATOR<pan_matrix>{
public:
matrix_calculator():CALCULATOR<pan_matrix>(), AutoInit(0){} virtual inline void Print(char ch);
virtual inline void Print(); bool AutoInit;
protected:
virtual int generate();
virtual inline int gettoken();
pan_matrix get_constant;
private:
inline bool Assigned(int x){return ~(x);}
inline void Reset(int &x){x = -1;}
inline void Destroy(int x){if (isreg(x)) registered[x] = false;}
inline void Free(int x){registered[x] = false;}
inline void Reg(int x){registered[x] = true;}
inline void RegNum(int x){reg[x] = get_constant; registered[x] = true;}
inline int ZigZag(int &id_a, int &oper_1, int &id_b, int &oper_2, int id_c);
}; #include "matrix_calculator.cpp"
//main #define MATRIX_EXCEPTION
#include "matrix_calculator.h"
#include <cstdio> int main(){
matrix_calculator C;
C.AutoInit = true;
while (1){
C.InputFromKeybord();
pan_matrix m = C.Calc();
printf("Result = ");
if (m.id_val == ID_DOUBLE){
m.output(3);
}
else{
m.output();
}
puts("--------------------");
}
}
Ps. 继承的运算符重载有点坑??
Matrix Calculator的更多相关文章
- [转载]John Burkardt搜集的FORTRAN源代码
Over the years, I have collected, modified, adapted, adopted or created a number of software package ...
- Shadertoy 教程 Part 3 - 矩形和旋转
Note: This series blog was translated from Nathan Vaughn's Shaders Language Tutorial and has been au ...
- angular2系列教程(十一)路由嵌套、路由生命周期、matrix URL notation
今天我们要讲的是ng2的路由的第二部分,包括路由嵌套.路由生命周期等知识点. 例子 例子仍然是上节课的例子:
- Pramp mock interview (4th practice): Matrix Spiral Print
March 16, 2016 Problem statement:Given a 2D array (matrix) named M, print all items of M in a spiral ...
- Atitit Data Matrix dm码的原理与特点
Atitit Data Matrix dm码的原理与特点 Datamatrix原名Datacode,由美国国际资料公司(International Data Matrix, 简称ID Matrix)于 ...
- Android笔记——Matrix
转自:http://www.cnblogs.com/qiengo/archive/2012/06/30/2570874.html#translate Matrix的数学原理 在Android中,如果你 ...
- 通过Matrix进行二维图形仿射变换
Affine Transformation是一种二维坐标到二维坐标之间的线性变换,保持二维图形的"平直性"和"平行性".仿射变换可以通过一系列的原子变换的复合来 ...
- [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
- [LeetCode] Longest Increasing Path in a Matrix 矩阵中的最长递增路径
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
随机推荐
- MySQL For Windows修改最大连接数
1.从官网下载安装MySQL Installer.MySQL Installer 提供了简单易用.向导式的 MySQL 软件的安装体验过程(目前只支持 Windows),包含的产品有: MySQL S ...
- Sprintf新解 (ZT)
Sprintf新解 2012-08-06 11:26:45 分类: 原文地址:Sprintf新解 作者:harserm 由于sprintf 跟printf 在用法上几乎一样,只是打印的目的地不同而已 ...
- Spring Boot+Cloud RestTemplate 调用IP或域名
在SpringBoot+Cloud的项目中,我们使用了自动配置的OAuth2RestTemplate,RestTemplate,但是在使用这些restTemplate的时候,url必须是服务的名称,如 ...
- TCP/IP之大明内阁---协议的制定
个人感言:真正的知识是深入浅出的,码农翻身" 公共号将苦涩难懂的计算机知识,用形象有趣的生活中实例呈现给我们,让我们更好地理解.感谢"码农翻身" 公共号,感谢你们的成果, ...
- 湖人VS爵士!!科比4月14日最后一战,本赛季最高得分!狂得60分!!完美大逆转!!!
莫愁前路无知己,天下谁人不识君.科比,愿你如迈克尔·乔丹,仍然活跃在篮球界.退役不是结束,而是另一段人生的开始. 北京时间2016年4月14日,湖人101-96击败爵士,科比-布莱恩特告别战,20年职 ...
- bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.# see /usr/share/doc/bash/examples/startup-fil ...
- 解压版mysql安装
步骤如下: 1.下载安装包 2.在环境变量中配置安装包的bin路径 3.修改安装包下的my-default.ini. 修改basedir和datadir的值为解压文件对应的路径,port和 serve ...
- Android IOS WebRTC 音视频开发总结(七七)-- WebRTC的架构和协议栈
本文主要介绍WebRTC的架构和协议栈(我们翻译和整理的,译者:litie),最早发表在[编风网] 支持原创,转载必须注明出处,欢迎关注我的微信公众号blacker(微信ID:blackerteam ...
- python中的内存管理
不像大多数编译型语言,变量必须在使用之前声明名字和类型,在python中,变量在第一次被赋值时自动声明.在变量创建时,python解释器会根据语法和右侧的操作数来决定新对象的类型,在对象创建后,一个该 ...
- Ajax方法封装
打算自己封装一个ajax方法,再不用jq库的情况下,直接引用: ajax作用:数据交互,在不刷新页面的情况下,发送请求,获取数据: 首页第一步常见一个ajax对象:XMLHttpRequest,之后会 ...