RPN Calculator in C. A little modification needed.

  • Thread starter Peon666
  • Start date
  • Tags
    Calculator
In summary, the conversation discusses the task of implementing an RPN calculator in C language using stacks and pointers. The code provided is for the driver program and includes a loop for inputting five elements, with a check for digits and non-digits. The main problem discussed is that the code is not performing addition correctly and is not storing values in array2. The poster is seeking help in understanding and resolving this issue.
  • #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! :(
 
Physics news on Phys.org
  • #2
Duplicate of thread [thread=334486]334486[/thread].
 
  • #3




It seems like there may be a few issues with your code. First, it looks like you are not initializing your stack properly. In your main function, you have declared a stackADT variable called "mystack" but you have not actually allocated any memory for it. This means that when you try to push elements onto the stack, it is essentially pushing them onto nothing. To fix this, you can use the "create_stack" function to create a new stack and assign it to your "mystack" variable.

Another issue may be with your use of the "pop" function. In your code, you are passing in the address of the "array" variable as the second parameter for the "pop" function. This means that the "pop" function will try to assign the value of the popped element to the address of "array". However, "array" is an integer array, so it does not have a specific address that can be assigned to. To fix this, you can declare a temporary variable and pass its address as the second parameter for the "pop" function.

Additionally, it looks like you may not be using the "array2" variable at all. You are popping two elements from the stack and assigning them to "array[0]" and "array[1]", but then you are not using those values in your addition operation. Instead, you are using the values from "array2[0]" and "array2[1]", which have not been assigned any values. To fix this, you can either use the values from "array[0]" and "array[1]" or assign the values from "array[0]" and "array[1]" to "array2[0]" and "array2[1]" respectively before performing the addition operation.

Finally, it may be helpful to print out the contents of your stack at various points in your code to see if the elements are being pushed and popped correctly. This can help you identify any other issues with your code.

I hope this helps you in modifying your code and implementing the RPN calculator successfully. Good luck!
 

FAQ: RPN Calculator in C. A little modification needed.

How does the RPN calculator in C work?

The RPN calculator in C follows the Reverse Polish Notation (RPN) method of mathematical expressions where the operands are entered first, followed by the operator. The calculator then performs the operation on the top two operands and replaces them with the result. This process continues until all the operands have been used and the final result is left on the stack.

Can I modify the RPN calculator in C to support more mathematical functions?

Yes, the RPN calculator in C can be modified to support additional mathematical functions by adding them to the code and updating the stack operations accordingly. However, it is important to ensure that the modifications do not affect the accuracy and functionality of the calculator.

What are the advantages of using RPN calculator in C?

The RPN calculator in C has a simple and efficient algorithm that allows for faster calculations. It also has a smaller memory footprint compared to traditional algebraic calculators, making it useful for devices with limited memory. Additionally, the RPN method eliminates the need for parentheses and reduces the chances of errors in complex mathematical expressions.

How can I handle errors in the RPN calculator in C?

To handle errors in the RPN calculator in C, you can implement error handling techniques such as checking for invalid inputs, division by zero, and stack overflows. You can also display error messages to the user and provide options for correcting the input or terminating the program.

Can I use the RPN calculator in C for different data types?

Yes, the RPN calculator in C can be modified to support different data types by changing the stack data structure and updating the operations accordingly. However, it is important to ensure that the modifications do not affect the accuracy and functionality of the calculator.

Similar threads

Replies
3
Views
976
Replies
4
Views
2K
Replies
17
Views
2K
Replies
4
Views
1K
Replies
7
Views
2K
Replies
3
Views
1K
Replies
5
Views
2K
Back
Top