Da qualche giorno sbatto la testa sull'algoritmo che permette di ottenere un'espressione in notazione postfissa (o reverse polish notation) partendo da una stringa in notazione prefissa per creare una calcolatrice. In particolare vorrei che lavorasse anche e soprattutto con numeri in virgola mobile. Nonostante la teoria mi è abbastanza chiara faccio degli errori nell'implementazione (non mi da nemmeno errori di compilazione).
Riporto il codice:
codice:
void rpNotation (char *iString, char *oString) {
int in = 0, out = 0;
char tmpCh;
stack lStack = stackInit (); /*la funzione si appoggia a uno stack creato da me a parte*/
while ((tmpCh = iString [in]) != '\0') { /*salvo in tmpCh un carattere proveniente dalla stringa iString (in è un semplice indice)*/
switch (tmpCh) {
case '(':
stackPush (lStack, tmpCh);
break;
case ')':
tmpCh = stackPop (lStack);
while (tmpCh != '(') {
oString [out++] = tmpCh;
tmpCh = stackPop (lStack);
}
break;
case '+':
while (showTop (lStack) != '(' || !stackEmpty (lStack)) {
if (showTop (lStack) == '-' || '*' || '/') {
oString [out] = ' ';
out++;
oString [out] = stackPop (lStack);
out++;
} else {
stackPush (lStack, tmpCh);
break;
}
}
break;
case '-':
while (showTop (lStack) != '(' || stackEmpty (lStack)) {
if (showTop (lStack) == '+' || '*' || '/') {
oString [out] = ' ';
out++;
oString [out] = stackPop (lStack);
out++;
} else {
stackPush (lStack, tmpCh);
break;
}
}
break;
case '*':
while (showTop (lStack) != '(' || stackEmpty (lStack)) {
if (showTop (lStack) == '/') {
oString [out] = ' ';
out++;
oString [out] = stackPop (lStack);
out++;
} else {
stackPush (lStack, tmpCh);
break;
}
}
break;
case '/':
while (showTop (lStack) != '(' || stackEmpty (lStack)) {
if (showTop (lStack) == '*') {
oString [out] = ' ';
out++;
oString [out] = stackPop (lStack);
out++;
} else {
stackPush (lStack, tmpCh);
break;
}
}
break;
default:
if (isdigit (tmpCh) || tmpCh == '.') {
oString [out] = tmpCh;
out++;
}
}
in++;
}
}