Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Help with c++ program. The following code takes an infix expression and converts

ID: 3879892 • Letter: H

Question

Help with c++ program. The following code takes an infix expression and converts it to postfix. When I compile the code it returns "Segmentation fault". However I don't know what causes this.

#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< }

Explanation / Answer

Segmentation fault problem is solved and modified code is given below

#include<iostream>
#include<string>
#include<vector>
#include<ctype.h>
using namespace std;
template<class T>
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<T> stack;
}; // Stack
template<class T>
Stack<T>::Stack()
{
}
template<class T>
bool Stack<T>::isempty()
{ if (stack.size() == 0)
return true;
else
return false;
}
template<class T>
T Stack<T>::gettop()
{ return stack[stack.size()-1];
}
template<class T>
void Stack<T>::push(T entry)
{
stack.push_back(entry);
}
template<class T>
void Stack<T>::pop()
{
stack.pop_back();
}
int main()
{
Stack<char> 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()!='(')
{
y += S.gettop();
S.pop();
}
S.pop();
}
if(isalpha(s))
{
y += s;
}
index++;
s=x[index];
}
while(!S.isempty())
{
y+=S.gettop();
S.pop();
}
cout<<y;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote