Write pseudocode for the conversion of infix to prefix notation algorithm
Pseudocode may use the following
Input : A * B + C / D
Output : + * A B/ C D
ANSWER:-
PSEUDOCODE:-
arr = infix expression
Set loop_counter to length of the array
do
char = arr[loop_counter]
if char is operator
push char to prefix stack
if char is ‘)’
push char to stack
if char is ‘(’
do
pop char and push it to prefix_stack
until char equals ‘(‘
if char is operator
if char precedence is greater or is equal to stack[top] precedence
push to stack
else
do
pop from stack and push to prefix stack
until condition satisfies
loop_counter--
while loop_counter is not equal to 0
pop out prefix stack and reverse and display each element
CODE IN C:-
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define max 100
char s[max];
int top=-1;
void push(char);
char pop();
int precede(char);
main(){
char infix[100],postfix[100],ch,a[100];
int i=0,j=0,k,l;
printf("Enter infix expression:");
scanf("%s",a);
l=strlen(a);
infix[l]='\0';
l--;
while(a[i]!='\0'){
if(a[i]=='('){
infix[l]=')';
}else if(a[i]==')'){
infix[l]='(';
}else{
infix[l]=a[i];
}
l--;
i++;
}
i=0;
while((ch=infix[i++])!='\0'){
if(isalnum(ch)){
postfix[j++]=ch;
}else if(ch=='('){
push(ch);
}else if(ch==')'){
while(s[top]!='('){
postfix[j++]=pop();
}
top--;
}else{
if(top==-1||precede(ch)>=precede(s[top])){
push(ch);
}else{
while(precede(ch)<=precede(s[top])&&top!=-1){
postfix[j++]=pop();
}
push(ch);
}
}
}
while(top!=-1){
postfix[j++]=pop();
}
postfix[j]='\0';
l=strlen(postfix);
l--;
i=0;
while(l>=0){
a[i]=postfix[l];
i++;
l--;
}
a[i]='\0';
printf("Infix expression = %s\nPrefix expression =
%s",infix,a);
}
void push(char a)
{
s[++top]=a;
}
char pop(){
return s[top--];
}
int precede(char a){
if(a=='('||a==')'){
return 1;
}else if(a=='-'||a=='+'){
return 2;
}else if(a=='%'||a=='/'||a=='*'){
return 3;
}
}
OUTPUT:-
NOTE:- If you have any doubts , please comment below.PLease give positive rating,THUMBS UP, THANK YOU!!!
Write pseudocode for the conversion of infix to prefix notation algorithm Pseudocode may use the following...
Write the reverse Polish notation, the infix notation and the result of the pseudocode given below. Show the floating-point register stack after each instruction. The first two instructions are given. FLD 2 FLD 4 FLD 2 FDIV FADD FLD 5 FLD 25 FSQRT FDIV FMUL FLD 5 FADD FLD 1 FADD FLD 2 FDIV
In C programming Language Write a version of the infix-to-postfix conversion algorithm. Write a program that converts an ordinary infix arithmetic expression (assume a valid expression is entered) with single-digit integers For Example: Infix expression (6 + 2) * 5 - 8 / 4 to a postfix expression is 62+5*84/- The program should read the expression into character array infix and use the stack functions implemented in this chapter to help create the postfix expression in character array postfix. The...
Write a Java program that will implement a stack object to convert from either infix notation to postfix notation or postfix notation to infix notation. The program will also implement a link list data structure to track all the conversions done. The Program should have a menu like the following as its output: "Please select what type of conversion you would like to do: Infix to postfix Postfix to infix Print Equations Exit"
Programming Assignment 2 – RPN Calculator – Infix to Postfix Conversion and The Evaluations of the Postfix Expression. You are to design and implement and algorithm in Java, to input an Infix expression , convert to a postfix expression and finally evaluate the postfix expression… Follow the examples done during class lectures… We are used to infix notation - ”3 + 4” - where the operator is between the operands. There is also prefix notation, where the operand comes before...
C++: Learning Outcomes Implement two stacks and use them to implement an infix to prefix expression convertor Stacks A stack is an abstract data type which uses a sequential container and limits access to that container to one end. You may enter or remove from the container, but only at one end. Using the Linked List data structure from your last homework assignment, implement a Stack of type string. The Stack should only have one data member: the Linked List....
EVALUATING GENERAL INFIX EXPRESSIONS INTRODUCTION The notation in which we usually write arithmetic expressions is called infix notation; in it, operators are written between their operands: X + Y. Such expressions can be ambiguous; do we add or multiply first in the expression 5 + 3 * 2? Parentheses and rules of precedence and association clarify such ambiguities: multiplication and division take precedence over addition and subtraction, and operators associate from left to right. This project implements and exercises a stack-based algorithm that evaluates...
Please help write a code in C that asks to translate from an infix expression (e.g. a * (b + c ) ) to a prefix notation (i.e., *a+bc) . You may assume that all the variables are one char and that the operators are limited to +, - , *. The infix expression is entered by the user from the command prompt. It has a maximum length of 20 characters and it must be correct – you do not...
By using PYTHON language Postfix to Infix using Stack Develop a stack application that can convert Postfix notation to Infix notation using the following algorithm. In your stack application, you can use only two stacks, one for a stack that can store Postfix notation, and the other is a stack to store infix notation. Also, it would help if you had a function to distinguish between an operation or an operand. Input A B C * + D E /...
Q1. [10 pts] Write an algorithm for Bubble sort that sorts array of n integers. Indicate the expected time complexity of the algorithm using the big-O notation. Use the following format for an algorithm pseudocode Function header (.....) Input: Output: Algorithm steps: 1. 2.
12. 10 points) Use pseudocode to write out algorithms for the following problems. (a) Assume n is any integer with n 2 7. Write out an algorithm SumofCertainIntenger in pseudocode that uses n as input variable. Use a "for" loop to compute the sum (Gk +4) (b) Assume m is any integer with m 2 5. Write out an algorithm ProductOfCertainInte- gers in pseudocode that uses m as input variable. Use a "while" loop to compute the product IT (é+4)....