0% found this document useful (0 votes)
2 views17 pages

Rules For Converting An Infix Expression To A Prefix Expression and Codes

The document outlines the rules for converting infix expressions to prefix expressions, which involve reversing the infix expression, converting it to postfix, and then reversing the postfix. It provides detailed steps for infix to postfix conversion, including operator precedence and associativity. Additionally, it includes examples and C++ code implementations for both infix to postfix and infix to prefix conversions.

Uploaded by

jogibaba207
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views17 pages

Rules For Converting An Infix Expression To A Prefix Expression and Codes

The document outlines the rules for converting infix expressions to prefix expressions, which involve reversing the infix expression, converting it to postfix, and then reversing the postfix. It provides detailed steps for infix to postfix conversion, including operator precedence and associativity. Additionally, it includes examples and C++ code implementations for both infix to postfix and infix to prefix conversions.

Uploaded by

jogibaba207
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Rules for converting an infix expression to a prefix expression

Infix to Prefix Conversion Rules

1. Reverse the Infix Expression

First, reverse the entire infix expression.

After reversing, swap every opening parenthesis ( with a closing


parenthesis ) and vice versa. This step helps in handling parentheses
correctly during the conversion process.

2. Apply Infix to Postfix Conversion on the Reversed Expression

Convert the modified (reversed) expression into a postfix expression


using the standard infix to postfix conversion rules (explained below).

3. Reverse the Postfix Expression

Finally, reverse the resulting postfix expression to get the correct prefix
expression.

Infix to Postfix Conversion Rules (for step 2)

To convert the reversed expression into postfix, follow these rules

1. If the character is an operand (a number or variable), add it to the postfix


expression.
2. If the character is an operator
 Pop operators from the stack and add them to the postfix
expression while they have higher or equal precedence than the
current operator.
 Push the current operator onto the stack.
3. If the character is a left parenthesis (, push it onto the stack.
4. If the character is a right parenthesis )
 Pop operators from the stack and add them to the postfix
expression until a left parenthesis ( is encountered.
 Discard the left parenthesis (.
5. At the end of the expression, pop all the remaining operators from the stack
and add them to the postfix expression.

Operator Precedence and Associativity

 Operators have the following precedence (from highest to lowest)


1. ^ (Exponentiation)
2. *, / (Multiplication, Division)
3. +, - (Addition, Subtraction)
 Associativity
 ^ (Exponentiation) is right-associative (evaluates right to left).
 All other operators (+, -, *, /) are left-associative (evaluate left to
right).

Example of Infix to Prefix Conversion

Let's convert the infix expression A + B * C to a prefix expression

1. Step 1 Reverse the Infix Expression

Original infix: A + B * C
Reversed expression: C * B + A

2. Step 2 Apply Infix to Postfix Conversion on the Reversed Expression

Process C * B + A using the infix to postfix rules

 C is an operand → add to postfix: C


 is an operator → push to the stack: *
 B is an operand → add to postfix: C B
 + is an operator → pop * from stack and add to postfix (since
* has higher precedence than +), then push + to the stack: C
B*
 A is an operand → add to postfix: C B * A

Resulting postfix expression CB*A+

3. Step 3 Reverse the Postfix Expression

Postfix expression CB*A+


Reversed postfix +A*BC
2nd Example

Convert the Infix Expression (A + B) * (C - D) to Prefix

Step 1
Reverse the Infix Expression

We begin by reversing the original infix expression

Original Infix Expression (A + B) * (C - D)


Reversed Expression (D - C) * (B + A)

 After reversing, we also swap the parentheses

 Swapped Parentheses Expression )D - C( * )B + A(

Step 2 Convert the Reversed Expression to Postfix

Now, we'll convert the reversed expression )D - C( * )B + A( into a postfix expression,


following the standard infix to postfix conversion rules.

1. Start with the first character )

A closing parenthesis doesn’t change anything at this point; we continue


to the next character.

2. Next character D

D is an operand, so we add it to the postfix expression

Output Postfix: D

3. Next character -

- is an operator, so we push it onto the stack

Stack: - and the Output is Postfix: D

4. Next character C and

C is an operand, so we add it to the postfix expression

Stack: - and the Output is Postfix: D C


5. Next character )

A closing parenthesis means we pop operators from the stack until we


