The Tape

The Tape() function is fairly simple, and is coded in main.cpp.

Listing 12.1. Tape() in main.cpp
 1: void Tape(const char theOperator,const float theOperand)
 2: {
 3:    static const int myTapeSize = 20; // Array size
 4:
 5:    static char myOperator[myTapeSize]; // Operator part
 6:    static float myOperand[myTapeSize]; // Operand part
 7:
 8:    static int myNumberOfEntries = 0; // What's in tape now
 9:
10:    // Remember that arrays start with element 0
11:    // And that the highest element is the size - 1;
12:
13:    if (theOperator != '?') // Add to the tape
14:    {
15:       if (myNumberOfEntries  < myTapeSize) // We have room
16:       {
17:          myOperator[myNumberOfEntries] = theOperator;
18:          myOperand[myNumberOfEntries] = theOperand;
19:          myNumberOfEntries++;
20:       }
21:       else // About to overflow the array
22:       {
23:          throw runtime_error[ccc]
                ("Error - Out of room on the tape.");
24:       } ;
25:    }
26:    else // Display the tape
27:    {
28:       for ( int Index = 0; Index < myNumberOfEntries; Index++ )
29:       {
30:          cout << myOperator[Index] << "," << myOperand[Index] << endl;
31:       } ;
32:    } ;
33: }

Lines 3–9 set up the tape. Notice how these are all static local variables, like myAccumulator in Accumulate(). Thus, they are initialized when the program starts and retain their contents from one call to the next.


Line 3 controls the maximum size of the arrays. Arrays require a constant expression to identify their size when they are created, and line 3 sets up that constant and gives it a name. Because the arrays are static, their size constant needs to be static too, or it will not have been initialized when the arrays are created at the start of the program run.

Lines 5 and 6 are the operator and operand arrays that are the actual tape. You might say they are parallel, because the operator in the first element of the myOperator array is the one used with the operand in the same element of the myOperand array.

Line 8 keeps a count of how many elements there are in the arrays so far. You need this because the arrays have a fixed size, and you have to throw an exception when the user has entered so many operator/operand pairs that the array will overflow. Line 32 updates this count.

Lines 10 and 11 provide a little reminder.

Line 13 checks to see whether theOperator is the array that causes the tape contents to be displayed. If so, control flows to line 27.

Line 15 checks to see whether there is room left on the tape. If myNumberOfEntries == myTapeSize, the index is about to go one past the end of the array. Rather than allow this, line 23 throws a runtime_error exception, which is caught in main().

Line 17 starts to actually add to the tape. This line sets the appropriate element of each of the arrays using an index that is, by definition, one above the last element used—myNumberOfEntries. This is a C++ trick based on the fact that arrays are indexed from 0, and the element count is based on 1, so the index of the new entry is always the same as the count.

Line 19 introduces a new arithmetic operator—the double plus (++). This is the postfix increment operator, a very commonly used operator. It will add 1 to the variable named before it. This line updates myNumberOfEntries to the appropriate count, so you can check it the next time through and make sure that you don't overrun the array bounds.

Line 28 is in the block that is performed when theOperator is ?, which means “display the tape.” In that block, a for loop is used to move through the array so that the elements can be displayed with cout.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.107.60