Ok, I am writing a program to read a line of input by the user/from a datafile, and manipulating this data using the ADT stacks. Im having an issue. I have to read in a line of input similar to the following.
7 3 12 * % 3 13 / + 6 - 17 + p
Each integer it reads, it has to place it in the stack, but if it reads a operator, then it performs that with the top 2 number on the stack (ex. when it reads * in the line above, it will multiply 12 and 3.
The problem im having is, how can i make c know if what it is reading is an integer or a character. I tried this with just integers, using scanf and %d as the placeholder. It worked fine, and placed the number in the stack. But as soon as i entered the operators in the line, it gives me a segmentation fault. As of right now, its just scanning everything as a integer, but obviously if there is a + sign, that is not an integer. Any idea how i could read a whole line of data input with integers and characters?
I wish i could just do:
etc, etc. But i dont know how to do this, and i would still be lost on what placeholder to use when scanfing. Any help would be appreciated.Code:if ( input = integer) place in stack; //pseudocode else { if (input = '+'); { perform addition; } else if (input = '-' ); { perform subtraction; }
Thanks
Alex