Próbuję dodać do algorytmu Odwrotnej Notacji Polskiej dodatkowe funkcje ( żeby interpretował też sinusy, log, itd).
Wszystko działa fajnie, ale nie do końca. Jeżeli w wyrażeniu pojawia się sinus, albo inna z tych dodatkowych funkcji to obcina mi na wyjściu ostatnią literę:
Przykład:
dla: s i n 2 (oczywiście odpowiada to sin(2) )
zwraca: s i 2
Czy ktoś ma pomysł jak wyeliminować ten błąd?
Kod programu:
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <string.h>
#include <math.h>
using namespace std;
int main(int argc, char *argv[])
{
char stos[256],el[256], wyjscie[256];
int ws = 0, i=0, j=0;
do
{
cin >> el;
if(isdigit(el[i]))
{
cout << el << " ";
wyjscie[j]=el[i];
}
else
switch(el[i])
{
case '+': ;
case '-': while(ws && stos[ws - 1] != '(' ) cout << stos[--ws] << " ";
wyjscie[j]=stos[ws];
stos[ws++] = el[i];
break;
case '*': ;
case '/': while(ws && stos[ws - 1] != '(' &&
stos[ws - 1] != '+' &&
stos[ws - 1] != '-') cout << stos[--ws] << " ";
wyjscie[j]=stos[ws];
stos[ws++] = el[i];
break;
case '^': while(ws && stos[ws - 1] == '^') cout << stos[--ws] << " ";
wyjscie[j]=stos[ws];
stos[ws++] = el[i];
break;
case '(': stos[ws++] = '(';
break;
case ')': while(stos[ws - 1] != '(') cout << stos[--ws] << " ";
ws--;
break;
case 'c': while(ws && stos[ws - 1] != '(' ) cout << stos[--ws] << " ";
wyjscie[j]=stos[ws];
stos[ws++] = el[i];
break;
case 'g': while(ws && stos[ws - 1] != '(' ) cout << stos[--ws] << " ";
wyjscie[j]=stos[ws];
stos[ws++] = el[i];
break;
case 'i': while(ws && stos[ws - 1] != '(' ) cout << stos[--ws] << " ";
wyjscie[j]=stos[ws];
stos[ws++] = el[i];
break;
case 'l': while(ws && stos[ws - 1] != '(' ) cout << stos[--ws] << " ";
wyjscie[j]=stos[ws];
stos[ws++] = el[i];
break;
case 'n': while(ws && stos[ws - 1] != '(' ) cout << stos[--ws] << " ";
wyjscie[j]=stos[ws];
stos[ws++] = el[i];
break;
case 'o': while(ws && stos[ws - 1] != '(' ) cout << stos[--ws] << " ";
wyjscie[j]=stos[ws];
stos[ws++] = el[i];
break;
case 's': while(ws && stos[ws - 1] != '(' ) cout << stos[--ws] << " ";
wyjscie[j]=stos[ws];
stos[ws++] = el[i];
break;
case 't': while(ws && stos[ws - 1] != '(' ) cout << stos[--ws] << " ";
wyjscie[j]=stos[ws];
stos[ws++] = el[i];
break;
case '=': while(ws) cout << stos[--ws] << " ";
wyjscie[j]=stos[ws];
cout << " =\n\n";
break;
}
j++;
} while(el[i] != '=');
system("PAUSE");
return EXIT_SUCCESS;
}
WAŻNE: Ten program nie jest w całości mój! Jest to kod znaleziony na http://edu.i-lo.tarnow.pl/inf/utils/010_2010/0410.php.