Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C++ Timesaving Techniques (2005) [eng].pdf
Скачиваний:
65
Добавлен:
16.08.2013
Размер:
8.35 Mб
Скачать

Testing Your Spreadsheet 221

provides access to the individual rows in the sheet, without any knowledge of how the columns are stored in each row.

2. Save the source file in the source-code editor and close the editor application.

Testing Your Spreadsheet

To see that the code is really working, implement a test driver for the code. The following steps show you how:

1. In the code editor of your choice, reopen the source file for the code that you just created.

In this example, the file is named ch39.cpp, although you can use whatever you choose.

2. Append the code from Listing 39-4 to the end of your file.

Better yet, copy the code from the source file on this book’s companion Web site.

LISTING 39-4: THE SPREADSHEET TEST DRIVER

int main(int argc, char **argv)

{

Spreadsheet s1(“Sheet1”, 10, 10 );

// Initialize the spreadsheet.

for ( int i=0; i<s1.NumRows(); ++i ) for ( int j=0; j<s1.NumColumns();

++j )

{

s1[i][j] = “*”; s1[i][j].setFormat(“%6s”);

}

 

 

// Set some values.

 

5

s1[5][4] = “Hello”;

 

s1[0][0] = “Begin”;

 

//Display it so that the user can see

it.

s1.Print();

//Get a slice of the spreadsheet. Spreadsheet s2 = s1(0,0,3,3); s2.setName(“Sheet 2”);

s2.Print();

//Change a column, so we know that it works.

s2[2][2] = “!”; s2.Print();

//Now, clear out the original sheet and display it.

s1.Clear();

s1.Print();

}

When the program is run, if you have done everything properly, you should see the output from Listing 39-5 in the shell window.

LISTING 39-5: THE OUTPUT FROM THE SPREADSHEET TEST DRIVER APPLICATION

Sheet: Sheet1

 

 

 

 

 

 

 

 

Begin

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

Hello

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

(continued)

222 Technique 39: Implementing a Spreadsheet

LISTING 39-5 (continued)

Sheet:

Sheet 2

 

 

Begin

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

Sheet:

Sheet 2

 

 

Begin

*

*

*

*

*

*

*

*

*

!

*

*

*

*

*

Sheet:

Sheet1

 

 

The output shown indicates the state of the spreadsheet at the time it is displayed. An asterisk (*) is shown in any cell that contains no data, while cells that do contain data are shown with the data value. For example, you will see the string Hello in the center of Sheet1, which was placed there at 5 in the code listing for the main driver. Likewise, the top left corner of Sheet1 contains the string Begin, which was placed there at the following line in the driver program.

We could easily use this spreadsheet class to store data, display it for the user, or manipulate data that is contained in a row/column definition.

The asterisks are simply placeholders to show where the actual column data should be. As you can

see, the data values that we set in our test driver show up where they’re supposed to.

You can’t truly implement a two-dimensional array in C++, since there is no operator [][]. However, if you look at the code, you can see a way to implement an operator that returns another class that implements the same operator. The spreadsheet class implements an operator[] (shown by the 4 in Listing 39-3)

which returns the row requested by the index.

 

 

The row class then implements the opera-

tor[] (shown by the

3 line in Listing 39-2)

to return the column requested by the index. That’s why [row][col] = value works.

Part VI

Input and Output