0% found this document useful (0 votes)
22 views5 pages

Stacks

Uploaded by

theunknownboy947
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views5 pages

Stacks

Uploaded by

theunknownboy947
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Problem Statement: Infix to Postfix Conversion:

The problem is to transform the infix expression, that is an expression in which the operators lie
in between the operands, to the postfix expression or the reverse Polish notation in which the
operators are placed after the operands they are working on. Italizing makes the computation of
expressions on computers employing stack data structures quite easier.

Objectives:
Understand Infix and Postfix Notations: The first one represents computing formats where we
need to understand the difference between infix and postfix notations: The second one shows
us why computer science uses postfix notation.

Implement Stack Operations: Make appropriate utilization of the stack operations such as push
and pop during the conversion.

Apply Operator Precedence: To convert an infix expression to a postfix expression we can make
the use of operator precedence rules.

Write and Test the Algorithm: Write a C++ code for the above algorithm and run the code with
different infix expressions to check the correctness.

Algorithm:
#include<iostream>

using namespace std;

string stack;
int top = -1;
void push(char c){
stack[++top]= c;
}

char pop(){
if(top==-1)
return -1;
else{
return stack[top--];
}
}

int precedence(char x){


if(x == '('){
return 0;
}
else if(x == '+' || x == '-'){
return 1;
}
else if(x == '*' || x == '/'){
return 2;
}
else if(x == '^'){
return 3;
}

int main(){

string infix;
cout << "Enter an infix expression: ";
getline(cin, infix);
char y;
char z;

cout << "The postfix expression is: ";


int i = 0;
while(i < infix.length()){
if(infix[i] == '(')
push(infix[i]);

else if(infix[i] == ')'){


while((y = pop()) != '('){
//y = pop();
cout << y;
// top--;
}
}
else if(infix[i] == '+' || infix[i] == '-' || infix[i] == '*' ||
infix[i] == '/'){
while(precedence(stack[top]) >= precedence(infix[i])){
z = pop();
cout << z;
}
push(infix[i]);
}

else{
cout << infix[i];
}
i++;
}
while(top!= -1){
z = pop();
cout << z;
}
return 0;
}

Problem Statement: Postfix Evaluation


The given expression is in postfix notation (or Reverse Polish Notation) and we need to carry out
enumeration to get a value. It is called postfix notation because the operators follow the
operands; therefore, its evaluation using the stack data structure is straightforward.

Objectives:
Understand Infix and Postfix Notations: The first one represents computing formats where we
need to understand the difference between infix and postfix notations. The second one shows
you why computer science uses postfix notation.

Implement Stack Operations: Make appropriate utilization of the stack operations such as push
and pop during the conversion.

Apply Operator Precedence: To convert an infix expression to a postfix expression we can make
the use of operator precedence rules.

Write and Test the Algorithm: Write a C++ code for the above algorithm and run the code with
different infix expressions to check the correctness.
Algorithm:
#include <bits/stdc++.h>
#include <iostream>

using namespace std;

int operation(string str);

int main()
{

string s;

cout << "Enter the expression:" << endl;

getline(cin, s);
// cout << s << endl;
cout << operation(s);
return 0;
}

int operation(string str)


{

stack<int> result;
string temp = "";
for (int i = 0; i <= str.size(); i++)
{

if (i < str.size() && str[i] != ' ' && !(str[i] == '+' || str[i] == '-'
|| str[i] == '*' || str[i] == '/'))
{
temp.push_back(str[i]);
}

else
{
if (!temp.empty())
{

int m = 0;
for (int i = 0; i < temp.size(); i++)
{
m = m * 10 + (temp[i] - '0');
}
result.push(m);
m = 0;
temp = "";
}
}

if (str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/')


{
int a = result.top();
result.pop();
int b = result.top();
result.pop();

int x = max(a, b);


int y = min(a, b);

switch (str[i])
{
case '+':
result.push(a + b);
break;
case '-':
result.push(x - y);
break;
case '*':
result.push(a * b);
break;
case '/':
result.push(x / y);
break;
}
}
}

int answer = result.top();


return answer;
}

You might also like