i want similar for this code to solve two questions :
1- Write a program to convert a postfix expression to infix expression
2-Write a program to convert an infix expression to prefix expression
each question in separate code ( so will be two codes )
#include <iostream>
#include <string>
#define SIZE 50
using namespace std;
// structure to represent a stack
struct Stack {
char s[SIZE];
int top;
};
void push(Stack *st, char c) {
st->top++;
st->s[st->top] = c;
}
char pop(Stack *st) {
char c = st->s[st->top];
st->top--;
//(A+B)*(C+D)
return c;
}
/* function to check whether a character is an operator or not.
this function returns 1 if character is operator else returns 0 */
int isOperator(char c) {
switch (c)
{
case '^':
case '+':
case '-':
case '*':
case '/':
case '%':
return 1;
default:
return 0;
}
}
/* function to assign precedence to operator.
In this function we assume that higher integer value means higher precedence */
int precd(char c) {
switch (c)
{
case '^':
return 3;
case '*':
case '/':
case '%':
return 2;
case '+':
case '-':
return 1;
default:
return 0;
}
}
//function to convert infix expression to postfix expression
string infixToPostfix(Stack *st, string infix) {
string postfix = "";
for(int i=0; i<infix.length(); i++)
{
if(isOperator(infix[i])==1)
{
while(st->top!=-1 && precd(st->s[st->top]) >= precd(infix[i]))
postfix += pop(st);
push(st,infix[i]);
}
else if(infix[i] == '(')
push(st,infix[i]);
else if(infix[i] == ')')
{
while(st->top!=-1 && st->s[st->top] != '(')
postfix += pop(st);
pop(st);
}
else
postfix += infix[i];
}
while(st->top != -1)
postfix += pop(st);
return postfix;
}
int main() {
Stack st;
st.top = -1;
string infix;
cout << "Enter an infix expression: ";
getline(cin, infix);
cout << "Postfix expression is: " << infixToPostfix(&st, infix);
return 0;
}
1)//function to convert postfix expression to infix expression
string PostToInfix(Stack *st, string postfix) {
string infix = "";
for(int i=0; i< postfix.length(); i++)
{
//If postfix[i]) is operator follow the given lines
//precedence is not needed in postfix expressions
if(isOperator( postfix[i])==1)
{
while(st->top!=-1)
string op1;
string op2;
op1=st->top;
pop(st);
op2=st->top;
pop(st);
infix='('+op2+postfix[i]+op1+')';
push(st,infix[i]);
}
else
//this condition runs if postfix[i] is an operand
push(st,postfix[i]);
}
return infix;
}
int main() {
Stack st;
st.top = -1;
string postfix;
cout << "Enter an postfix expression: ";
getline(cin, postfix);
cout << "Infix expression is: " << PostToInfix(&st,postfix);
return 0;
}
2)//function to convert infix expression to prefix expression
string infixToprefix(Stack *st, string infix) {
string prefix = "";
reverse(infix.begin(),infix.end());
for(int i=0; i<infix [i].length(); i++)
{
if(infix[i] == '(')
infix[i] == ')';
if(infix[i] == ')')
infix[i] == '(';
}
for(int i=0; i<infix [i].length(); i++)
{
if(isOperator(infix[i])==1)
{
if(st->top!=-1 && precd(infix[i])>=precd(st->s[st->top]))
push(st,infix[i]);
else if (precd(infix[i])==precd(st->s[st->top])&& (infix[i]=='^'))
{ prefix += pop(st);
pop(st);
}
else
push(st,infix[i]);
}
for(int i=0; i<infix [i].length(); i++)
{
if(infix[i] == ')')
{
while(st->top!=-1 && st->s[st->top] != '(')
{ prefix += infix[i];
pop(st);
}
}
else if(infix[i] == '(')
push(st,infix[i])
return prefix;
}
}
int main() {
Stack st;
st.top = -1;
string infix;
cout << "Enter an infix expression: ";
getline(cin, infix);
cout << "Prefix expression is: " << infixToprefix(&st, infix);
return 0;
}
i want similar for this code to solve two questions : 1- Write a program to...
Given the following C code. Develop a evaluate post fix function that calculates the converted post fix. #include <stdio.h> #include <ctype.h> #define max 10 char s[100]; int top=-1; void push(char); char pop(); int precede(char); main(){ char infix[100],postfix[100],ch; int i=0,j=0; // Read infix expression from user printf("Enter infix expression:"); scanf("%s",infix); // evaluate each char in infix expression while((ch=infix[i++])!='\0'){ // if it is number, add it to postfix expression if(isalnum(ch)){ ...
This code in C converts infix to postfix and evaluates it. The problem is that it only evaluates one digit expressions. I need to fix it so that it can evaluate 2 digits expressions as well. #include <stdio.h> #include <ctype.h> #include <string.h> #include <math.h> #define SIZE 100 char s[SIZE]; int top=-1; void infixToPostfix(char *infix, char *postfix); void postfixEvaluation(char *postfix); void push(char elem){ s[++top]=elem; } char pop(){ return(s[top--]); } int pr(char elem){ // Order of precedence switch (elem) { case '(':...
Help me to fix this code in C language. This code converts infix expressions to postfix and then evaluate the expression. Right now, it works with single digits. I need to modify it so that it can evaluate expressions with also 2 digits, example: (60+82)%72. Additionally I need to display an error when the parenthesis don't match like (89+8(. I have muted some line that would print the postfix expression with a space like: 22 8 + when the input...
Finish function to complete code. #include <stdio.h> #include <stdlib.h> #include<string.h> #define Max_Size 20 void push(char S[], int *p_top, char value); char pop(char S[], int *p_top); void printCurrentStack(char S[], int *p_top); int validation(char infix[], char S[], int *p_top); char *infix2postfix(char infix[], char postfix[], char S[], int *p_top); int precedence(char symbol); int main() { // int choice; int top1=0; //top for S1 stack int top2=0; //top for S2 stack int *p_top1=&top1; int *p_top2=&top2; char infix[]="(2+3)*(4-3)"; //Stores infix string int n=strlen(infix); //length of...
I need assistance with this code. Is there any way I can create
this stack class (dealing with infix to postfix then postfix
evaluation) without utilizing <stdio.h> and
<math.h>?
____________________________________________________________________________________________
C++ Program:
#include <iostream>
#include <string>
#include <stdio.h>
#include <math.h>
using namespace std;
//Stack class
class STACK
{
private:
char *str;
int N;
public:
//Constructor
STACK(int maxN)
{
str = new char[maxN];
N = -1;
}
//Function that checks for empty
int empty()
{
return (N == -1);
}
//Push...
Stacks are used by compilers to help in the process of
evaluating expressions and generating machine language code.In this
exercise, we investigate how compilers evaluate arithmetic
expressions consisting only of constants, operators and
parentheses. Humans generally write expressions like 3 + 4and 7 /
9in which the operator (+ or / here) is written between its
operands—this is called infix notation. Computers “prefer” postfix
notation in which the operator is written to the right of its two
operands. The preceding...
Return a method as an expression tree
Hi guys.
I need to return a method as an expression tree, it's currently
returning null.
public static ExpressionTree getExpressionTree(String
expression)
throws Exception {
char[] charArray = expression.toCharArray();
Node root = et.constructTree(charArray);
System.out.println("infix expression is");
et.inorder(root);
return et;
}
In the above method, et needs to have a value.
-- Take a look at the complete class below.
Kindly assist.
; public class ExpressionTree extends BinaryTree { private static final String DELIMITERS...
Write a program (InfixToPostfix function) to convert an infix expression that includes +,-, * and / to postfix. int main(int argc, const char argv[l) cout <"Final Result : "<< InfixToPostfix("(5+3)*4" return The above main function should display the result below: Final Result 53+4* Here are some uiliy functions that you may find usefal int precedence(char ch) switch (ch) case case- return 1; case case ソ': return 2 '^': return 3; case
Total point: 15 Introduction: For this assignment you have to write a c program that will take an infix expression as input and display the postfix expression of the input. After converting the postfix expression, the program should evaluate the expression from the postfix and display the result. What should you submit? Write all the code in a single file and upload the .c file. Problem We as humans write math expression in infix notation, e.g. 5 + 2 (the...
Convert infix to postfix, and evaluate postfix using custom Stack created using a singly linked list. This is only supposed to use THAT method, calling a normal Stack will give me a zero. I do have the conversion to postfix, but there may be error in there. But the main problem currently is the evaluation of postfix. I keep getting an error that I made for an empty stack, which I will include. For testing it is only supposed to...