C++ code:
Problem 3. Convert the following infix expression to a prefix expression by Stack operation. A + B* C + (D^E) * F/G/H + I
Evaluate the value of prefix expression when A=5, B=10, C=3, D=12, E=3, F=5, G=8, H=4, I=100
/* Source code*/
#include <bits/stdc++.h>
using namespace std;
bool isOperator(char c)
{
return (!isalpha(c) && !isdigit(c));
}
int charPriority(char C)
{
if (C == '-' || C == '+')
return 1;
else if (C == '*' || C == '/')
return 2;
else if (C == '^')
return 3;
return 0;
}
string infixToPostfix(string infix)
{
infix = '(' + infix + ')';
int l = infix.size();
stack<char> char_stack;
string output;
for (int i = 0; i < l; i++) {
// If the scanned
character is an
// operand, add it to output.
if (isalpha(infix[i]) ||
isdigit(infix[i]))
output +=
infix[i];
// If the scanned
character is an
// ‘(‘, push it to the stack.
else if (infix[i] == '(')
char_stack.push('(');
// If the scanned
character is an
// ‘)’, pop and output from the
stack
// until an ‘(‘ is
encountered.
else if (infix[i] == ')')
{
while
(char_stack.top() != '(') {
output += char_stack.top();
char_stack.pop();
}
//
Remove '(' from the stack
char_stack.pop();
}
// Operator found
else {
if
(isOperator(char_stack.top())) {
while (charPriority(infix[i]) <=
charPriority(char_stack.top())) {
output +=
char_stack.top();
char_stack.pop();
}
// Push current Operator on stack
char_stack.push(infix[i]);
}
}
}
return output;
}
string infixToPrefix(string infix)
{
/* Reverse String
* Replace ( with ) and vice versa
* Get Postfix
* Reverse Postfix * */
int l = infix.size();
// Reverse infix
reverse(infix.begin(), infix.end());
// Replace ( with ) and vice versa
for (int i = 0; i < l; i++) {
if (infix[i] == '(')
{
infix[i] =
')';
i++;
}
else if (infix[i] == ')') {
infix[i] =
'(';
i++;
}
}
string prefix = infixToPostfix(infix);
// Reverse postfix
reverse(prefix.begin(), prefix.end());
return prefix;
}
/* These functions are for evaluate of prefix
expressions*/
bool isOperand(char c)
{
// If the character is a digit then it must
// be an operand
return isdigit(c);
}
double evaluatePrefix(string exprsn)
{
stack<double> Stack;
for (int j = exprsn.size() - 1; j >= 0; j--) {
// Push operand to Stack
// To convert exprsn[j] to digit subtract
// '0' from exprsn[j].
if (isOperand(exprsn[j]))
Stack.push(exprsn[j] - '0');
else {
// Operator encountered
// Pop two elements from Stack
double o1 = Stack.top();
Stack.pop();
double o2 = Stack.top();
Stack.pop();
// Use switch case to operate on o1
// and o2 and perform o1 O o2.
switch (exprsn[j]) {
case '+':
Stack.push(o1 + o2);
break;
case '-':
Stack.push(o1 - o2);
break;
case '*':
Stack.push(o1 * o2);
break;
case '/':
Stack.push(o1 / o2);
break;
}
}
}
return Stack.top();
}
// Driver code
int main()
{
string s = ("A+B*C+(D^E)*F/G/H+I");
int A=5, B=10, C=3, D=12, E=3, F=5, G=8, H=4,
I=100;
string exprsn = infixToPrefix(s);
cout << "INFIX to Prefix notation : " <<
exprsn << std::endl;
string changedString;
//now change charactor values with respective
value
for (int j = 0; j < exprsn.size(); j++) {
switch(exprsn[j]){
case 'A': changedString.append("(5)"); break;
case 'B': changedString.append("10"); break;
case 'C' : changedString.append("3"); break;
case 'D': changedString.append("12"); break;
case 'E': changedString.append("3"); break;
case 'F': changedString.append("5"); break;
case 'G': changedString.append("8"); break;
case 'H': changedString.append("4"); break;
case 'I': changedString.append("100"); break;
default:
string s;
s.push_back(exprsn[j]);
changedString.append(s);
}
}
cout<<"Changed string with the value is "
<< changedString<<endl;
cout << "Evaluation of prefix expression with
A=5, B=10, C=3, D=12, E=3, F=5, G=8, H=4, I=100" <<
evaluatePrefix(changedString) << endl;
return 0;
}
/Output*/

C++ code: Problem 3. Convert the following infix expression to a prefix expression by Stack operation....
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....
Convert the following infix expression to A) postfix B) prefix 3 * 4 / ( 5 - 6 * 7 )
a) Show the steps that a stack uses to convert the algebraic expression a*(b+c/d from infix to postfix notation. Indicate each intermediate change in the stack and postfix output. (Be sure to identify how operator precedence is determined. b) show the steps a stack uses to evaluate the postfix expression from part (a) when (a-6, b-4, c-2, d 5) c) Show the steps a stack uses to produce an expression tree with the postfix expression from part (a).
a) Show...
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 /...
java
Convert the following expressions to both Prefix and Postfix / Infix and create the binary trees which represent them. (A B/C+D$E)* (F/ G) - H B. (A+B)+(C/ (D E)-F)/G H KL+AB+C DEF$/-/HI+* -
Using ADT Stack: Evaluating infix expressions by converting them to postfix expressions Postfix notation: In a postfix expression, a binary operation follows its two opperands. The order of the operands in a infix expression is the same as in the corresponding postfix expression but the order of the operators might change based on the precedence of the operators and the existing of paranthses. Infix Postfix a + b a b + (a + b) * c a b + c...
Convert the following Infix Expression to Postfix, Using the above sample solution 10. A+ ((B-C* D/E ) +F-G/H
In Java please as soon as possible
Convert the following expressions to both Prefix and Postfix / Infix and create the binary trees which represent them. V. (A B/C+D$E) * (F/G)- H A. (A+B)+(C/(D E)-F)/G H B. KL+A B+ CDEF$/-/HI+ * - C.
Convert the following expressions to both Prefix and Postfix / Infix and create the binary trees which represent them. V. (A B/C+D$E) * (F/G)- H A. (A+B)+(C/(D E)-F)/G H B. KL+A B+ CDEF$/-/HI+ * - C.
Write a program to convert an expression written in infix notation to be converted to postfix notation. The program must do the following: a. Read a string of characters representing an expression in infix notation. The '$' is to be added at the end of the string to mark its ending. Each character is a letter, digit, +,-,*, or /. If a character is any other character an error must be signaled and the program is terminated b. Use stacks...
Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...