The switch Statement

There are numerous possibilities for an arithmetic operator. We will implement addition, subtraction, multiplication and integer division (our calculator will only deal with integer arithmetic and not make use of floating point numbers, although it would not be impossible to alter this). Instead of having a separate if clause for each possibility however, there is a useful statement which C provides for dealing with numerous cases. It is called a switch statement. We implement it as follows:

switch (strPointer[0])
{
    case '+': 
    {
        //implement addition here
    } break;
    case '-': 
    {
        //implement subtraction here
    } break;
    case '*': 
    {
        //implement multiplication here
    } break;
    case '/': 
    {
        //implement division here
    } break;
    case ' ': break;
    case '\n':
    {
        //check that the stack is empty
        //display the result of the computation and return
    }
    default:
    {
        //alert the user that something has gone wrong
        return;	
    }
}

There are two parts of this which require explanation. Following each case you will see the word break. This tells C to break out of the switch construct and continue execution of the program. The default is to just continue on through all of the statements in the other cases as well. This is useful in certain limited situations, but usually we just want to break out when we have completed executing the appropriate case. The other thing to note is the default case. This is executed if none of the other cases are. In our program, if the user has entered a non-digit which is not an arithmetic operation or a space and not a carriage return, then we have an error and need to inform the user that the input was incorrect.

The arithmetic operations are all implemented in a similar way. Here is the code for the addition case:

if (stackTop != NULL)
{
    currentNum+=stackTop->numdata;
    stackTop = popItem(stackTop);
} else
{
    printf("Error! Too many arithmetic operations.\n");
    return;
}

By checking whether stackTop points to NULL or not, we can determine whether there are any nodes on the stack. Of course there ought to be if we are going to perform an addition with currentNum, otherwise we need to return an error.

We haven't yet defined the popItem function, but we will do so momentarily.

For the case where a carriage return is encountered, we ought to be at the end of the computation, and the stack ought to be empty. The lines we need to insert for this case are:

if (stackTop == NULL)
{
    printf("The result of the computation is: %d.\n",currentNum);
    return;
} else
{
    printf("Error! Too many numbers.\n");
    while (stackTop != NULL) stackTop = popItem(stackTop);
    return;
}

Notice that we use a double equals sign to test for equality. This is to distinguish from an assignment where we are setting a variable equal to some value. A major source of errors for beginners in the C language is to forget to put a double equals sign when testing for equality in conditional statements.