C++ program help 1st photo is stack class used for the assignment. 2nd photo are
ID: 3880438 • Letter: C
Question
C++ program help
1st photo is stack class used for the assignment.
2nd photo are the instructions for the program. 1-7 are the instructions for writing the algorithm to convert from infix to postfix expression. This is what I'm having trouble with.
#include #include using namespace std; template class Stack public: Stack;//creates the stack bool isempty); 1/ 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: vectorcT> stack I/ Stack templatectypename T> Stack::Stack() templatectypename bool StackcT>:zisempty) ( if (stack.size)e return true; se return false; templatectypename T T StackcT>::gettopO ( return stack stack.size)-11 templatectypenane T void Stackepush(T entry) stack.push back(entry) templatectypenase T void StackcTpop) stack,pop.backO O 09 Type here to searchExplanation / Answer
code:
#define ll long long
#define pb push_back
#define mk make_pair
#define f first
#define se second
#include<bits/stdc++.h>
using namespace std;
int main()
{
stack<char>s;
string ss,y="";
cin>>ss;
ss=ss+']' ;// as mentioned in question
s.push('[');
ll n=ss.length(); // finding length of the string
for(ll i=0;i<n;i++)
{
if(ss[i] == '(')
{
s.push('(');
}
else if(ss[i] =='+' || ss[i] == '-' || ss[i] == '*' || ss[i] == '/') // checking the opearor and following the algorithm
{
char f=s.top();
s.pop();
if(f!='(') // checking the corner cases so that ( should n't appear again;
{
y=y+f; // ading it to y string
}
s.push(ss[i]);
}
else if(ss[i]==')')
{
while(s.top()!='(')
{
char f=s.top();
s.pop();
y=y+f;
}
}
else if(ss[i]==']')
{
while(s.top()!='[')
{
char f=s.top();
s.pop();
if(f!='(') // checking the corner cases so that ( should n't appear again;
{
y=y+f;
}
}
s.pop();
}
else
{
s.push(ss[i]);
}
}
cout<<y<<endl;
}
output:
infix:(A+B)*C(D/J(J+D))
postfix:AB+DJD+J/C*
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.