Professional Documents
Culture Documents
Infix To Postfix
Infix To Postfix
Assignment 1
BS-SE-18
Section A
Name Waleed Qadeer
Roll no. 18094198-022
Submitted TO Prof.Bushra Fareed
Infix to Postfix
#include<iostream>
#include<stack>
#include<string>
// Function to verify whether a character is alphanumeric chanaracter (letter or numeric digit) or not.
int main()
string expression;
getline(cin,expression);
cout<<"Output = "<<postfix<<"\n";
stack<char> S;
// If character is operator, pop two elements from stack, perform operation and push the result back.
else if(IsOperator(expression[i]))
postfix+= S.top();
S.pop();
S.push(expression[i]);
else if(IsOperand(expression[i]))
postfix +=expression[i];
S.push(expression[i]);
postfix += S.top();
S.pop();
S.pop();
while(!S.empty()) {
postfix += S.top();
S.pop();
return postfix;
bool IsOperand(char C)
return false;
bool IsOperator(char C)
return true;
return false;
return false;
// Function to get weight of an operator. An operator with higher weight will have higher precedence.
switch(op)
{
case '+':
case '-':
weight = 1;
case '*':
case '/':
weight = 2;
case '$':
weight = 3;
return weight;
// If operators have equal precedence, return true if they are left associative.
if(op1Weight == op2Weight)
Postfix to Prefix
#include <iostream>
#include <cstring>
#include <stack>
#include <algorithm>
return true;
else
return false;
int main()
stack<char> stk;
int j=0,len;
cin>>postfix;
len = strlen(postfix);
for(int i=len-1;i>=0;i--)
if(isOperator(postfix[i]))
stk.push(postfix[i]);
else
prefix[j++] = postfix[i];
stk.pop();
prefix[j++] = stk.top();
stk.pop();
stk.push(flag);
prefix[j] = 0;
reverse(prefix, prefix + len);
return 0;
Prefix to Postfix
#include <iostream>
#include <fstream>
#include <string>
#include <stack>
#include <stdlib.h>
int main()
ofstream outfile;
outfile.open("Postfix.txt", ios::out);
stack<char> operators;
stack<char> flags;
string line;
string outputLine;
getline(myfile, line); //read the next line of file and store into ‘line’
//strcat(outputLine, symbol);
//outputLine.append(symbol);
//strcat(outputLine, operators.top());
//outputLine.append(operators.top());
flags.push(1);
}//end of for
if(!operators.empty() || (!flags.empty()) )
outfile << "SOMETHING WENT WRONG. Prob incorrect input" << endl;
else
outfile.close();
}//end of main
bool isOperator(char symbol) //definition of isOperator
return true;
else
return false;
Postfix to infix
#include<iostream>
#include<cstring>
struct node
char data ;
node *next ;
};
class stack
private :
node *head;
public :
stack()
head = NULL;
void push(char a)
temp ->data = a ;
head = temp ;
}
char pop()
head = temp->next ;
char a = temp->data ;
delete temp;
return a;
char see_top()
if(is_empty())
return '0' ;
return (temp->data);
int is_empty()
if(head == NULL)
return 1;
else
return 0 ;
};
int is_digit(char a)
return 1;
else
return 0;
}
int is_operand(char a)
switch(a)
case '+' :
case '-' :
case '*' :
default : return 0;
int i=0;
stack b ;
for(int j=0;j<l;j++)
b.push(a[j]);
while(!b.is_empty())
a[i++]=b.pop();
return ;
int main()
int i , l , j=0;
stack s ;
cin.getline(exp,100);
l=strlen(exp);
reverse(exp,l);
for(i=0;i<l;i++)
if(is_digit(exp[i]))
infix[j++] = exp[i];
if(i==l-1)
break ;
infix[j++] = s.pop() ;
else
s.push(exp[i]);
while(!s.is_empty())
infix[j++] =s.pop();
reverse(infix,l);
for(i=0;i<l;i++)
return 0;
Recursion
1. #include <iostream>
2. using namespace std;
3.
4. int factorial(int);
5.
6. int main()
7. {
8. int n;
9. cout<<"Enter a number to find factorial: ";
10. cin >> n;
11. cout << "Factorial of " << n <<" = " << factorial(n);
12. return 0;
13. }
14.
15. int factorial(int n)
16. {
17. if (n > 1)
18. {
19. return n*factorial(n-1);
20. }
21. else
22. {
23. return 1;
24. }
25. }