home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_06 / vancamp / tdexmpl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-02  |  2.7 KB  |  93 lines

  1. // tdexmpl.cpp: Example use of TableData (LISTING 6)
  2. #include <stdio.h>
  3. #include <assert.h>
  4. #include <iostream.h>
  5. #include <iomanip.h>
  6. #include "tdexmpl.hpp"
  7. #include "tdnorm.hpp"
  8. #include "tdavg.hpp"
  9.  
  10. // For ease of specifying type:
  11. typedef TableData<ValAttrCell> ValAttrTable;
  12. // Local function prototypes:
  13. static void PrintTable(ValAttrTable *, ValAttrTable *);
  14. static void PrintRow(ValAttrTable *, int);
  15.  
  16. int main ()
  17. {
  18.     // First instantiate the buffer
  19.     ValAttrTable *aTableDataMarketBuf = new
  20.             TableDataMarketBuf<ValAttrCell>
  21.             ((ValAttrTable *)0, 4, 4);
  22.     assert (aTableDataMarketBuf != 0);
  23.  
  24.     // Instantiate filters: first one normalizes
  25.     ValAttrTable *aTableDataNormalize = new
  26.             TableDataNormalize<ValAttrCell>
  27.             (aTableDataMarketBuf);
  28.     assert (aTableDataNormalize != 0);
  29.  
  30.     // Next one averages the original data:
  31.     ValAttrTable *aTableDataAverage1 = new
  32.             TableDataAverage<ValAttrCell>
  33.             (aTableDataMarketBuf);
  34.     assert (aTableDataAverage1 != 0);
  35.  
  36.     // Next one averages the normalized data:
  37.     ValAttrTable *aTableDataAverage2 = new
  38.             TableDataAverage<ValAttrCell>
  39.             (aTableDataNormalize);
  40.     assert (aTableDataAverage2 != 0);
  41.  
  42.     cout << "Original data:" << endl;
  43.     PrintTable (aTableDataMarketBuf,
  44.             aTableDataAverage1);
  45.  
  46.     cout << endl << "After normalizing:" << endl;
  47.     PrintTable (aTableDataNormalize,
  48.             aTableDataAverage2);
  49.  
  50.     delete aTableDataAverage2;
  51.     delete aTableDataAverage1;
  52.     delete aTableDataNormalize;
  53.     delete aTableDataMarketBuf;
  54.     return (0);
  55. }
  56.  
  57. static void PrintTable (ValAttrTable *topTD,
  58.         ValAttrTable *avgTD)
  59. {
  60.     // Print the table: First the column headings
  61.     cout << setw(11) << " ";
  62.     for (int col = 0; col < topTD->GetNumCols(); col++)
  63.         cout << setw(10)
  64.                 << &(topTD->GetColHeading (col));
  65.     cout << endl;
  66.  
  67.     for (int row = 0; row < topTD->GetNumRows(); row++)
  68.         PrintRow (topTD, row);
  69.  
  70.     // Then print the averages:
  71.     PrintRow (avgTD, 0);
  72. }
  73.  
  74. static void PrintRow (ValAttrTable *topTD, int row)
  75. {
  76.     cout << setw(13) << &(topTD->GetRowHeading (row));
  77.     // Set up money-style output for floats:
  78.     cout.precision(2);
  79.     cout.setf(ios::fixed,ios::floatfield);
  80.     cout.setf(ios::right);
  81.     for (int col = 0; col < topTD->GetNumCols(); col++)
  82.     {
  83.         ValAttrCell cell = topTD->GetCell (row, col);
  84.         cout << setw(8) << cell.GetValue();
  85.         // Show protected cells with an asterisk
  86.         if (cell.GetAttr() & ValAttrCell::CT_PROTECTED)
  87.             cout << "* ";
  88.         else
  89.             cout << "  ";
  90.     }
  91.     cout << endl;
  92. }
  93.