I'm trying to make a reverse polish notation calculator.
One of my first steps is to figure out whether an operand (number) was entered or an operator/letter (l, p, q, +, -, *, or /).
Here's what I have for this part:
Code:
char buff[MAX_BUFFER_SIZE], o;
/* This reads a string from the stack */ if( scanf("%s", buff) < 1 )
{
printf("I did not understand\n");
return 0;
}
/* checks to see if the user entered a number */
if(isdigit(buff[0]) || isdigit(buff[1]))
{
/*turns string into number and puts value in x*/
sscanf( buff, "%lf", &x);
/* place statements here to handle getting a number */
} else {
/* place statements to do operations */
sscanf( buff, "%lf", &o);
if (o == 'l') {
printf("its l");
}
else if (o == 'q') {
printf("its q");
}
else (o == 'p'); {
printf("its p");
}
Obviously the intention of this part is not to print "its _" but I have it set for troubleshooting.
When I compile, any operator or letter I enter will print "its p".
I had it as a switch, but gave up on that and am now trying if/else.
Any tips will be great.
NOTE: We haven't learned isdigit() or sscanf() in class (that part of the program was given to us by our instructor), so therefore we are not allowed to use it anywhere else in the program.