Like Tree1Likes
  • 1 Post By quzah

RPN Calculator

This is a discussion on RPN Calculator within the C Programming forums, part of the General Programming Boards category; Hi, I'm in the process of building a RPN calculator, but I am confused about how to store data from ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    9

    RPN Calculator

    Hi,

    I'm in the process of building a RPN calculator, but I am confused about how to store data from the user. If you take a look at the attachment picture, you will understand what I mean:

    Name:  RPNCalcInfo.jpg
Views: 563
Size:  105.4 KB

    The Hints: section wants me to use the following code to read operators and operands:

    Code:
    scanf("%c", &ch);
    If I'm entering long data, how is that possible?

    Code:
    Enter an RPN expression: 1 2 3 * + =
    I would appreciate any help.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You're not entering long data in your example. 1, 2, 3, *, + are all single characters and can be read quite easily using %c. If you wanted to allow two-digit numbers, then things might get a little interesting; but even then, you'd probably want to read one character at a time.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,036
    The hint says to use scanf(" %c", &ch);. Note the space.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    9
    Quote Originally Posted by King Mir View Post
    The hint says to use scanf(" %c", &ch);. Note the space.
    Aha, I missed that space! I will try to work it out, thanks.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    You need to insert each char (it'll be ASCII - convert to a number) into an array (the array is used as the stack) and process this array of numbers whenever you receive an operator (*, / etc) instead of a number.

    Seems the examples in the attached assume single-digit numbers with no use of an "enter" key (or its equivalent). Not sure if that's part of your requirements?

    Ever use an HP calculator?

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    9
    Okay, I've gotten this far:

    Code:
    //** Include Files ******************************************************
    #include <stdio.h>
    #include <stdlib.h>
    
    //** Defines ************************************************************
    #define		STACK_SIZE		100
    
    //** External Variables *************************************************
    int contents[STACK_SIZE];
    
    
    int main()
    {
    	int i, j;
    	char ch;
    	
    	printf("Enter integer: "); 
    	
    	i = 0;
    	
    	do
    	{
    		scanf(" %c", &contents[i]);
    		printf("You entered %c \r\n", contents[i]);
    	}while(contents[i++] != '0');
    	
    	for(j = i; j >= 0; j--)
    	{
    		printf("contents[%d] = %c \r\n", j, contents[j]);
    	}
    	printf("\n");
    	
    	system("PAUSE");
    	
    }
    Notice that I use a 0 char to terminate the loop, but there is a flaw in doing that: what if I have a 0 in the middle of my data? I tried a null, but that doesn't seem to work.
    Last edited by ElectroNerd; 06-06-2011 at 02:43 PM.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The example in the picture is using a q not a 0 to terminate the loop.


    Quzah.
    mike65535 likes this.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculator in c...
    By nobita in forum C Programming
    Replies: 1
    Last Post: 11-28-2009, 08:22 AM
  2. Calculator
    By Justinjah91 in forum C Programming
    Replies: 5
    Last Post: 08-18-2009, 12:56 PM
  3. Please help with this calculator
    By Asbestos in forum C++ Programming
    Replies: 9
    Last Post: 03-08-2005, 12:00 PM
  4. Calculator
    By C++programming in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2004, 06:15 PM
  5. calculator in c++
    By gamer in forum C++ Programming
    Replies: 11
    Last Post: 02-19-2003, 09:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21