Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Im trying to write a program to convert an infix expression into a prefix expres

ID: 3628994 • Letter: I

Question

Im trying to write a program to convert an infix expression into a prefix expression. I can not get the program run properly. #ifndef INFIX_H #define INFIX_H #include <string> #include<iostream> //#include<stack> using namespace std; //# define MAX 50 /** The class Infix represent infix expression.*/ class Infix { public: // Initaialize infix void Initialize (void); // Get SetExpression void SetExpression (char *str); //pops an operator from the stack char PopFromStack ( void); /* adds operator to the stack */ void PushOnStack (char c ); /* returns the priotity of the operator */ int priority (char c ); /* converts the Infix expression to Prefix form */ void ConvertToPrefix (void); }; #endif    #include "Infix.h" #include "Infix.h" #include <stdio.h> #include <conio.h> #include <string.h> #include <ctype.h> //using namespace std; # define MAX 50 char output[MAX] ; char Stack[MAX] ; char input[MAX] ; char *s, *t ; /*pointers to input and output strings*/ char ch; /*choice*/ int top; /*Stack top*/ int l ; /*length of infix string*/ void Infix:: Initialize (void) { top = -1 ;/*Make stack empty*/ strcpy ( output, "" ) ; strcpy ( Stack, "" ) ; l = 0 ; } void Infix:: SetExpression ( char *str ) { s = str ; l = strlen ( s ) ; *( output + l ) = '' ; t = output; } /* adds operator to the stack */ void Infix:: PushOnStack ( char c ) { if ( top == MAX - 1 ) printf ( " Stack is full. " ) ; else { top++ ; Stack[top] = c ; } } /* pops an operator from the stack */ char Infix:: PopFromStack (void ) { if ( top == -1 ) /* Stack is empty*/ return -1 ; else { char item = Stack[top] ; top-- ; return item ; } } /* returns the priotity of the operator */ int Infix:: priority ( char c ) { if ( c == '^' ) return 3 ;/*Exponential operator*/ if ( c == '*' || c == '/' || c == '%' ) return 2 ; else if ( c == '+' || c == '-' ) return 1 ; else return 0 ; } /* converts the Infix expression to Prefix form */ void Infix:: ConvertToPrefix (void) { char opr ; while ( *( s ) ) { /*Skip white spaces, if any*/ if ( *( s ) == ' ' || *( s ) == ' ' ) { s++ ; continue ; } if ( isdigit ( *( s ) ) || isalpha ( *( s ) ) )/*operands*/ { while ( isdigit ( *( s ) ) || isalpha ( * ( s ) ) ) { *( t ) = *( s ) ; s++ ; t++ ; } } if ( *( s ) == ')' )/*Closing Parenthesis*/ { PushOnStack ( *( s ) ) ; s++ ; } if ( *( s ) == '*' || *( s ) == '+' || *( s ) == '/' || *( s ) == '%' || *( s ) == '-' || *( s ) == '^' ) { if ( top != -1 ) { opr = PopFromStack ( ) ; while ( priority ( opr ) > priority ( *( s ) ) ) { *( t ) = opr ; t++ ; opr = PopFromStack ( ) ; } PushOnStack ( opr ) ; PushOnStack ( *( s ) ) ; } else PushOnStack ( *( s ) ) ; s++ ; } if ( *( s ) == '(' )/*Opening Parenthesis*/ { opr = PopFromStack ( ) ; while ( opr != ')' ) { *( t ) = opr ; t++ ; opr = PopFromStack ( ) ; } s++ ; } } while ( top != -1 )/*While Stack is not empty*/ { opr = PopFromStack ( ) ; *( t ) = opr ; t++ ; } t++ ; } #include "Infix.h" #include"Infix.cpp" #include<stdio.h> # include <iostream> using namespace std; void main( ) { Infix i; i.Initialize ( ) ; printf ( " Enter an expression in infix form: " ) ; gets ( input ) ; i.SetExpression (input) ; strrev ( s ); i.ConvertToPrefix ( ) ; strrev ( output ) ; printf ( " The Prefix expression is: " ) ; puts(output); getch( ) ; } Im trying to write a program to convert an infix expression into a prefix expression. I can not get the program run properly. #ifndef INFIX_H #define INFIX_H #include <string> #include<iostream> //#include<stack> using namespace std; //# define MAX 50 /** The class Infix represent infix expression.*/ class Infix { public: // Initaialize infix void Initialize (void); // Get SetExpression void SetExpression (char *str); //pops an operator from the stack char PopFromStack ( void); /* adds operator to the stack */ void PushOnStack (char c ); /* returns the priotity of the operator */ int priority (char c ); /* converts the Infix expression to Prefix form */ void ConvertToPrefix (void); }; #endif    #include "Infix.h" #include "Infix.h" #include <stdio.h> #include <conio.h> #include <string.h> #include <ctype.h> //using namespace std; # define MAX 50 char output[MAX] ; char Stack[MAX] ; char input[MAX] ; char *s, *t ; /*pointers to input and output strings*/ char ch; /*choice*/ int top; /*Stack top*/ int l ; /*length of infix string*/ void Infix:: Initialize (void) { top = -1 ;/*Make stack empty*/ strcpy ( output, "" ) ; strcpy ( Stack, "" ) ; l = 0 ; } void Infix:: SetExpression ( char *str ) { s = str ; l = strlen ( s ) ; *( output + l ) = '' ; t = output; } /* adds operator to the stack */ void Infix:: PushOnStack ( char c ) { if ( top == MAX - 1 ) printf ( " Stack is full. " ) ; else { top++ ; Stack[top] = c ; } } /* pops an operator from the stack */ char Infix:: PopFromStack (void ) { if ( top == -1 ) /* Stack is empty*/ return -1 ; else { char item = Stack[top] ; top-- ; return item ; } } /* returns the priotity of the operator */ int Infix:: priority ( char c ) { if ( c == '^' ) return 3 ;/*Exponential operator*/ if ( c == '*' || c == '/' || c == '%' ) return 2 ; else if ( c == '+' || c == '-' ) return 1 ; else return 0 ; } /* converts the Infix expression to Prefix form */ void Infix:: ConvertToPrefix (void) { char opr ; while ( *( s ) ) { /*Skip white spaces, if any*/ if ( *( s ) == ' ' || *( s ) == ' ' ) { s++ ; continue ; } if ( isdigit ( *( s ) ) || isalpha ( *( s ) ) )/*operands*/ { while ( isdigit ( *( s ) ) || isalpha ( * ( s ) ) ) { *( t ) = *( s ) ; s++ ; t++ ; } } if ( *( s ) == ')' )/*Closing Parenthesis*/ { PushOnStack ( *( s ) ) ; s++ ; } if ( *( s ) == '*' || *( s ) == '+' || *( s ) == '/' || *( s ) == '%' || *( s ) == '-' || *( s ) == '^' ) { if ( top != -1 ) { opr = PopFromStack ( ) ; while ( priority ( opr ) > priority ( *( s ) ) ) { *( t ) = opr ; t++ ; opr = PopFromStack ( ) ; } PushOnStack ( opr ) ; PushOnStack ( *( s ) ) ; } else PushOnStack ( *( s ) ) ; s++ ; } if ( *( s ) == '(' )/*Opening Parenthesis*/ { opr = PopFromStack ( ) ; while ( opr != ')' ) { *( t ) = opr ; t++ ; opr = PopFromStack ( ) ; } s++ ; } } while ( top != -1 )/*While Stack is not empty*/ { opr = PopFromStack ( ) ; *( t ) = opr ; t++ ; } t++ ; } #include "Infix.h" #include"Infix.cpp" #include<stdio.h> # include <iostream> using namespace std; void main( ) { Infix i; i.Initialize ( ) ; printf ( " Enter an expression in infix form: " ) ; gets ( input ) ; i.SetExpression (input) ; strrev ( s ); i.ConvertToPrefix ( ) ; strrev ( output ) ; printf ( " The Prefix expression is: " ) ; puts(output); getch( ) ; }

Explanation / Answer

ok then dear save your first file name as Infix.h save the sec file as Infix.cpp save the third file as your defined name. now u just copile first 2 programs.even if u get errors or warnings..say compile anyway. now u compile and run your 3rd program. this will workout all the best