Emma (hearthand) wrote in algorithms,
Emma
hearthand
algorithms

  • Mood:

C

I'm finding C difficult to understand. I'm working using two books: C in a Nutshell and I'm referencing with Comprehensive C (which is old but it was free.) The problem I'm trying to solve is to create a Reverse Polish Notation calculator. I'm using an array as a stack and the basic principle is:
for input (from command prompt) 2 1 +
Push 2 on the stack. Push 1 onto the stack. Meet operator, pop two and then push answer onto the stack.

But I'm having problems with my main method, particularly with the C syntax. This is my main method so far:

/*
 * I need a way for the program to read input separated by space, where a space will
 * indicate the end of a value. For example: 23 3 is the input twenty three and three,
 * not: two, three, three.
 *
 * When an integer operand is encountered, it will be pushed onto the stack. An operator
 * will cause two items of the stack of be popped then a push of the answer.
    argc = SIZE OF THE ARRAY
    argv = Array of strings that represents the command line entered.
 */

int main (int argc, char *argv)

{
    int i;
    for (i = 1; i < argc; i++)
    {
        if (x argv = int)
        {
            push(int x)       

                //http://www.cppreference.com/stdstring/isdigit.html
                //http://www.cppreference.com/stdstring/atoi.html
    }
}

The hyperlinks are a link to two methods from the library that I need to use: isdigit() and atoi(). Because the input is being entered from the command prompt it's my understanding that a character and an integer cannot be differentiated simply by their bit pattern. What I need to do with the input is take the "string", convert it to an integer and then check to see if it is an integer or not.

Now, in Java I'd do something like x.isdigit().atoi() but... gah! There's a lot of true/false requirements in there and I don't think I actually fully understand exactly what I'm trying to do.

This is what I'm planning to do, in English:

If ( x is a character) {
    either return 0 as value
   or set next item to memory location [x]
}
if (x is an operator) {
   POP two from the stack
   use operator on two operands
   Push answer onto the stack again
      //error checking for condition that only one value has been pushed onto stack
}
//otherwise, fall through assuming that x is an integer
convert x into an integer using atoi()
Push (x)

TADA!

I'm planning on using if for error checking and converting everything else to case statements instead. I'm use to programming in java, though, so I'm scared that I'm java-ing too much :/

Advice? Kicks? Clarifications?
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 55 comments