urgentisimo
Publicado por Tati (1 intervención) el 23/10/2007 11:48:49
programa para comvertir de expresiones infijas a postfijas, gracias por la ayuda
Valora esta pregunta


0



#include <iostream>#include <stack>#include <cctype>using namespace std;
int precedence(char op) {
if (op == '+' || op == '-') return 1;
if (op == '*' || op == '/') return 2;
return 0;
}string infixToPostfix(const string& infix) {
stack<char> s;
string postfix;
for (char ch : infix) {
if (isalnum(ch)) {
postfix += ch;
} else if (ch == '(') {
s.push(ch);
} else if (ch == ')') {
while (!s.empty() && s.top() != '(') {
postfix += s.top();
s.pop();
}s.pop(); // Elimina el '(' de la pila
} else {
while (!s.empty() && precedence(ch) <= precedence(s.top())) {
postfix += s.top();
s.pop();
}s.push(ch);
} }while (!s.empty()) {
postfix += s.top();
s.pop();
}return postfix;
}int main() {
string infixExpression;
cout << "Ingrese la expresión infija: ";
getline(cin, infixExpression);
string postfixExpression = infixToPostfix(infixExpression);
cout << "Expresión postfija: " << postfixExpression << endl;
return 0;
}
