answersLogoWhite

0


Best Answer

// made by vijay NITJ

#include<iostream.h>

#include<string.h>

#include<stdio.h>

#include<math.h>

int precedence(char);

void main()

{

char q[20];

char p[20];

char stack[20];

int tp=-1,ts=0;

stack[0]='(';

cout<<stack;

int i=0, f=1;

cout<<"enter the prefix expression ";

gets(q);

int l=strlen(q);

q[l]=')';

while(ts!=-1)

{

if(q[i]>=47 && q[i]<=58 q[i]<=65 && q[i]<=90 q[i]<=97 && q[i]>=122)

{

p[++tp]=q[i];

}

else if(q[i]=='(')

stack[++ts]=q[i];

else if(q[i]=='+' q[i]=='-' q[i]=='*' q[i]=='/')

{

if(stack[ts]=='(')

stack[++ts]=q[i];

else

{

while(stack[ts]!='(')

{

int p1=precedence(q[i]);

int p2=precedence(stack[ts]);

if(p2>=p1)

p[++tp]=stack[ts--];

else

{

f=0;

break;

}

}

if(f==0)

stack[++ts]=q[i];

}

}

else if(q[i]==')')

{

while(stack[ts]!='(')

{

p[++tp]=stack[ts--];

}

ts--;

}

i++;

}

for(int j=0; j<=tp;j++)

{

cout<<p[j];

}

//precedence program

int precedence(char a)

{

if(a=='+' a=='-')

return 1;

else if(a=='*' a=='/')

return 2;

}

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

/**

* @(#)ConvterPostfixToinfix.java

* project NO 1

* @version 1.00 2009/5/5 Beta

*/

import java.awt.*;

import javax.swing.*;

public class ConvterPostfixToinfix {

private CharStack Opr;

private String StPostfix="",StInfix;

public ConvterPostfixToinfix(int size) {

Opr=new CharStack(size);

}

public void Postfix(String StInfix)

{

char ch;

if(StInfix.charAt(0)=='-')

{

StPostfix+=StInfix.charAt(0);

StInfix=StInfix.substring(1);

}

else if(StInfix.charAt(0)=='+')

StInfix=StInfix.substring(1);

for (int i = 0; i

{

ch=StInfix.charAt(i);

switch(ch){

case '(':

Opr.Push(ch);

break; // lift arch

case ')':

RightArch();

break;//right arch

case '+':

case '-':

Oprator(ch,1);

break;//op+ or -

case '*':

case '/':

Oprator(ch,2);

break;//op * or /

default :StPostfix+=ch;

break;//number

}

}

while(!Opr.isEmptey()){

StPostfix+=Opr.Pop();

}

JOptionPane.showMessageDialog(null,"After conversion : "+StPostfix);

}//method

private void RightArch()

{

while(!Opr.isEmptey())

{

char local=Opr.Pop();

if(local=='(')

break;

else

StPostfix+=local;

}

}

private void Oprator(char OpIn,int opin)

{

while(!Opr.isEmptey())

{

char Top=Opr.Pop();

if(Top=='(')

{

Opr.Push(Top);

break;

}

else

{

int opTop=(Top=='+'Top=='-')?1:2;

if(opTop

{

Opr.Push(Top);

break;

}

else

StPostfix+=Top;

}//else

}

Opr.Push(OpIn);

}

}

//This Main Method

public static void main (String[] args) {

String StInfix =JOptionPane.showInputDialog("Enter Math Eqution ");

ConvterPostfixToinfix ob=new ConvterPostfixToinfix(StInfix.length());

ob.Postfix(StInfix);

}

// This Stack

public class CharStack {

private int top=-1;

private char []array;

public CharStack(int size) {

array= new char[size];

}

public boolean isEmptey()

{

return (top==-1);

}

public boolean isFull()

{

return (top==array.length-1);

}

public void Push(char o)

{

if(!isFull())

array[++top]=o;

}

public char Pop()

{

return array[top--];

}

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Infix Expression :

Any expression in the standard form like "2*3-4/5" is an Infix(Inorder) expression.

Postfix Expression :

The Postfix(Postorder) form of the above expression is "23*45/-".

Infix to Postfix Conversion :

In normal algebra we use the infix notation like a+b*c. The corresponding postfix notation is abc*+. The algorithm for the conversion is as follows :

  • Scan the Infix string from left to right.
  • Initialise an empty stack.
  • If the scannned character is an operand, add it to the Postfix string. If the scanned character is an operator and if the stack is empty Push the character to stack.
    • If the scanned character is an Operand and the stack is not empty, compare the precedence of the character with the element on top of the stack (topStack). If topStack has higher precedence over the scanned character Pop the stack else Push the scanned character to stack. Repeat this step as long as stack is not empty and topStack has precedence over the character.
    Repeat this step till all the characters are scanned.
  • (After all characters are scanned, we have to add any character that the stack may have to the Postfix string.) If stack is not empty add topStack to Postfix string and Pop the stack. Repeat this step as long as stack is not empty.
  • Return the Postfix string.

Example :

Let us see how the above algorithm will be imlemented using an example.

Infix String : a+b*c-d

Initially the Stack is empty and our Postfix string has no characters. Now, the first character scanned is 'a'. 'a' is added to the Postfix string. The next character scanned is '+'. It being an operator, it is pushed to the stack.

Stack

Postfix String

Next character scanned is 'b' which will be placed in the Postfix string. Next character is '*' which is an operator. Now, the top element of the stack is '+' which has lower precedence than '*', so '*' will be pushed to the stack.

Stack

Postfix String

The next character is 'c' which is placed in the Postfix string. Next character scanned is '-'. The topmost character in the stack is '*' which has a higher precedence than '-'. Thus '*' will be popped out from the stack and added to the Postfix string. Even now the stack is not empty. Now the topmost element of the stack is '+' which has equal priority to '-'. So pop the '+' from the stack and add it to the Postfix string. The '-' will be pushed to the stack.

Stack

Postfix String

Next character is 'd' which is added to Postfix string. Now all characters have been scanned so we must pop the remaining elements from the stack and add it to the Postfix string. At this stage we have only a '-' in the stack. It is popped out and added to the Postfix string. So, after all characters are scanned, this is how the stack and Postfix string will be :

Stack

Postfix String

End result :

  • Infix String : a+b*c-d
  • Postfix String : abc*+d-
This answer is:
User Avatar

User Avatar

Wiki User

11y ago

#include<stdio.h>

#include<ctype.h>

#include<string.h>

#define TRUE 1

#define FALSE 0

#define MAX 100

typedef struct stack {

int top;

union array {

char items[MAX][MAX];

int intitems[MAX];

} a;

} s;

void Post_In(void); /* Function Prototypes */

int convertpostfix(char postfix[], char infix[][MAX]);

void Pre_In(void);

void Reduce(void);

int convertprefix(char prefix[], char infix[][MAX]);

void remove_superfluous_parantheses(char infix[], char reducedinfix[]);

void push(s *pt, char operand[]);

void pop(s *pt, char operand[]);

int empty(s *pt);

int isoperand(char symb);

int prcd(char symb);

int popindex(s *pt);

void pushindex(s *pt, int index);

void removeindices(s *pt1, s *pt2);

void formexpression(char op1[], char op2[], char symb, char operand[]);

/***************************** Postfix To Infix ****************************/

void Post_In(void) {

int i, n;

char postfix[MAX], *string, reducedinfix[MAX], infix[MAX][MAX];

for (i = 0; (postfix[i] = getchar()) != '\n'; ++i)

;

postfix[i] = '\0';

n = convertpostfix(postfix, infix);

for (i = 0; i < n; ++i)

strcat(string, infix[i]);

remove_superfluous_parantheses(string, reducedinfix);

return;

}

int convertpostfix(char postfix[], char infix[][MAX]) {

int k;

char symb, operand[MAX], op1[MAX], op2[MAX];

s opndstack;

opndstack.top = -1;

for (k = 0; (symb = postfix[k]) != '\0'; ++k)

if (isoperand(symb)) {

/* converting symb to a string */

operand[0] = symb;

operand[1] = '\0';

/* pushing the operand into the stack */

push(&opndstack, operand);

} else {

pop(&opndstack, op2);

pop(&opndstack, op1);

formexpression(op1, op2, symb, operand);

/* pushing the infix expression into the stack */

push(&opndstack, operand);

}

/* copying the final infix expression from the stack into the infix array */

for (k = 0; !empty(&opndstack); ++k)

pop(&opndstack, infix[k]);

return (k); /* k is the count of the no of operands in the stack */

}

/****************************** Prefix To Infix *****************************/

void Pre_In(void) {

int i, n;

char prefix[MAX], *string, reducedinfix[MAX], infix[MAX][MAX];

for (i = 0; (prefix[i] = getchar()) != '\n'; ++i)

;

prefix[i] = '\0';

n = convertprefix(prefix, infix);

for (i = 0; i < n; ++i)

strcat(string, infix[i]);

remove_superfluous_parantheses(string, reducedinfix);

return;

}

int convertprefix(char prefix[], char infix[][MAX]) {

int k, length;

char symb, operand[MAX], op1[MAX], op2[MAX];

s opndstack;

opndstack.top = -1;

for (k = 0; prefix[k] != '\0'; ++k)

;

length = k - 1;

for (k = length; k >= 0; --k)

if (isoperand(symb = prefix[k])) {

/* converting symb to a string */

operand[0] = symb;

operand[1] = '\0';

/* pushing the operand into the stack */

push(&opndstack, operand);

} else {

pop(&opndstack, op1);

pop(&opndstack, op2);

formexpression(op1, op2, symb, operand);

/*pushing the infix expression into the stack */

push(&opndstack, operand);

}

/* copying the final infix expression from the stack into the infix array */

for (k = 0; !empty(&opndstack); ++k)

pop(&opndstack, infix[k]);

return (k);

}

/********************* Removal of Superfluous Parantheses ******************/

void Reduce(void) {

int i;

char infix[MAX], reducedinfix[MAX];

for (i = 0; (infix[i] = getchar()) != '\n'; ++i)

;

infix[i] = '\0';

remove_superfluous_parantheses(infix, reducedinfix);

return;

}

/***************************** Common Functions ****************************/

void remove_superfluous_parantheses(char infix[], char reducedinfix[]) {

/* neededleft and neededright arrays will contain the indices of only the required parantheses */

int i, j, k, lcntr, rcntr, present, hold, neededleft[MAX], neededright[MAX];

int prcdlast, prcdfirst;

char symb, lastoperator, firstoperator;

s leftstack, rightstack;

leftstack.top = rightstack.top = -1;

lcntr = rcntr = 0;

for (i = 0; (symb = infix[i]) != '\0'; ++i)

if (symb -1 ? TRUE : FALSE);

}

int isoperand(char symb) {

return (isalpha(symb) ? TRUE : FALSE);

}

/***************************************************************************/

void main() // main function

{

Post_In();

Pre_In();

Reduce();

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

#include <stdio.h>

#include <stdlib.h>

#define max 20

typedef enum status_code{failure,success}status_code;

typedef struct node_tag

{

char operator;

struct node_tag *next;

}node;

typedef struct queue

{

struct node_tag *front;

struct node_tag *rear;

}queue;

void initialise_queue(queue *qptr)

{

qptr->front=NULL;

qptr->rear=NULL;

}

status_code insert_node(node **lptr,char val)

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Write and explain a 'C' function to convert the given infix expression to postfix expressionWrite and explain a 'C' function to convert the given infix expression to postfix expression

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a java program that implements stack ADT converts infix expression to postfix form evaluates the postfix expression?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the difference between prefix and postfix increment operator in c plus plus?

Both the prefix and the postfix increment operators increment the operand. The difference is what is the value of the expression during the evaluation of the expression. In the prefix form, the value is already incremented. In the postfix form, it is not. int a = 1; int b = ++a; // both a and b are now equal to 2 int a = 1; int b = a++; // a is equal to 2 and b is equal to 1


Why there is only 2 plus in c plus plus languagewhy not 3 or 4 plus?

I assume by 2 plus you really mean ++. This is the increment operator which is used to increment the operand. If placed before the operand, the operator evaluates the incremented operand (prefix increment). If placed after the operand, the operator evaluates the non-incremented operand (postfix increment). +++ and ++++ are meaningless but are assumed to mean incrementing an increment. If you wish to increment an increment, you must use the compound expression ++(++) or (++)++. Thus for the variable x, prefix incrementing twice would be achieved with ++(++x), while postfix incrementing twice would be achieved with (x++)++. You can also mix the two, such as ++(x++) or (++x)++, both of which would increment x twice but would evaluate the increment of x. If postfix increment is not a requirement, it would be much easier to use the compound expression x+=n, where n is the amount you wish to increment. This is the same as saying x=x+n.


Minimum size of stack to evaluate postfix expression?

Scan the postfix expression from left to right and count the number of values and the number of operators. The maximum value of their difference is the required stack size. Eg: 1 2 3 + 4 + * 1 2 3 2 3 2 1 The maximum is 3.


What is prefix expression?

Example: prefix: * 2 + 3 4 infix: 2 * (3+4) postfix: 2 3 4 + *


Infix to postfix C?

Infix Expression :Any expression in the standard form like "2*3-4/5" is an Infix(Inorder) expression.Postfix Expression :The Postfix(Postorder) form of the above expression is "23*45/-".Infix to Postfix Conversion :In normal algebra we use the infix notation like a+b*c. The corresponding postfix notation is abc*+. The algorithm for the conversion is as follows :Scan the Infix string from left to right.Initialise an empty stack.If the scannned character is an operand, add it to the Postfix string. If the scanned character is an operator and if the stack is empty Push the character to stack. If the scanned character is an Operand and the stack is not empty, compare the precedence of the character with the element on top of the stack (topStack). If topStack has higher precedence over the scanned character Pop the stack else Push the scanned character to stack. Repeat this step as long as stack is not empty and topStack has precedence over the character.Repeat this step till all the characters are scanned.(After all characters are scanned, we have to add any character that the stack may have to the Postfix string.) If stack is not empty add topStack to Postfix string and Pop the stack. Repeat this step as long as stack is not empty.Return the Postfix string.Example :Let us see how the above algorithm will be imlemented using an example.Infix String : a+b*c-dInitially the Stack is empty and our Postfix string has no characters. Now, the first character scanned is 'a'. 'a' is added to the Postfix string. The next character scanned is '+'. It being an operator, it is pushed to the stack.StackPostfix StringNext character scanned is 'b' which will be placed in the Postfix string. Next character is '*' which is an operator. Now, the top element of the stack is '+' which has lower precedence than '*', so '*' will be pushed to the stack.StackPostfix StringThe next character is 'c' which is placed in the Postfix string. Next character scanned is '-'. The topmost character in the stack is '*' which has a higher precedence than '-'. Thus '*' will be popped out from the stack and added to the Postfix string. Even now the stack is not empty. Now the topmost element of the stack is '+' which has equal priority to '-'. So pop the '+' from the stack and add it to the Postfix string. The '-' will be pushed to the stack.StackPostfix StringNext character is 'd' which is added to Postfix string. Now all characters have been scanned so we must pop the remaining elements from the stack and add it to the Postfix string. At this stage we have only a '-' in the stack. It is popped out and added to the Postfix string. So, after all characters are scanned, this is how the stack and Postfix string will be :StackPostfix StringEnd result :Infix String : a+b*c-dPostfix String : abc*+d-

Related questions

Why you need convert a expression into postfix expression?

You convert an (infix) expression into a postfix expression as part of the process of generating code to evaluate that expression.


What is the difference between prefix and postfix increment operator in c plus plus?

Both the prefix and the postfix increment operators increment the operand. The difference is what is the value of the expression during the evaluation of the expression. In the prefix form, the value is already incremented. In the postfix form, it is not. int a = 1; int b = ++a; // both a and b are now equal to 2 int a = 1; int b = a++; // a is equal to 2 and b is equal to 1


What is a 'post fix expression' in java programming?

Postfix expressions are expressions where the operator is at the end of the expression. These include the "++" (increment) and "--" (decrement) operators. Most Java expressions use in-fix notation (e.g. "a + b") but the increment and decrement operators can be postfix ("e.g. "a++" to increment variable a) or even prefix (e.g. "++a").


Why there is only 2 plus in c plus plus languagewhy not 3 or 4 plus?

I assume by 2 plus you really mean ++. This is the increment operator which is used to increment the operand. If placed before the operand, the operator evaluates the incremented operand (prefix increment). If placed after the operand, the operator evaluates the non-incremented operand (postfix increment). +++ and ++++ are meaningless but are assumed to mean incrementing an increment. If you wish to increment an increment, you must use the compound expression ++(++) or (++)++. Thus for the variable x, prefix incrementing twice would be achieved with ++(++x), while postfix incrementing twice would be achieved with (x++)++. You can also mix the two, such as ++(x++) or (++x)++, both of which would increment x twice but would evaluate the increment of x. If postfix increment is not a requirement, it would be much easier to use the compound expression x+=n, where n is the amount you wish to increment. This is the same as saying x=x+n.


Evaluate a post fix expression?

Okay, here is a postfix expression: 3 4 * 5 6 * + the evaluation: 3*4 + 5*6 12 + 30 42


Minimum size of stack to evaluate postfix expression?

Scan the postfix expression from left to right and count the number of values and the number of operators. The maximum value of their difference is the required stack size. Eg: 1 2 3 + 4 + * 1 2 3 2 3 2 1 The maximum is 3.


What is prefix expression?

Example: prefix: * 2 + 3 4 infix: 2 * (3+4) postfix: 2 3 4 + *


Sample program of postfix to infix?

You can convert from postfix to infix through the use of stacks. Consider the following expression conversion:54+67*+ -> ((5+4)+(6*7))The way this can be achieved is that whenever you encounter an operator, pop the last two expressions and join them using the operator. Remember to include the open braces before the first expression and a close braces after the second expression. Check the given link below for the program:


Write an algorithm in c to convert an infix expression to a postfix expressionexecute your algorithm with the following infix expression as your input. m nk pgh a bc?

An algorithm can not be written with the following infix expression without knowing what the expression is. Once this information is included a person will be able to know how to write the algorithm.


Infix to postfix C?

Infix Expression :Any expression in the standard form like "2*3-4/5" is an Infix(Inorder) expression.Postfix Expression :The Postfix(Postorder) form of the above expression is "23*45/-".Infix to Postfix Conversion :In normal algebra we use the infix notation like a+b*c. The corresponding postfix notation is abc*+. The algorithm for the conversion is as follows :Scan the Infix string from left to right.Initialise an empty stack.If the scannned character is an operand, add it to the Postfix string. If the scanned character is an operator and if the stack is empty Push the character to stack. If the scanned character is an Operand and the stack is not empty, compare the precedence of the character with the element on top of the stack (topStack). If topStack has higher precedence over the scanned character Pop the stack else Push the scanned character to stack. Repeat this step as long as stack is not empty and topStack has precedence over the character.Repeat this step till all the characters are scanned.(After all characters are scanned, we have to add any character that the stack may have to the Postfix string.) If stack is not empty add topStack to Postfix string and Pop the stack. Repeat this step as long as stack is not empty.Return the Postfix string.Example :Let us see how the above algorithm will be imlemented using an example.Infix String : a+b*c-dInitially the Stack is empty and our Postfix string has no characters. Now, the first character scanned is 'a'. 'a' is added to the Postfix string. The next character scanned is '+'. It being an operator, it is pushed to the stack.StackPostfix StringNext character scanned is 'b' which will be placed in the Postfix string. Next character is '*' which is an operator. Now, the top element of the stack is '+' which has lower precedence than '*', so '*' will be pushed to the stack.StackPostfix StringThe next character is 'c' which is placed in the Postfix string. Next character scanned is '-'. The topmost character in the stack is '*' which has a higher precedence than '-'. Thus '*' will be popped out from the stack and added to the Postfix string. Even now the stack is not empty. Now the topmost element of the stack is '+' which has equal priority to '-'. So pop the '+' from the stack and add it to the Postfix string. The '-' will be pushed to the stack.StackPostfix StringNext character is 'd' which is added to Postfix string. Now all characters have been scanned so we must pop the remaining elements from the stack and add it to the Postfix string. At this stage we have only a '-' in the stack. It is popped out and added to the Postfix string. So, after all characters are scanned, this is how the stack and Postfix string will be :StackPostfix StringEnd result :Infix String : a+b*c-dPostfix String : abc*+d-


What is a postfix expression?

The operator comes after the operands. E.g., 1 + 2 becomes 1 2 + while 1 + 2 + 3 becomes 1 2 3 + +.


Why do compilers convert infix expressions to postfix?

people almost exclusively use infix notation to write mathematical expressions, computer languages almost exclusively allow programmers to use infix notation. However, if a compiler allowed infix expressions into the binary code used in the compiled version of a program, the resulting code would be larger than needed and very inefficient. Because of this, compilers convert infix expressions into postfix notation expressions, which have a much simpler set of rules for expression evaluation. Postfix notation gets its name from the fact that operators in a postfix expression follow the operands that they specify an operation on. Here are some examples of equivalent infix and postfix expressions Infix Notation Postfix Notation 2 + 3 2 3 + 2 + 3 * 6 3 6 * 2 + (2 + 3) * 6 2 3 + 6 * A / (B * C) + D * E - A - C A B C * / D E * + A C * - Where as infix notation expressions need a long list or rules for evaluation, postfix expressions need very few.