How To Convert a Postfix Expression into an Infix Expression in C++

来源:转载

Hi,Postfix notation is also known as Reverse Polish Notation (RPN) in which every operator follows all of its operands.Developer : ijaz Rs khAn & Bilal tahir

Email : [email protected]

Compiler : Microsoft Visual Studio 2013

Comp. Date: 1-11-2015

Convert a Postfix Expression into an Infix Expression in C++ Source Code # include <iostream.h> // for input output standard # include <string.h>// forstring value#include <stdlib.h># include <conio.h>// for clear screenint top=-1; char Stack[25][100]={NULL}; // void push(constchar *); // functionsconstchar* pop( ); void postfix_to_infix(constchar *); int main( ) //main function{clrscr( ); // clear screen char Postfix_expression[100]={NULL}; cout<<"/n/n/t Insert Postfix Expression : ";cin.getline(Postfix_expression,80); postfix_to_infix(Postfix_expression); getch( );return 0;}{if(top==24)cout<<"Error : Stack is fully now."<<endl; else{top++;strcpy(Stack[top],Symbol);}} {char Symbol[100]={NULL}; if(top==-1)cout<<"Error: Stack is empty now."<<endl;else{strcpy(Symbol,Stack[top]);strset(Stack[top],NULL);top--; } return Symbol;} {char Infix_expression[100]={NULL};char Postfix_expression[100]={NULL}; strcpy(Postfix_expression,Postfix);strcat(Postfix_expression,"="); int count=0; char Symbol_scanned[5]={NULL}; do{ Symbol_scanned[0]=Postfix_expression[count]; if(Symbol_scanned[0]=='/' || Symbol_scanned[0]=='*' ||Symbol_scanned[0]=='-' || Symbol_scanned[0]=='+' ||Symbol_scanned[0]=='^' ){char Value_1[100]={NULL};char Value_2[100]={NULL};char Result[100]={NULL}; strcpy(Value_1,pop( ));strcpy(Value_2,pop( )); if(Infix_expression[(count+1)]!='=')strcpy(Result,"("); strcat(Result,Value_2);strcat(Result,Symbol_scanned);strcat(Result,Value_1); if(Infix_expression[(count+1)]!='=')strcat(Result,")"); push(Result);} elsepush(Symbol_scanned); count++;}while(Postfix_expression[count]!='='); strset(Infix_expression,NULL);strcpy(Infix_expression,pop( )); cout<<"/n/n/t Infix Expression is : "<<Infix_expression;} Copy and paste the following C++ program in test.cpp file and compile and run this program.if you Face any problem Freely contact us.Give your Feed Back.Thank you.



分享给朋友:
您可能感兴趣的文章:
随机阅读: