DSL Assignment
DSL Assignment
Sub DsLab.
Roll no 22-cs-438
#include <iostream>
#include <stack>
#include <string>
#include <unordered_map>
class InfixToPrefix {
public:
InfixToPrefix();
private:
};
InfixToPrefix::InfixToPrefix() {}
{'^', 3}
};
return precedence_map[op];
std::stack<char> operators;
std::stack<std::string> operands;
if (isdigit(currentChar)) {
operands.push(std::string(1, currentChar));
operators.push(currentChar);
while (!operators.empty() && (operators.top() != ')' && operators.top() != ']' && operators.top() !
= '}')) {
char op = operators.top();
operators.pop();
operands.pop();
operands.pop();
operands.push(temp);
}
operators.pop();
} else {
char op = operators.top();
operators.pop();
operands.pop();
operands.pop();
operands.push(temp);
operators.push(currentChar);
while (!operators.empty()) {
char op = operators.top();
operators.pop();
operands.pop();
operands.pop();
std::string temp = op + operand2 + operand1;
operands.push(temp);
return operands.top();
std::stack<int> values;
if (isdigit(currentChar)) {
values.push(currentChar - '0');
} else {
values.pop();
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