finish operation vector building

This commit is contained in:
2026-10-05 11:17:48 +03:00
parent f00ed50d32
commit bc4fbafec4
7 changed files with 412 additions and 60 deletions
+94 -1
View File
@@ -24,7 +24,9 @@ public:
enum TokenType {
VAR,
CONST,
OPERATOR
OPERATOR,
CV, // compound value (result of an operation)
NAT, // not a token
} t;
enum Operator {
@@ -41,6 +43,10 @@ public:
std::shared_ptr<std::string> s;
double d;
Operator o;
struct {
int w; // weight
int i; // index in operation vector
} cv;
explicit TokenValue(std::string& s)
: s(std::make_shared<std::string>(s))
@@ -89,6 +95,27 @@ public:
t = OPERATOR;
}
Token(const Expression::Token& other)
{
t = other.t;
switch (t)
{
case VAR:
v.s = other.v.s;
break;
case CONST:
v.d = other.v.d;
break;
case OPERATOR:
v.o = other.v.o;
break;
default:
std::cout << "Warning: token type is invalid\n";
break;
}
}
Token(Expression::Token&& other)
{
t = other.t;
@@ -128,6 +155,24 @@ public:
}
}
std::shared_ptr<std::string> to_math_str(void) const
{
switch (t)
{
case VAR:
return v.s;
case CONST:
{
char buffer[128] = {0};
return std::make_shared<std::string>((std::ostringstream() << v.d).str());
}
case OPERATOR:
return std::make_shared<std::string>(op_math_str());
default:
return std::make_shared<std::string>("Not a valid token!");
}
}
const char *type_str(void) const
{
switch (t)
@@ -170,6 +215,54 @@ public:
return "Invalid operator";
}
}
const char *op_math_str(void) const
{
if (t != OPERATOR)
{
return "Token is not an operator!";
}
switch (v.o)
{
case OPA:
return "(";
case CPA:
return ")";
case ADD:
return "+";
case SUB:
return "-";
case MUL:
return "*";
case DIV:
return "/";
case POW:
return "^";
default:
return "Invalid operator";
}
}
};
struct Operation
{
Token o; // operator
enum Type
{
NORMAL,
DERIVED,
} tl, tr; // type left, type right
int il, ir; // index left, index right
int w; // operation weight
Operation(enum Expression::Token::Operator o, enum Type tl, int il, enum Type tr, int ir, int w)
: o(o), tl(tl), tr(tr), il(il), ir(ir), w(w)
{
}
};
class TreeNode {