Matrix Calculator

时间:2024-01-19 09:02:14
Matrix Calculator表达式分析+矩阵+计算器+寄存器=矩阵计算器
怎么想起来搞这个呢..
//刚看龙书兴致勃勃要搞表达式分析
这个寄存器比较简陋,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.    继承的运算符重载有点坑??