Write an program in C/C++, to read an expression in Inform and Display the same in Pofstfix Form
Question
Write an program in C/C++, to read an expression in Inform and Display the same in Postfix Form
Solution
Sure, here is a step-by-step guide to write a program in C/C++ to read an expression in Inform and display it in postfix form:
- Start by including the necessary header files for input/output operations and string manipulation:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
- Define a function to check if a character is an operator:
int isOperator(char ch) {
if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
return 1;
return 0;
}
- Define a function to convert the infix expression to postfix form:
void infixToPostfix(char infix[], char postfix[]) {
int i, j;
char stack[100];
int top = -1;
// Iterate through each character in the infix expression
for (i = 0, j = 0; infix[i] != '\0'; i++) {
// If the character is an operand, add it to the postfix expression
if (isalnum(infix[i])) {
postfix[j++] = infix[i];
}
// If the character is an opening parenthesis, push it to the stack
else if (infix[i] == '(') {
stack[++top] = infix[i];
}
// If the character is a closing parenthesis, pop operators from the stack and add them to the postfix expression until an opening parenthesis is encountered
else if (infix[i] == ')') {
while (top != -1 && stack[top] != '(') {
postfix[j++] = stack[top--];
}
// Pop the opening parenthesis from the stack
if (top != -1 && stack[top] == '(') {
top--;
}
}
// If the character is an operator, pop operators from the stack and add them to the postfix expression until an operator with lower precedence or an opening parenthesis is encountered
else if (isOperator(infix[i])) {
while (top != -1 && stack[top] != '(' && infix[i] <= stack[top]) {
postfix[j++] = stack[top--];
}
// Push the current operator to the stack
stack[++top] = infix[i];
}
}
// Pop any remaining operators from the stack and add them to the postfix expression
while (top != -1 && stack[top] != '(') {
postfix[j++] = stack[top--];
}
// Add null character at the end of the postfix expression
postfix[j] = '\0';
}
- Define the main function to read the infix expression from the user and display it in postfix form:
int main() {
char infix[100], postfix[100];
// Read the infix expression from the user
printf("Enter the infix expression: ");
scanf("%s", infix);
// Convert the infix expression to postfix form
infixToPostfix(infix, postfix);
// Display the postfix expression
printf("Postfix expression: %s\n", postfix);
return 0;
}
- Compile and run the program. Enter the infix expression when prompted, and the program will display the corresponding postfix expression.
Note: This program assumes that the infix expression is valid and does not handle errors such as mismatched parentheses or invalid operators.
Similar Questions
Write an program in C/C++, to read an expression in Inform and Display the same in Pofstfix Form
what is the output of the given program?main(){#ifndef sanprintf("no");#endifprintf("yes");}
write a program to print integer no, from 1 to n where n is the input from the user using do while loop in c language use getch
Write a C program to read a sentence through keyboard and to display the count ofwhite spaces in the given sentence.
What will be the output of the C program? ans.09119011 Previous Marked for Review Next
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.