- #1
Peon666
- 108
- 0
Homework Statement
To implement an RPN (Reverse Polish Notation) calculator in C language.
Homework Equations
Implement with the help of Stacks and Pinters. No linked lists or Queues. Keep it simple.
The Attempt at a Solution
Here's my code: (only the driver programe)
int main(int argc, char *argv[])
{
int i, rc;
char elements[5];
char *ptr;
int array[2];
int array2[2];
int temp, temp2;
stack_elem_t elem;
stackADT mystack = NULL;
printf ("Enter five elements, operants and operators included.\n");
for (i=0; i<5; i++)
{
scanf ("%s", &elements);
if (isdigit(elements))
{
// array = strtol (elements, &ptr , 0);
array = elements - '0';
push(mystack, array);
}
if (!isdigit(elements))
{
switch (elements)
{
case '+':
if (i < 2)
printf ("Not enough operators to perform addition operation!\n");
array2[0] = pop(mystack, &array[0]); // Output: 1
array2[1] = pop(mystack, &array[1]); // Output: 1
printf ("%d\n", array[0]); // Output: 3 (as I entered)
printf ("%d\n", array[1]); // Output: 4 (as I entered)
temp = (array[0]+array[1]);
push (mystack, temp);
printf ("%d\n", temp); // Output: 1
temp2 = pop (mystack, &temp);
printf ("%d\n",temp2); // Output: 1
break;And MAIN problem is:
Now the problem is that this does not add the two integers. Why is this happening? Besides, it does not take anything in array2[0] and array2[1]. When I pop the elements array[0] and array[1] into array2[0] and array2[1] and print them out, only 1 is printed out. What could be the problem?
Further, when I add array[0] and array[1] into temp and print temp out, again, 1 is printed and nothing else. However, if I print out array[0] and array[1] separately, they are displayed as I enter them, i.e 3 and 4.
What's going on? I'm really troubled about this fiasco! HELP ME OUT! :(