Help with compliling c++ program #include #include #include using namespace std;
ID: 3879883 • Letter: H
Question
Help with compliling c++ program
#include
#include
#include
using namespace std;
template
class Stack
{
public:
Stack();//creates the stack
bool isempty(); // returns true if the stack is empty
T gettop();//returns the front of the list
void push(T entry);//add entry to the top of the stack
void pop();//remove the top of the stack
private:
vector stack;
}; // Stack
template
Stack::Stack()
{
}
template
bool Stack::isempty()
{ if (stack.size() == 0)
return true;
else
return false;
}
template
T Stack::gettop()
{ return stack[stack.size()-1];
}
template
void Stack::push(T entry)
{
stack.push_back(entry);
}
template
void Stack::pop()
{
stack.pop_back();
}
int main()
{
Stack S;
S.push('[');
string y;
string x = "(A+B)*C-(D/(J+D))]";
int index=0;
char s=x[index];
while(s!=']')
{
if(s=='(');
{
S.push(s);
}
if(s=='*' or s=='/')
{
while(S.gettop()=='*' or S.gettop()=='/')
{
y += S.gettop();
S.pop();
}
S.push(s);
}
if(s=='+' or s=='-')
{
while(S.gettop()=='+' or S.gettop()=='-'
or S.gettop()=='*' or S.gettop()=='/')
{
y += S.gettop();
S.pop();
}
S.push(s);
}
if(s==')' or s==']')
{
while(S.gettop()!=='(' or S.gettop()!=='[')
{
y += S.gettop();
S.pop();
}
S.pop();
}
if(s!=='*' or s!=='/' or s!=='+' or s!=='-')
{
y += s;
}
index++;
s=x[index];
}
cout< }
Above is the source code and below are the errors i keep receiving. The code is supposed to convert a string from infix to postfix.
lab12.cpp: In function ‘int main()’:
lab12.cpp:84:35: error: expected primary-expression before ‘=’ token
while(S.gettop()!=='(' or S.gettop()!=='[')
^
lab12.cpp:84:55: error: expected primary-expression before ‘=’ token
while(S.gettop()!=='(' or S.gettop()!=='[')
^
lab12.cpp:91:15: error: expected primary-expression before ‘=’ token
lab12.cpp:91:26: error: expected primary-expression before ‘=’ token
if(s!=='*' or s!=='/' or s!=='+'
^
lab12.cpp:91:37: error: expected primary-expression before ‘=’ token
if(s!=='*' or s!=='/' or s!=='+'
^
lab12.cpp:92:31: error: expected primary-expression before ‘=’ token
or s!=='-')
^
Explanation / Answer
U can solve the error by changing the following expression
while(S.gettop()!=='(' or S.gettop()!=='[')
to
while(S.gettop()!='(' or S.gettop()!='[')
and in the similar manner
if(s!=='*' or s!=='/' or s!=='+' to
if(s!='*' or s!='/' or s!='+') your problem will get solved
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.