I'm trying to build a mathematical expression (including parenthesis) with the operators and operands included in a queue.
Here is my code:
string createExp (queue<char> q) {
string s;
string s1, s2;
char c;
while (!q.empty()) {
c = q.front();
if (c == 'x') {
s += "x";
q.pop();
}
else if (c == 'y') {
s += "y";
q.pop();
}
else if (c == 'a') {
s += "avg(";
q.pop();
s1 = createExp(q);
q.pop();
s2 = createExp(q);
q.pop();
s += s1;
s += ',';
s += s2;
s += ')';
}
else if (c == 's') {
s += "sin(pi*";
q.pop();
op++;
}
else if (c == 'c') {
s += "cos(pi*";
q.pop();
op++;
}
else {
s += "*";
q.pop();
}
}
while (op > cp) {
s += ")";
cp++;
}
return (s);
}
As you can see, in the case of the average (avg), I'm trying to call recursively to the function to obtain the next sequence of values.
For example, if my queue contains the next values:
s m a y x y
The expression should be like this:
sin(pi*(avg(y,x)*y)
But my code return this sequence:
sin(pi**avg(yyx)yyxyyx
Could you help me with this?
Thank you very much.