Sunday 20 August 2017

RPN to INFIX conversion, Stack



RPN to INFIX conversion……
ALGORITHM
C++ CODE
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
////////////////////////////////////////////////
void post_infix(char exp[]);
////////////////////////////////////////////////
class Stack
{
       char arr[10][15];
       int top;
public:
       Stack() :top(-1){}
       void push(char z[15])
       {
              strcpy_s(arr[++top], z);
       }
       char* pop()
       {
              return arr[top--];
       }
       bool isempty()
       {
              if (top == -1)
                     return true;
              else
                     return false;
       }
       char* peak()
       {
              return arr[top];
       }
};
////////////////////////////////////////////////
int main()
{
       post_infix("ab+cd-*");
       system("pause");
       return 0;
}
////////////////////////////////////////////////
int lenght(char expe[50])
{
       return strlen(expe);
}
////////////////////////////////////////////////
void post_infix(char exp[50])
{
       int i, j;
       char*f = NULL, *d;
       char c[15] = {NULL},C[50] = {NULL},B[50][50] = {NULL};
       Stack s;
       for (i = 0; i<strlen(exp); i++)
       {
              d = NULL;
              *c = exp[i];
              s.push(c);
              if (exp[i] == '+' || exp[i] == '/' || exp[i] == '*' || exp[i] == '-')
              {
                     j = 0;
                     do
                     {
                           f = s.pop();
                           strcpy_s(B[j], f);
                           j++;
                     }
                     while (j != 3);
                    
                     strcpy_s(d,*B);            yahan problem ho rahi hai
                           OR
                     for(j = 0; j<strlen(*B); j++)
                           d[j] = *B[j];
                     strcpy_s(*B, *(B + 2));
                     strcat_s(*B, d);
                     strcat_s(*B, *(B + 1));

                     s.push(*B);
              }
       }
       cout<<s.peak();
}

0 comments: