Skip to content

Commit

Permalink
Final Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
AyuBisht committed Apr 29, 2022
1 parent 612c89f commit 4b23e23
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
99 changes: 99 additions & 0 deletions S-Stack/InfixToPrefix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//---------- Infix expression to Prefix ------------//

//#include <iostream>
#include <bits/stdc++.h>
using namespace std;

// to comparare the precedence of operaters
int precedence_comparision(char symbol){

switch(symbol)
{
case '+':
case '-':
return 1;

case '*':
case '/':
return 2;

case '^':
return 3;

default:
return -1;
}
}

string InfixToPrefix(string s){
stack<char> st;
string result="";

//we reverse the string
reverse(s.begin(),s.end());

for(int i=0; i<s.length(); i++){
//reads a character
char symbol = s[i];

//if it is an operand , add to resultant string
if((symbol >='a' && symbol <='z') || (symbol >= 'A' && symbol <= 'Z') || (symbol >= '0' && symbol <= '9')){
result += symbol;
}

// treated as an opening bracket and pushed in the stack
else if(symbol == ')'){
st.push(symbol);
}

//treated as a closing bracket and elements are added to the resultant string and popped from the stack until ')' is encountered
else if(symbol == '('){
while(!st.empty() && st.top() != ')'){
result += st.top();
st.pop();
}
if(!st.empty()){
st.pop();
}
}

//if an operator is encountered, check precedence and push or pop accordingly
else{
while(!st.empty() && (precedence_comparision(st.top()) > precedence_comparision(symbol)) ){
result += st.top();
st.pop();
}
st.push(symbol);
}
}
//pop the rest of the expression from the stack
while(!st.empty()){
result += st.top();
st.pop();
}

reverse(result.begin(), result.end());
return result;
}

// driver program to test InfixtoPrefix function
/*
expression: a^(b*c-d/(e+f))
))f+e(/d-c*b(^a
((f+e)/d-c*b)^a
(fe+/d-c*b)^a
(fe+d/-c*b)^a
(fe+d/-cb*)^a
(fe+d/cb*-)^a
fe+d/cb*-a^
^a-*bc/d+ef
*/
// Main Function
int main(){
string expression;
cout<<"Enter expression : ";
getline (cin, expression);
//string expression ="a^(b*c-d/(e+f))";
cout << InfixToPrefix(expression);
return 0;
}
34 changes: 34 additions & 0 deletions S-Stack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- [How To check parenthesis using Stack ?](#how-to-check-parenthesis-using-stack)
- [Converting decimal number to binary](#converting-decimal-number-to-binary)
- [Stack ADT using linked list](#stack-adt-using-linked-list)
- [Infix to Prefix Notation using stack](#infix-to-prefix-notation-using-stack)

## STACK

Expand Down Expand Up @@ -170,3 +171,36 @@ For example:
- Working with linked list and void pointers is little bit difficult.

![image](https://prepinsta.com/wp-content/uploads/2019/07/ll.png)

## Infix to Prefix Notation using stack

- The conventational method of representing an arithmatic expression is known as infix notation of an expression
- Prefix expression: The operator is placed before the operand in prefix notation .
- Infix: A+B
- Prefix: +AB

Need of prefix Notation :
Although this notation is not very readable by humans ,but proved very useful for compiler designers in generating machine language code for evaluating arithmetic expression .

Algorithm
```
- Time Complexity O(n) , where n is length of expression .
1. Using cpp build a stack.
2. Reverse the infix expression
3. Read each character of Infix expression one by one from left to right.
4. If the character is an operand, add to output.
5. Else If the character is ')', push in stack.
6. Else If the character is '(', pop the stack and output the string upto character ')' .
7. Else, it is a operator , check precedence and push or pop accordingly
1. If the precedence of the checked operator is greater than the precedence of the operator in the stack(or the stack is empty or the stack contains a '(' ), push it.
2. Else, Pop all the operators from the stack which are greater than or equal to in precedence than that of the checked operator. After doing that Push the checked operator to the stack. (If there is a parenthesis while popping then stop there and push the checked operator in the stack.)
7. Repeat steps 4-7 upto the end of Infix expression.
8. pop the remaining expression from the stack until stack is empty
9.reverse the resultant string.
9. print the result string that is the prefix notation.
```
- Its implementation is [here](InfixToPrefix.cpp)

![Infix to postfix conversion](https://prepinsta.com/wp-content/uploads/2020/07/Infix-to-prefix-conversion.webp)

- For more information about infix to postfix conversion [click here](https://www.javatpoint.com/convert-infix-to-prefix-notation)

0 comments on commit 4b23e23

Please sign in to comment.