RPN Calculator in C. A little modification needed.

  • Thread starter Thread starter Peon666
  • Start date Start date
  • Tags Tags
    Calculator
Click For Summary
SUMMARY

The forum discussion centers on implementing an RPN (Reverse Polish Notation) calculator in C using stacks and pointers, specifically avoiding linked lists and queues. The user encounters issues with their code where the addition operation does not yield the expected results, as the values popped from the stack are not correctly stored in the intended variables. The primary problem lies in the incorrect handling of the stack operations and variable assignments, leading to unexpected outputs during calculations.

PREREQUISITES
  • Understanding of C programming language
  • Knowledge of stack data structures
  • Familiarity with pointers in C
  • Basic understanding of Reverse Polish Notation
NEXT STEPS
  • Review stack implementation in C, focusing on push and pop operations
  • Learn about handling user input in C, particularly with character arrays
  • Investigate debugging techniques in C to trace variable values during execution
  • Explore the concept of operator precedence and evaluation in RPN calculators
USEFUL FOR

C programmers, computer science students, and anyone interested in implementing data structures and algorithms in C, particularly those focusing on stack-based calculations.

Peon666
Messages
107
Reaction score
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! :(
 
Physics news on Phys.org
Duplicate of thread [thread=334486]334486[/color][/thread].
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
Replies
9
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 12 ·
Replies
12
Views
2K