表达式分析+矩阵+计算器+寄存器=矩阵计算器
怎么想起来搞这个呢..
//刚看龙书兴致勃勃要搞表达式分析
这个寄存器比较简陋,26字母+4缓存,//字母不分大小写
当然,不只能算矩阵,还能算数= =
简陋的命令行如图
尚处于初步阶段,奇怪的功能们尚待加入=ω=
 
代码 展示一下接口吧= =
//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的更多相关文章

  1. [转载]John Burkardt搜集的FORTRAN源代码

    Over the years, I have collected, modified, adapted, adopted or created a number of software package ...

  2. Shadertoy 教程 Part 3 - 矩形和旋转

    Note: This series blog was translated from Nathan Vaughn's Shaders Language Tutorial and has been au ...

  3. angular2系列教程(十一)路由嵌套、路由生命周期、matrix URL notation

    今天我们要讲的是ng2的路由的第二部分,包括路由嵌套.路由生命周期等知识点. 例子 例子仍然是上节课的例子:

  4. 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 ...

  5. Atitit Data Matrix dm码的原理与特点

    Atitit Data Matrix dm码的原理与特点 Datamatrix原名Datacode,由美国国际资料公司(International Data Matrix, 简称ID Matrix)于 ...

  6. Android笔记——Matrix

    转自:http://www.cnblogs.com/qiengo/archive/2012/06/30/2570874.html#translate Matrix的数学原理 在Android中,如果你 ...

  7. 通过Matrix进行二维图形仿射变换

    Affine Transformation是一种二维坐标到二维坐标之间的线性变换,保持二维图形的"平直性"和"平行性".仿射变换可以通过一系列的原子变换的复合来 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. APP开发和web开发的区别

    1.web开发: 1.浏览器直接访问,协议是http,基本都是PC访问,也就是在家和办公场合,网络比较好. 2.web如果多个服务集成的话使用的是ESB来统一集成 2.APP开发 1.手机app客户端 ...

  2. C#设计模式——解释器模式(Interpreter Pattern)

    一.概述 在软件开发特别是DSL开发中常常需要使用一些相对较复杂的业务语言,如果业务语言使用频率足够高,且使用普通的编程模式来实现会导致非常复杂的变化,那么就可以考虑使用解释器模式构建一个解释器对复杂 ...

  3. ubuntu12.04 安装mac主题

    1.下载主题包我把主题上传到了csdn的资源栏目,下载地址:http://download.csdn.net/detail/chenjiebin/4531537 2.将/mac/sharc/theme ...

  4. Access数据库的模糊查询到底是用*还是%

    今天被用了一下Access数据库,结果被它的模糊查询给折腾了一上午,到底是用*还是%?特此记下来 事情是这样的,我用C#写了个小的窗体程序,访问Access数据库进行一个模糊查询,我先手工往Acces ...

  5. AngularJS-系统代码的配置和翻译

    前言:在Web开发中常常会遇到这样的情况,有些页面的下拉选项是固定不变的几个,比如:性别,一般有男.女.保密等.对于这样的情形我们一般在数据库中存储的是数字或者其对应的代码,如果是可维护的需要系统给出 ...

  6. Excel 取得一定范围内最大的有值的行号

    dim iRow iRow = Range("A1000").End(xlUp).Row

  7. cs11_c++_lab6

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

  8. oracle number型日期转date型日期

    在搞数据库时,发现有这样的一个字段,类型是NUMBER(38),查看了一下里面的数据,都是这样的, 13239576781141321326994295132212930680413221297162 ...

  9. 两种open()函数

    C语言中文件操作函数中,open()有两种形式: 一种形式是有两个参数open2: 另一种形式是有三个参数open3: 共有的参数有两个,第一个是"被打开文件的路径",第二个是&q ...

  10. VS2008下,aspx页面设计模式消失,只有黑白字体

    版权声明:本文为博主原创文章,未经博主允许不得转载. RT,在工具选项里面重新设定的时候,报错,说一部分文件无法安装. 寻找一晚上,终于有了解决方案,供以后参考. luanwey(大陆响尾蛇)(不做温 ...