表达式分析+矩阵+计算器+寄存器=矩阵计算器
怎么想起来搞这个呢..
//刚看龙书兴致勃勃要搞表达式分析
这个寄存器比较简陋,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. Druid安装-单机

    单机版安装 下载安装包http://static.druid.io/artifacts/releases/druid-0.9.1.1-bin.tar.gz 安装  解压缩 安装zookeeper cu ...

  2. android studio新建hello world时出现Rendering Problems

    The following classes could not be instantiated:        - android.support.v7.internal.widget.ActionB ...

  3. SQLServer 列出每个表的列和属性

    USE DBProjectSY GO SELECT OBJECT_SCHEMA_NAME(T.[object_id], DB_ID()) AS [架构名] , T.[name] AS [表名] , A ...

  4. @RequestMapping用法详解

    @RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径. RequestMapping注解有六个属性,下面我们把 ...

  5. TCP、UDP、IP 协议分析

    http://rabbit.xttc.edu.cn/rabbit/htm/artical/201091145609.shtml  http://bhsc881114.github.io/2015/06 ...

  6. 使用Fluent配置表关系

    转载MS官方文档:https://msdn.microsoft.com/zh-cn/data/jj591620 Configuring Relationships with the Fluent AP ...

  7. C语言PIC16 serial bootloader和C#语言bootloader PC端串口通信程序

    了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序). 新PIC16 Boot ...

  8. wc命令

    wc命令的功能为统计指定文件中的字节数.字数.行数, 并将统计结果显示输出. 语法:wc [选项] 文件- 说明:该命令统计给定文件中的字节数.字数.行数.如果没有给出文件名,则从标准输入读取.wc同 ...

  9. C#打开文件对话框

    OpenFileDialog ofd = new OpenFileDialog(); ofd.InitialDirectory = System.Environment.CurrentDirector ...

  10. ue4 c++ 接口

    使用UE4接口比起普通的高级语言,要多做很多工作,是因为要兼容蓝图的使用,有一些小坑需要注意,开始吧. 1.新建接口类 打开UE4编辑器,与往常一样,新建C++类,然后选择Object继承,然后取名字 ...