answersLogoWhite

0

Write a c Program to convert postfix to prefix?

Updated: 8/17/2019
User Avatar

Wiki User

11y ago

Best Answer

#include #include char opnds[50][80],oprs[50]; int topr=-1,topd=-1; pushd(char *opnd) { strcpy(opnds[++topd],opnd); } char *popd() { return(opnds[topd--]); } pushr(char opr) { oprs[++topr]=opr; } char popr() { return(oprs[topr--]); } int empty(int t) { if( t == 0) return(1); return(0); } main() { char prfx[50],ch,str[50],opnd1[50],opnd2[50],opr[2]; int i=0,k=0,opndcnt=0; gets(prfx); printf(" Given Prefix Expression : %s\n",prfx); while( (ch=prfx[i++]) != '\0') { if(isalnum(ch)) { str[0]=ch; str[1]='\0'; pushd(str); opndcnt++; if(opndcnt >= 2) { strcpy(opnd2,popd()); strcpy(opnd1,popd()); strcpy(str,opnd1); strcat(str,opnd2); ch=popr(); opr[0]=ch;opr[1]='\0'; strcat(str,opr); pushd(str); opndcnt-=1; } } else { pushr(ch); if(opndcnt==1)opndcnt=0; /* operator followed by single operand*/ } } if(!empty(topd)) { strcpy(opnd2,popd()); strcpy(opnd1,popd()); strcpy(str,opnd1); strcat(str,opnd2); ch=popr(); opr[0]=ch;opr[1]='\0'; strcat(str,opr); pushd(str); } printf(" Postfix Expression: "); puts(opnds[topd]); }

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a c Program to convert postfix to prefix?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Write a program to convert a 2 digit BCD number into hexadecimal number?

Write a program to convert a 2-digit BCD number into hexadecimal


2 Write a program to convert a 2-digit BCD number into hexadecimal?

WRITE A PROGRAM TO CONVERT A 2-DIGIT bcd NUMBER INTO HEXADECIMAL


How do you write a c program to convert binary to decimal by using while statement?

write a c++ program to convert binary number to decimal number by using while statement


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.


How do you write program to convert meter to kilometer in c plus plus?

Divide it by 1000.


How do infix notation and postfix notation differ?

It's simply a matter of where the operators are placed in relation to their operands: infix: X + Y prefix: + X Y postfix: X Y + All of the above are equivalent. Prefix notation is also known as Polish notation, hence postfix is also known as reverse Polish notation. Given the infix equation A * B + C / D, the order of evaluation is always parenthesis, orders, divide/multiply, add/subtract (PODMAS), thus we must multiply A by B first, then divide C by D, and finally add the two results together. If we wish to perform the addition first, then we must re-write the equation with parenthesis: A * (B + C) / D. With postfix and prefix notation, operator precedence becomes superfluous because we always evaluate these expressions in left-to-right order: Infix A * B + C / D becomes postfix A B * C D / + or prefix / * A + B C D Infix A * (B + C) / D becomes postfix A B C + * D / or prefix + * A B / C D When we eliminate operator precedence with postfix or prefix notation, we greatly simplify the algorithm required to evaluate complex expressions. For example, given the postfix expression A B C + * D /, we simply read the symbols one at a time, placing them on a stack, until we encounter an operator. We then pop the first two elements off the stack, perform the operation, and then pop the result back on the stack. We repeat this process until there are no more symbols left, at which point the stack holds just one value: the result. With prefix notation, we place the operators on the stack instead of the operands. When we read the first operand we simply store it in an accumulator. We continue pushing operators onto the stack until we encounter the second operand, at which point we can pop the first operator off the stack, perform the operation and update the accumulator. We repeat this process until there are no symbols left, at which point the accumulator holds the final result. Note that when presented with an infix expression, a machine has to convert the expression to the equivalent prefix or postfix expression before it can be evaluated. By eliminating this conversion process, computation by machine can be performed with much greater speed.


Write a C program to convert hexadecimal number into decimal number?

pongada punda vayanungala ..................


Write a Program to convert decimal number into hexadecimal no without using function and string?

This is not a question.


Write a program to convert stack into queue using c language?

In order to write a program to convert stack into queue using c language you must be able to identify the proper program. Having a special certification in programing will be beneficial as well to make sure you recognize the proper queues for the programs.


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.


What is SAP give you the brief solution?

Write a program to convert a 2-digit BCD number into hexadecimal


How do you write a program in C to convert capital letters into small letters?

Use the tolower() function in the C standard library.