encounter an opening parenthesis (. We pop the operator - and add it to
the postfix expression:

Postfix: D C -
Stack: (empty)

6. Next character *

* is an operator, so we push it onto the stack

Stack: * and output is Postfix: D C -

7. Next character (

An opening parenthesis means we push it onto the stack

Stack: * ( and output is Postfix: D C -

8. Next character B

B is an operand, so we add it to the postfix expression

Stack: * ( and output is Postfix: D C - B

9. Next character +

+is an operator. We check the precedence of operators in the stack. The


operator ( has no precedence, so we push + onto the stack

Stack: * ( + and output is Postfix: D C - B

10. Next character A

A is an operand, so we add it to the postfix expression

Stack: * ( + and output is Postfix: D C - B A

11. Final character )

A closing parenthesis means we pop the + operator from the stack and
add it to the postfix expression
Postfix: D C - B A +
Stack: *

12. End of expression

Finally, we pop the * operator from the stack and add it to the postfix
expression:

Postfix: D C - B A + *

13. Step 3 Reverse the Postfix Expression

Now, reverse the postfix expression D C - B A + * to obtain the final prefix expression

Final Prefix Expression

*+AB-CD
#include <iostream>
#include <cstring>
using namespace std;

char stack[100]; // Stack to hold operators


int top = -1; // Stack pointer
// Function to check if a character is an operator
bool isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/' || c == '^');
}
// Function to define precedence of operators
int precedence(char c) {
if (c == '^') return 3;
if (c == '*' || c == '/') return 2;
if (c == '+' || c == '-') return 1;
return 0;
}
// Function to convert infix to postfix
void infixToPostfix(char infix[]) {
char postfix[100]; // Resulting postfix expression
int j = 0; // Index for postfix output
for (int i = 0; infix[i] != '\0'; i++) {
char ch = infix[i];
// If operand, add it to output
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || isdigit(ch)) {
postfix[j++] = ch;
}
// If '(', push to stack
else if (ch == '(') {
stack[++top] = ch;
}
// If ')', pop until '('
else if (ch == ')') {
while (top != -1 && stack[top] != '(') {
postfix[j++] = stack[top--];
}
top--; // Pop the '('
}
// If operator
else if (isOperator(ch)) {
while (top != -1 && precedence(stack[top]) >= precedence(ch)) {
postfix[j++] = stack[top--];
}
stack[++top] = ch;
}
}
// Pop remaining operators
while (top != -1) {
postfix[j++] = stack[top--];
}
postfix[j] = '\0'; // Null-terminate the string
cout << "Postfix Expression: " << postfix << endl;
}
int main() {
char expression[100];
cout << "Enter Infix Expression (no spaces): ";
cin >> expression;
infixToPostfix(expression);
return 0;
}
#include <iostream>
#include <cstring>
#include <cctype> // For isdigit()
using namespace std;
char stack[100]; // Stack to hold operators
int top = -1; // Stack pointer
bool isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/' || c == '^');
}
int precedence(char c) {
if (c == '^') return 3;
if (c == '*' || c == '/') return 2;
if (c == '+' || c == '-') return 1;
return 0;
}
void infixToPostfix(char infix[]) {
char postfix[100]; // Postfix expression
int j = 0; // Pointer for postfix

for (int i = 0; infix[i] != '\0'; i++) {


char ch = infix[i];
// If it's an operand (variable or number), add it to the postfix expression
if (isalnum(ch)) {
postfix[j++] = ch;
}
// If it's an opening parenthesis, push it to the stack
else if (ch == '(') {
stack[++top] = ch;
}
// If it's a closing parenthesis, pop from the stack until an opening parenthesis is found
else if (ch == ')') {
while (top != -1 && stack[top] != '(') {
postfix[j++] = stack[top--];
}
top--; // Pop the '('
}
// If it's an operator
else if (isOperator(ch)) {
while (top != -1 && precedence(stack[top]) >= precedence(ch)) {
postfix[j++] = stack[top--];
}
stack[++top] = ch;
}
}

// Pop any remaining operators from the stack


while (top != -1) {
postfix[j++] = stack[top--];
}

postfix[j] = '\0'; // Null terminate the postfix expression


cout << "Postfix Expression: " << postfix << endl;
}

int main() {
char infix[] = "A+B*(C^D-E)^(F+G*H)-I"; // Example complex expression
infixToPostfix(infix); // Convert the expression
return 0;
}
#include <iostream>
#include <cstring>
#include <cctype> // For isdigit()
#include <stack> // Using stack from STL
using namespace std;
// Function to check if a character is an operator
bool isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/' || c == '^');
}
// Function to define precedence of operators
int precedence(char c) {
if (c == '^') return 3;
if (c == '*' || c == '/') return 2;
if (c == '+' || c == '-') return 1;
return 0;
}
// Function to reverse the string
string reverseString(string s) {
string reversed = s;
int n = reversed.length();
for (int i = 0; i < n / 2; i++) {
swap(reversed[i], reversed[n - i - 1]);
}
return reversed;
}
// Function to convert infix to postfix
string infixToPostfix(string infix) {
stack<char> st;
string postfix = "";
for (int i = 0; i < infix.length(); i++) {
char ch = infix[i];
// If it's an operand, add it to the postfix expression
if (isalnum(ch)) {
postfix += ch;
}
// If it's an opening parenthesis, push it to the stack
else if (ch == '(') {
st.push(ch);
}
// If it's a closing parenthesis, pop until we find an opening parenthesis
else if (ch == ')') {
while (!st.empty() && st.top() != '(') {
postfix += st.top();
st.pop();
}
st.pop(); // Pop the '('
}
// If it's an operator, pop operators of higher or equal precedence
else if (isOperator(ch)) {
while (!st.empty() && precedence(st.top()) >= precedence(ch)) {
postfix += st.top();
st.pop();
}
st.push(ch);
}
}
// Pop any remaining operators from the stack
while (!st.empty()) {
postfix += st.top();
st.pop();
}

return postfix;
}

// Function to convert infix to prefix


string infixToPrefix(string infix) {
// Reverse the infix expression
infix = reverseString(infix);
// Step 1: Reverse parentheses and handle them
for (int i = 0; i < infix.length(); i++) {
if (infix[i] == '(') {
infix[i] = ')';
} else if (infix[i] == ')') {
infix[i] = '(';
}
}
// Step 2: Convert the reversed infix to postfix
string postfix = infixToPostfix(infix);

// Step 3: Reverse the postfix expression to get the prefix


return reverseString(postfix);
}
int main() {
string infix = "(A+B)*(C-D)"; // Example expression
string prefix = infixToPrefix(infix);
cout << "Infix Expression: " << infix << endl;
cout << "Prefix Expression: " << prefix << endl;
return 0;
}
#include <iostream>
#include <cstring>
#include <cctype> // For isdigit()
#include <string> // For string handling
#include <stack> // Using stack from STL
using namespace std;

// Function to check if a character is an operator


bool isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/' || c == '^');
}

// Function to define precedence of operators


int precedence(char c) {
if (c == '^') return 3;
if (c == '*' || c == '/') return 2;
if (c == '+' || c == '-') return 1;
return 0;
}

// Function to reverse the string


string reverseString(string s) {
string reversed = s;
int n = reversed.length();
for (int i = 0; i < n / 2; i++) {
swap(reversed[i], reversed[n - i - 1]);
}
return reversed;
}

// Function to convert infix to postfix


string infixToPostfix(string infix) {
stack<char> st;
string postfix = "";
string operand = ""; // To accumulate multi-character operands

for (int i = 0; i < infix.length(); i++) {


char ch = infix[i];

// If it's an operand, accumulate it


if (isalnum(ch)) {
operand += ch;
} else {
if (!operand.empty()) {
postfix += operand + " "; // Add the accumulated operand
operand = ""; // Reset the operand
}

// If it's an opening parenthesis, push it to the stack


if (ch == '(') {
st.push(ch);
}
// If it's a closing parenthesis, pop until we find an opening parenthesis
else if (ch == ')') {
while (!st.empty() && st.top() != '(') {
postfix += st.top();
postfix += " ";
st.pop();
}
st.pop(); // Pop the '('
}
// If it's an operator, pop operators of higher or equal precedence
else if (isOperator(ch)) {
while (!st.empty() && precedence(st.top()) >= precedence(ch)) {
postfix += st.top();
postfix += " ";
st.pop();
}
st.push(ch);
}
}
}

// Add the last operand if present


if (!operand.empty()) {
postfix += operand + " ";
}

// Pop any remaining operators from the stack


while (!st.empty()) {
postfix += st.top();
postfix += " ";
st.pop();
}
return postfix;
}

// Function to convert infix to prefix


string infixToPrefix(string infix) {
// Reverse the infix expression
infix = reverseString(infix);
// Step 1: Reverse parentheses and handle them
for (int i = 0; i < infix.length(); i++) {
if (infix[i] == '(') {
infix[i] = ')';
} else if (infix[i] == ')') {
infix[i] = '(';
}
}
// Step 2: Convert the reversed infix to postfix
string postfix = infixToPostfix(infix);
// Step 3: Reverse the postfix expression to get the prefix
return reverseString(postfix);
}
int main() {
string infix = "(A1+B2)*(C3-D4)"; // Example expression with multi-character
operands
string prefix = infixToPrefix(infix);
cout << "Infix Expression: " << infix << endl;
cout << "Prefix Expression: " << prefix << endl;
return 0;
}

You might also like