Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: arctic_kooks en 27 Junio 2011, 03:01 am



Título: Reducir stack con expresiones aritméticas en prefix.
Publicado por: arctic_kooks en 27 Junio 2011, 03:01 am
Realice este programa, pero ahora tengo que resolverlo, me podrían ayudar?  :-\

Tengo este programa, sobre este tengo que Reducir el stack con expresiones aritmeticas:

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include <cstdlib>
#include <iostream>

using namespace std; //Habilita funciones cout, cin, endl

char stack[20];
int top = -1;
char pop();
void push(char item);

int prcd(char symbol) {
switch(symbol) {
case '+':
case '-':
return 2;
case '*':
case '/':
return 4;
case '^':
return 6;
case '(':
case ')':
case '#':
return 1;
}

}

int isoperador(char symbol) {
switch(symbol) {
case '+':
case '-':
case '*':
case '/':
case '^':
case '(':
case ')':
return 1;
default:
return 0;
}

}

void convertirp(char infix[],char prefix[]) {
int i,symbol,j=0;
char test[20];

infix=strrev(infix);
stack[++top]='#';

for(i=0;i<strlen(infix);i++) {
symbol=infix;
if(isoperador(symbol)==0) {
prefix[j]=symbol;
j++;
}else {
if(symbol==')') {
push(symbol);
}else if(symbol=='(') {
while(stack[top]!=')') {
prefix[j]=pop();
j++;
}
pop();
}else {
if(prcd(symbol)>prcd(stack[top])) {
push(symbol);
}else {
while(prcd(symbol)<=prcd(stack[top])) {
prefix[j]=pop();
j++;
}
push(symbol);
}//fin else.
}//fin else.
}//fin else.
}//fin for.

while(stack[top]!='#') {
prefix[j]=pop();
j++;
}
prefix[j]='\0';
prefix=strrev(prefix);


}

int main() {
char infix[20],prefix[20];
//printf("Entra la función en infix:\n");
//gets(infix);
cout<<"-------------------------------…
cout<<"Expresión en infix: ";
cin>>infix;
convertirp(infix,prefix);
cout<<"---------------Resultados------…
//printf("El prefix es:\n");
//puts(prefix);
cout<<"El prefix es: + : "<<prefix<<endl;
getch();

return 0;

}

void push(char item) {
top++;
stack[top]=item;

}

char pop() {
char a;
a=stack[top];
top--;
return a;

//system("pause");
}