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

    The Screenshot plugin allows your application to take screenshots of the current screen and save the ...

  2. 通过WMI接口监控服务器性能

    WMI 是微软操作系统的一个内置的组件,通过使用WMI我们可以获取服务器硬件信息.收集服务器性能数据.操作Windows服务,甚至可以远程关机或是重启服务器. 一.在C#编程中使用WMI 要想在C#程 ...

  3. 冒泡排序,sql分页语句

    对数组中的数字进行排序 public int[] PopSmall(int[] IntArray) { ; ; i < IntArray.Length - ; i++) { ; j < I ...

  4. java httpclient cookie

    BasicCookieStore cookieStore = new BasicCookieStore();BasicClientCookie cookie = new BasicClientCook ...

  5. AIDL小结

    AIDL : Android Interface Define Language(接口定义语言) Service中跨进程间通信利器.... 一般都会有Client端和Server端(Server端提供 ...

  6. Objective C ARC 使用及原理

    手把手教你ARC ,里面介绍了ARC的一些特性, 还有将非ARC工程转换成ARC工程的方法 ARC 苹果官方文档 下面用我自己的话介绍一下ARC,并将看文档过程中的疑问和答案写下来.下面有些是翻译,但 ...

  7. 用Qt Creator 对 leveldb 进行简单的读写

    #include <iostream> #include <string> #include <leveldb/db.h> #include <boost/l ...

  8. nginx转发post消息成301了

    刚才开始是:第一次遇到这个问题是因为不支持sslv3 后来测试的时候,是因为没注意https的导致http 301跳转https post请求nginx转发成301了,到时post数据丢失

  9. RTC,登陆后添加权限值

    修改单元:rtcMW.DM.Main; 修改组件:fnLogin 在方法中添加: 服务端: const SQL_SELECT_USER = 'SELECT * FROM Users WHERE Use ...

  10. linux病毒

    linux病毒查杀规范 一.病毒发现 1.ps -A.ps -ef.ps -aux查看是否有异常进程 2.last,lastlog命令可查看最近登录的帐户及时间 3.查看/var/log/messag ...