Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Postfix To Infix in C and C++ (#992)
- Loading branch information
1 parent
e6921ca
commit e340b0f
Showing
2 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# include <stdio.h> | ||
# include <stdlib.h> | ||
# include <string.h> | ||
# include <stdbool.h> | ||
|
||
char stack[20]; | ||
int top = -1; | ||
|
||
//Pop the top value from the stack. | ||
char pop() | ||
{ | ||
return stack[top--]; | ||
} | ||
|
||
//Push it onto the stack. | ||
void push(char ch) | ||
{ | ||
stack[++top] = ch; | ||
} | ||
|
||
//Checking if it is operator or letter | ||
bool operand(char x) | ||
{ | ||
if(x == '+' || x == '-' || x == '*'|| x == '/') | ||
return true; | ||
return false; | ||
} | ||
|
||
void postfix_to_infix(char postfix[]) | ||
{ | ||
int length; | ||
char element, operator; | ||
length = strlen(postfix); | ||
printf("\nInfix Expression: "); | ||
printf("%c", postfix[0]); | ||
for(int count = 1; count < length; count++) | ||
{ | ||
// If the character is not an operand. | ||
if(!operand(postfix[count])) | ||
{ | ||
//If the character is letter , push it in the stack | ||
push(postfix[count]); | ||
} | ||
else | ||
{ | ||
//pop the element present in the stack | ||
element = pop(); | ||
//character is assigned to operator | ||
operator = postfix[count]; | ||
printf(" %c %c", operator, element); | ||
} | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
char postfix[20]; | ||
printf("Enter Postfix Expression: "); | ||
scanf("%s", postfix); | ||
postfix_to_infix(postfix); | ||
return 0; | ||
} | ||
|
||
/* | ||
Enter Postfix Expression: ab*c+ | ||
Infix Expression: a * b + c*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# include <iostream> | ||
# include <cstring> | ||
using namespace std; | ||
|
||
char stack[20]; | ||
int top = -1; | ||
|
||
//Pop the top value from the stack. | ||
char pop() | ||
{ | ||
return stack[top--]; | ||
} | ||
|
||
//Push it onto the stack. | ||
void push(char ch) | ||
{ | ||
stack[++top] = ch; | ||
} | ||
|
||
//Checking if it is operator or letter | ||
bool operand(char x) | ||
{ | ||
if(x == '+' || x == '-' || x == '*'|| x == '/') | ||
return true; | ||
return false; | ||
} | ||
|
||
void postfix_to_infix(char postfix[]) | ||
{ | ||
int length; | ||
char element,operators; | ||
length = strlen(postfix); | ||
cout << "\nInfix Expression: "; | ||
cout << postfix[0]; | ||
for(int count = 1; count < length; count++) | ||
{ | ||
// If the character is not an operand. | ||
if(!operand(postfix[count])) | ||
{ | ||
//If the character is letter , push it in the stack | ||
push(postfix[count]); | ||
} | ||
else | ||
{ | ||
//pop the element present in the stack | ||
element = pop(); | ||
//character is assigned to operator | ||
operators = postfix[count]; | ||
cout << operators << element ; | ||
} | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
char postfix[20]; | ||
cout << "Enter Postfix Expression: "; | ||
cin >> postfix; | ||
postfix_to_infix(postfix); | ||
return 0; | ||
} | ||
|
||
/*Enter Postfix Expression: ab*c+ | ||
Infix Expression: a*b+c */ |