Date: 03/28/08 (Algorithms) Keywords: programming, html, java 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: /* * 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? Source: http://community.livejournal.com/algorithms/98390.html
|