0% found this document useful (0 votes)
10 views2 pages

DSL Practical 7

The document contains a C++ program that converts an infix expression to postfix notation and evaluates the postfix expression. It defines functions for operator precedence, checking if a character is an operator or operand, converting infix to postfix, and evaluating postfix expressions. The main function prompts the user for an infix expression and outputs the corresponding postfix expression.

Uploaded by

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

DSL Practical 7

The document contains a C++ program that converts an infix expression to postfix notation and evaluates the postfix expression. It defines functions for operator precedence, checking if a character is an operator or operand, converting infix to postfix, and evaluating postfix expressions. The main function prompts the user for an infix expression and outputs the corresponding postfix expression.

Uploaded by

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

#include <iostream>

#include <stack>
#include <string>
#include <cctype>
#include <sstream>
using namespace std;

int precedence(char op) {


if (op == ’+’ || op == ’-’)
return 1;
if (op == ’*’ || op == ’/’)
return 2;
return 0;
}
bool isOperator(char ch) {
return (ch == ’+’ || ch == ’-’ || ch == ’*’ || ch == ’/’);
}
bool isOperand(char ch) {
return isalnum(ch);
}

string infixToPostfix(const string& infix) {


stack<char> s;
string postfix = "";
string number = "";

for (char ch : infix) {


if (isdigit(ch) || isalpha(ch)) {
number += ch;
} else {
if (!number.empty()) {
postfix += number + ’ ’;
number = "";
}
if (ch == ’(’) {
s.push(ch);
} else if (ch == ’)’) {
while (!s.empty() && s.top() != ’(’) {
postfix += s.top();
postfix += ’ ’;
s.pop();
}
s.pop();
} else if (isOperator(ch)) {
while (!s.empty() && precedence(s.top()) >= precedence(ch)) {
postfix += s.top();
postfix += ’ ’;
s.pop();
}
s.push(ch);
}
}
}
if (!number.empty()) {
postfix += number + ’ ’;
}

while (!s.empty()) {
postfix += s.top();
postfix += ’ ’;
s.pop();
}
return postfix;
}

int evaluatePostfix(const string& postfix) {


stack<int> s;
istringstream iss(postfix);
string token;

while (iss >> token) {


if (isdigit(token[0])) {
s.push(stoi(token));
} else if (isOperator(token[0])) {
int operand2 = s.top();
s.pop();
int operand1 = s.top();
s.pop();

switch (token[0]) {
case ’+’: s.push(operand1 + operand2); break;
case ’-’: s.push(operand1 - operand2); break;
case ’*’: s.push(operand1 * operand2); break;
case ’/’: s.push(operand1 / operand2); break;
}
}
}
return s.top();
}

int main() {
string infixExpression;
cout << "Enter infix expression: ";
getline(cin, infixExpression);
string postfixExpression = infixToPostfix(infixExpression);
cout << "Postfix expression: " << postfixExpression;
}

Output:
Enter infix expression: a+b*(c-d)
Postfix expression: a b c d - * +

You might also like