PDA

Visualizza la versione completa : [C++]Expr Algebriche


Luc@s
31-01-2004, 14:03
Sto provando a fare un parser di expr matematiche.
Ho creato la mia bella classina ma ho un problema


#include <list>
#include <string>

// The expression operator enum
typedef enum
{
PLUS = 0,
MINUS,
DIV,
MOD
} OpType;

// The Expression
struct ExprOp
{
OpType op;
std::string op1;
std::string op2;
ExprOp(const std::string &Op1, const std::string &Op2, OpType Op)
{
op1 = Op1;
op2 = Op2;
op = Op;
}
};

typedef std::list<ExprOp *> Expression;
typedef std::list<ExprOp *>::iterator Expression_it;

class CExprParse
{
private:
std::string ris;
Expression expr;
bool ParseEspr(const std::string &value);
public:
CExprParse();
~CExprParse();
CExprParse(CExprParse &p);
bool Exec();
inline const std::string Get();
bool addExpr(const std::string &value);
};

CExprParse::CExprParse()
{
}

CExprParse::CExprParse(CExprParse &p)
{
// no autoassignment
if(&p != this)
{
// TODO
}
}

CExprParse::~CExprParse()
{
}

bool CExprParse::ParseEspr(const std::string &value)
{
unsigned int plus_loc = value.find( "+", 0 );
unsigned int minus_loc = value.find( "-", 0 );
unsigned int div_loc = value.find( "/", 0 );
if(plus_loc != std::string::npos)
{
expr.push_back(new ExprOp(value.substr( 0, minus_loc ), value.substr( minus_loc, value.size() ),PLUS));
return true;
}
else if(minus_loc != std::string::npos)
{
expr.push_back(new ExprOp(value.substr( 0, minus_loc ), value.substr( minus_loc, value.size() ), MINUS));
return true;
}
else if(div_loc != std::string::npos)
{
expr.push_back(new ExprOp(value.substr( 0, minus_loc ), value.substr( minus_loc, value.size() ), DIV));
return true;
}else
{
return false;
}
}

bool CExprParse::addExpr(const std::string &value)
{
return (ParseEspr(value));
}

bool CExprParse::Exec()
{
std::string tmp("");
ExprOp * ex;
// IF EMPTY EXIT
if(expr.empty())
return false;
while(!expr.empty())
{
// RETURN THE PART OF EXRESSION TO EXECUTE
ex = expr.front();
//////////////////////////////////////////
///////// CHECK THE LITTERAL /////////////
//std::string f_l = ;
//std::string s_l = ;
/*
if(f_l == s_l) // ax + bx
{
switch(ex.op)
{
case PLUS:
break;
case MINUS:
break;
case DIV:
break;
}
}else // ax + by
{
switch(ex.op)
{
case PLUS:
break;
case MINUS:
break;
case DIV:
break;
}
}
*/
//////////////////////////////////////////
//////////CHECK THE NUMBERICAL ///////////
//int fist = static_cast< int >();
//int second = static_cast< int >();
//////////////////////////////////////////
////////// EXEC THE OPERARATION //////////

// REMOVE THE ELEMENT
expr.pop_front();
}
return true;
}

const std::string CExprParse::Get()
{
if(Exec())
return ris;
else
new std::string("Can't execute the expression\n");
}


Come proseguo????
La mia implementazione del metodo Exec va bene????

Tnk 10000000000000

Luc@s
31-01-2004, 15:36
Cosi va meglio???


#include <list>
#include <string>

// The expression operator enum
typedef enum
{
PLUS = 0,
MINUS,
DIV,
MOD
} OpType;

// The Expression
struct ExprOp
{
OpType op;
std::string op1;
std::string op2;
ExprOp(const std::string &Op1, const std::string &Op2, OpType Op)
{
op1 = Op1;
op2 = Op2;
op = Op;
}
};

typedef std::list<ExprOp *> Expression;
typedef std::list<ExprOp *>::iterator Expression_it;

class CExprParse
{
private:
std::string ris;
Expression expr;
bool ParseEspr(const std::string &value);
public:
CExprParse();
~CExprParse();
CExprParse(CExprParse &p);
bool Exec();
inline const std::string Get();
bool addExpr(const std::string &value);
};

CExprParse::CExprParse()
{
}

CExprParse::CExprParse(CExprParse &p)
{
// no autoassignment
if(&p != this)
{
// TODO
}
}

CExprParse::~CExprParse()
{
}

bool CExprParse::ParseEspr(const std::string &value)
{
unsigned int plus_loc = value.find( "+", 0 );
unsigned int minus_loc = value.find( "-", 0 );
unsigned int div_loc = value.find( "/", 0 );
if(plus_loc != std::string::npos)
{
expr.push_back(new ExprOp(value.substr( 0, minus_loc ), value.substr( minus_loc, value.size() ),PLUS));
return true;
}
else if(minus_loc != std::string::npos)
{
expr.push_back(new ExprOp(value.substr( 0, minus_loc ), value.substr( minus_loc, value.size() ), MINUS));
return true;
}
else if(div_loc != std::string::npos)
{
expr.push_back(new ExprOp(value.substr( 0, minus_loc ), value.substr( minus_loc, value.size() ), DIV));
return true;
}else
{
return false;
}
}

bool CExprParse::addExpr(const std::string &value)
{
return (ParseEspr(value));
}

bool CExprParse::Exec()
{
std::string tmp("");
ExprOp * ex;
// IF EMPTY EXIT
if(expr.empty())
return false;
while(!expr.empty())
{
// RETURN THE PART OF EXRESSION TO EXECUTE
ex = expr.front();
unsigned int x_loc = ex->op1.find( "x", 0 );
unsigned int y_loc = ex->op1.find( "y", 0 );
unsigned int x2_loc = ex->op2.find( "x", 0 );
unsigned int y2_loc = ex->op2.find( "y", 0 );
if(x_loc != std::string::npos && x2_loc != std::string::npos) // ax + bx
{
/*
ax + bx
\
(a + b)x
\
abx
a = op1.substr(0, x_loc);
b = op2.substr(0, x2_loc);
*/
}
else if(x_loc != std::string::npos && y2_loc != std::string::npos) // ax + by
{
/*
ax + by
\
\
ax by
a = op1.substr(0, x_loc);
b = op2.substr(0, y2_loc);
*/
}
else if((x_loc == std::string::npos && y2_loc != std::string::npos)) // ay + by
{
/*
ay + by
\
(a + b)y
\
aby
a = op1.substr(0, y_loc);
b = op2.substr(0, y2_loc);
*/
}
expr.pop_front();
}
return true;
}

const std::string CExprParse::Get()
{
if(Exec())
return ris;
else
new std::string("Can't execute the expression\n");
}

Loading