100% found this document useful (1 vote)
40 views6 pages

DSL Assignment

The document contains C++ code that defines a class called InfixToPrefix for converting infix notation mathematical expressions to prefix notation and evaluating the prefix expressions. The class contains methods for converting a given infix string to prefix and evaluating a given prefix string. The code also includes a main method that tests the conversion and evaluation on a sample infix expression.

Uploaded by

huzaifaazeem48
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
100% found this document useful (1 vote)
40 views6 pages

DSL Assignment

The document contains C++ code that defines a class called InfixToPrefix for converting infix notation mathematical expressions to prefix notation and evaluating the prefix expressions. The class contains methods for converting a given infix string to prefix and evaluating a given prefix string. The code also includes a main method that tests the conversion and evaluation on a sample infix expression.

Uploaded by

huzaifaazeem48
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/ 6

Name M Huzaifa Azeem.

Sub DsLab.

Roll no 22-cs-438

#include <iostream>

#include <stack>

#include <string>

#include <unordered_map>

class InfixToPrefix {

public:

InfixToPrefix();

std::string convertToPrefix(const std::string& infix);

int evaluatePrefix(const std::string& prefix);

private:

int precedence(char op);

};

InfixToPrefix::InfixToPrefix() {}

int InfixToPrefix::precedence(char op) {

std::unordered_map<char, int> precedence_map = {

{'+', 1}, {'-', 1},

{'*', 2}, {'/', 2},

{'^', 3}

};
return precedence_map[op];

std::string InfixToPrefix::convertToPrefix(const std::string& infix) {

std::stack<char> operators;

std::stack<std::string> operands;

for (int i = infix.length() - 1; i >= 0; i--) {

char currentChar = infix[i];

if (isdigit(currentChar)) {

operands.push(std::string(1, currentChar));

} else if (currentChar == ')' || currentChar == ']' || currentChar == '}') {

operators.push(currentChar);

} else if (currentChar == '(' || currentChar == '[' || currentChar == '{') {

while (!operators.empty() && (operators.top() != ')' && operators.top() != ']' && operators.top() !
= '}')) {

char op = operators.top();

operators.pop();

std::string operand1 = operands.top();

operands.pop();

std::string operand2 = operands.top();

operands.pop();

std::string temp = op + operand2 + operand1;

operands.push(temp);

}
operators.pop();

} else {

while (!operators.empty() && precedence(operators.top()) > precedence(currentChar)) {

char op = operators.top();

operators.pop();

std::string operand1 = operands.top();

operands.pop();

std::string operand2 = operands.top();

operands.pop();

std::string temp = op + operand2 + operand1;

operands.push(temp);

operators.push(currentChar);

while (!operators.empty()) {

char op = operators.top();

operators.pop();

std::string operand1 = operands.top();

operands.pop();

std::string operand2 = operands.top();

operands.pop();
std::string temp = op + operand2 + operand1;

operands.push(temp);

return operands.top();

int InfixToPrefix::evaluatePrefix(const std::string& prefix) {

std::stack<int> values;

for (int i = prefix.length() - 1; i >= 0; i--) {

char currentChar = prefix[i];

if (isdigit(currentChar)) {

values.push(currentChar - '0');

} else {

int operand1 = values.top();

values.pop();

int operand2 = values.top();

values.pop();

switch (currentChar) {

case '+':

values.push(operand1 + operand2);

break;

case '-':

values.push(operand1 - operand2);

break;

case '*':
values.push(operand1 * operand2);

break;

case '/':

values.push(operand1 / operand2);

break;

case '^':

values.push(static_cast<int>(std::pow(operand1, operand2)));

break;

default:

break;

return values.top();

int main() {

InfixToPrefix converter;

// Test expression

std::string infixExpression = "3 + (4 * 2) - 1";

std::string prefixExpression = converter.convertToPrefix(infixExpression);

std::cout << "Infix expression: " << infixExpression << std::endl;

std::cout << "Prefix expression: " << prefixExpression << std::endl;

int result = converter.evaluatePrefix(prefixExpression);

std::cout << "Evaluation result: " << result << std::endl;


return 0;

You might also like