Git Product home page Git Product logo

prefix-expression-to-infix's Introduction

prefix-expression-to-infix

prefix expression to infix

what is prefix expression ?

Prefix : An expression is called the prefix expression if the operator appears in the expression before the operands. Simply of the form (operator operand1 operand2). Example : *+AB-CD (Infix : (A+B) * (C-D) )

now look at the code to find it how can you convert it :)

#include <iostream> 
using namespace std; 

class Stack {
   protected:
      int top=-1;
      int MAX = 1000;
      string stack[1000];
   
   public:
      void push(string val) {
        if(top>=MAX-1)
            cout<<"Stack Overflow"<<endl; 
        else {
            top++;
            stack[top]=val;
        }
      }

      void pop() {
        if(top<=-1) {
            cout<<"Stack Underflow"<<endl;
        } else {
            top--;
        }
      }

      void display() {
        if(top>=0) {
            cout<<"Stack elements are:";
            for(int i=top; i>=0; i--)
                cout<<stack[i]<<" ";
                cout<<endl;
        } else
            cout<<"Stack is empty";
      }

      string get(int i) {
          return stack[i];
      }

      int getMax() {
          return MAX;
      }

      int getTop() {
          return top;
      }

      string getTopValue() {
          return stack[top];
      }

      int getSize() {
          return (top + 1);
      }

      int isEmpty() {
          if(top<=-1) return 1;
          return 0;
      }
};
  
bool isOperator(char x) { 
  switch (x) { 
  case '+': 
  case '-': 
  case '/': 
  case '*': 
  case '^': 
    return true; 
  } 
  return false; 
} 
  
string preToInfix(string pre_exp) { 
  Stack s; 
  int length = pre_exp.size(); 
  for (int i = length - 1; i >= 0; i--) { 
    if (isOperator(pre_exp[i])) { 
      string op1 = s.getTopValue();
      s.pop(); 
      string op2 = s.getTopValue();
      s.pop(); 
      string temp = "(" + op1 + pre_exp[i] + op2 + ")"; 
      s.push(temp);
    } 
    else { 
      s.push(string(1, pre_exp[i])); 
    } 
  } 
  return s.getTopValue(); 
} 
  
int main() { 
  string input; 
  cout << "Type a prefix expression: ";
  cin >> input;
  cout << "Infix is : " << preToInfix(input)<<endl; 
  return 0; 
} 

prefix-expression-to-infix's People

Contributors

azibom avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.