Witam. Piszę kalkulator obsługujący zapis RPN. Poniższy kod jest oczywiście w fazie początkowej (wiem, że jest namieszane C z CPP, ale na mojej uczelni na AiSD jest to dopuszczalne) i sporo w nim brakuje, ale już teraz napotkałem na pewien problem. Nie wiem czemu fragment:
cout << stack[top-1] << endl;
nic nie zwraca - jak widać zmienna jest pusta. Pytanie brzmi dlaczego, skoro wywołałem funkcję push wrzucającą element na szczyt stosu? Z góry dziękuję za pomoc.
#include <string>
#include <iostream>
#include <cstdio>
using namespace std;
#define STACKMAX 100
char stack[STACKMAX], *element, temp;
int top;
void push(int temp)
{
if(top < STACKMAX)
{
/* place the element at the top of the stack */
stack[top] = temp;
top++;
}
}
int pop()
{
if(top != 0)
{
/* take the element from the stack */
top--;
return stack[top];
}
}
int main()
{
char input[STACKMAX];
cout << "Write the equation in Reverse Polish Notation:\n";
fgets (input, STACKMAX, stdin);
cout << input;
element=strtok(input, " ");
while( element != NULL )
{
cout << element << endl;
temp=atoi(element);
push(temp);
cout << stack[top-1] << endl;
element = strtok( NULL, " " );
}
system ("pause");
}