home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / vbdatabs / testf64.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-17  |  7.8 KB  |  263 lines

  1. // ------------------------------- //
  2. // -------- Start of File -------- //
  3. // ------------------------------- //
  4. // ----------------------------------------------------------- // 
  5. // C++ Source Code File Name: testf64.cpp 
  6. // Compiler Used: MSVC40, DJGPP 2.7.2.1, GCC 2.7.2.1, HP CPP 10.24
  7. // Produced By: Doug Gaer   
  8. // File Creation Date: 09/05/1997  
  9. // Date Last Modified: 03/17/1999
  10. // Copyright (c) 1997 Douglas M. Gaer
  11. // ----------------------------------------------------------- // 
  12. // ------------- Program Description and Details ------------- // 
  13. // ----------------------------------------------------------- // 
  14. /*
  15. This is a test program for the FLOAT64 class.
  16.  
  17. The VBD C++ classes are copyright (c) 1997, by Douglas M. Gaer.
  18. All those who put this code or its derivatives in a commercial
  19. product MUST mention this copyright in their documentation for
  20. users of the products in which this code or its derivative
  21. classes are used. Otherwise, you have the freedom to redistribute
  22. verbatim copies of this source code, adapt it to your specific
  23. needs, or improve the code and release your improvements to the
  24. public provided that the modified files carry prominent notices
  25. stating that you changed the files and the date of any change.
  26.  
  27. THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
  28. THE ENTIRE RISK OF THE QUALITY AND PERFORMANCE OF THIS SOFTWARE
  29. IS WITH YOU. SHOULD ANY ELEMENT OF THIS SOFTWARE PROVE DEFECTIVE,
  30. YOU WILL ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR
  31. CORRECTION.
  32. */
  33. // ----------------------------------------------------------- // 
  34. #include <iostream.h>
  35. #include <stdio.h>
  36. #include "float64.h"
  37.  
  38. void pause()
  39. {
  40.   cout << endl;
  41.   cout << "Press enter to continue..." << endl;
  42.   cin.get();
  43. }
  44.  
  45. void SkipToEol(istream &s)
  46. // Used to clear istream
  47. {
  48.   char c;
  49.   s.clear();
  50.   while(s.get(c) && c != '\n') { ; }
  51. }
  52.  
  53. void FWrite(char *fname)
  54. {
  55.   FLOAT64 aa = 3.141592653589793; // Arc cosine of -1 equals pi
  56.   cout.precision(15);  // Show 15 places after the decimal point 
  57.   cout.setf(ios::fixed | ios::right); // Normal FP notation right justified
  58.   cout << "Writing a FLOAT64 value " << aa << " to a file..." << endl;
  59.   ios::sync_with_stdio();
  60.   FILE *stream;
  61.   int numwritten;
  62.  
  63.   /* Open file in binary mode: */
  64.   if((stream = fopen( fname, "w+b" )) != NULL )
  65.     {
  66.       numwritten = fwrite((char *)&aa, 1, sizeof(FLOAT64), stream );
  67.       printf( "Wrote %d items\n", numwritten );
  68.       fclose( stream );
  69.     }
  70.   else
  71.     printf( "Problem opening the file\n" );
  72. }
  73.  
  74. void FRead(char *fname)
  75. {
  76.   FLOAT64 bb = 0;
  77.   ios::sync_with_stdio();
  78.   FILE *stream;
  79.   int numread;
  80.  
  81.   cout.precision(15);  // Show 15 places after the decimal point 
  82.   cout.setf(ios::fixed | ios::right); // Normal FP notation right justified
  83.   cout << "Reading back FLOAT64 value from the file..." << endl;
  84.   if( (stream = fopen( fname, "r+b" )) != NULL )
  85.     {
  86.       numread = fread((char *)&bb, 1, sizeof(FLOAT64), stream );
  87.       printf( "Number of items read = %d\n", numread );
  88.       fclose( stream );
  89.     }
  90.   else
  91.     printf( "File could not be opened\n" );
  92.   
  93.   cout << "Value read from file = " << bb << endl;
  94.  
  95. }
  96.  
  97. template<class TYPEX, class TYPEY>
  98. inline void OperatorTest(TYPEX x, TYPEY y)
  99. {
  100.   char *tf[2] = {"FALSE", "TRUE"};
  101.   cout << endl;
  102.   cout << "Value x = " << x << ", Value y = " << y << endl;
  103.   TYPEX z;
  104.   cout << x << " += " << y << " = "; z = x; z += y; cout << z << endl;
  105.   cout << x << " -= " << y << " = "; z = x; z -= y; cout << z << endl;
  106.   cout << x << " *= " << y << " = "; z = x; z *= y; cout << z << endl;
  107.   cout << x << " /= " << y << " = "; z = x; z /= y; cout << z << endl;
  108.   cout << x << " * " << y << " = " ; z = (x * y); cout << z << endl;
  109.   cout << x << " / " << y << " = " ; z = (x / y); cout << z << endl;
  110.   cout << x << " + " << y << " = " ; z = (x + y); cout << z << endl;
  111.   cout << x << " - " << y << " = " ; z = (x - y); cout << z << endl;
  112.   cout << x << " == " << y << " = " << tf[(x == y)] << endl;
  113.   cout << x << " != " << y << " = " << tf[(x != y)] << endl;
  114.   cout << x << " <= " << y << " = " << tf[(x <= y)] << endl;
  115.   cout << x << " >= " << y << " = " << tf[(x >= y)] << endl;
  116.   cout << x << " < " << y << " = " << tf[(x < y)] << endl;
  117.   cout << x << " > " << y << " = " << tf[(x > y)] << endl;
  118.   cout << x << "++ = "; z = x; z++; cout << z << endl;
  119.   cout << x << "-- = "; z = x; z--; cout << z << endl; 
  120.   cout << "++" << x << " = "; z = x; ++z; cout << z << endl;
  121.   cout << "--" << x << " = "; z = x; --z; cout << z << endl;
  122. }
  123.  
  124. int main()
  125. {
  126.   FLOAT64 a = DPFLOATPositiveLimit;
  127.   cout << "FLOAT64 positive limit = " << a << endl;
  128.  
  129.   FLOAT64 b(DPFLOATNegitiveLimit);
  130.   cout << "FLOAT64 negitive limit = " << b << endl;
  131.  
  132.   pause();
  133.   
  134.   cout << "Testing FLOAT64 copy consturctor..." << endl;
  135.   FLOAT64 c(a);
  136.   cout << c << endl;
  137.  
  138.   cout << endl;
  139.   cout << "Testing FLOAT64 assignment operator..." << endl;
  140.   FLOAT64 d;
  141.   d = a;
  142.   cout << d << endl;
  143.  
  144.   pause();
  145.  
  146.   // Reading and writing a FLOAT64 value to a file
  147.   char fname[255] = "testf64.out";
  148.   FILE *tmp; // Temporary file pointer
  149.   tmp = ::fopen(fname, "rb");
  150.  
  151.   if(tmp) FRead(fname); else FWrite(fname);
  152.     
  153.   pause();
  154.   
  155.   cout.precision(10);  // Show 10 places after the decimal point 
  156.   cout.setf(ios::fixed | ios::right); // Normal FP notation right justified
  157.   cout << "Testing overloaded operators (FLOAT64, FLOAT64)..." << endl;
  158.   __DPFLOAT__ buf1, buf2, af, bf;
  159.   
  160.   cout << "Enter first floating point value: ";
  161.   cin >> buf1;
  162.   if(cin) {
  163.     a = buf1;
  164.     af = buf1;
  165.     cout << "Enter second floating point value: ";
  166.     cin >> buf2;
  167.   }
  168.   else {
  169.     cout << "Input stream broken. Exiting..." << endl;
  170.     return 0;
  171.   }
  172.   if(cin) {
  173.     b = buf2;
  174.     bf = buf2;
  175.   }
  176.   else {
  177.     cout << "Input stream broken. Exiting..." << endl;
  178.     return 0;
  179.   }
  180.  
  181.   SkipToEol(cin);
  182.     
  183.   cout << endl;
  184.   
  185.   OperatorTest(a, b);
  186.   
  187.   pause();
  188.  
  189.   cout << "Testing overloaded operators (FLOAT64, __DPFLOAT__)..." << endl;
  190.   OperatorTest(a, bf);
  191.  
  192.   pause();
  193.  
  194.   cout << "Testing overloaded operators (__DPFLOAT__, FLOAT64)..." << endl;
  195.   OperatorTest(af, b);
  196.   
  197.   pause();
  198.  
  199.   cout << "Testing overloaded operators (FLOAT64, __LWORD__)..." << endl;
  200.   OperatorTest(a, (__LWORD__)bf);
  201.  
  202.   pause();
  203.  
  204.   cout << "Testing overloaded operators (__LWORD__, FLOAT64)..." << endl;
  205.   OperatorTest((__LWORD__)af, b);
  206.   
  207.   pause();
  208.  
  209.   cout << "Testing overloaded operators (FLOAT64, __WORD__)..." << endl;
  210.   OperatorTest(a, (__WORD__)bf);
  211.  
  212.   pause();
  213.  
  214.   cout << "Testing overloaded operators (__WORD__, FLOAT64)..." << endl;
  215.   OperatorTest((__WORD__)af, b);
  216.  
  217.   pause();
  218.  
  219.   cout << "Testing overloaded operators (FLOAT64, __SWORD__)..." << endl;
  220.   OperatorTest(a, (__SWORD__)bf);
  221.  
  222.   pause();
  223.  
  224.   cout << "Testing overloaded operators (__SWORD__, FLOAT64)..." << endl;
  225.   OperatorTest((__SWORD__)af, b);
  226.  
  227.   pause();
  228.   
  229.   cout << "Testing overloaded operators (FLOAT64, __UWORD__)..." << endl;
  230.   OperatorTest(a, (__UWORD__)bf);
  231.  
  232.   pause();
  233.  
  234.   cout << "Testing overloaded operators (__UWORD__, FLOAT64)..." << endl;
  235.   OperatorTest((__UWORD__)af, b);
  236.  
  237.   pause();
  238.  
  239.   cout << "Testing overloaded operators (FLOAT64, __USWORD__)..." << endl;
  240.   OperatorTest(a, (__USWORD__)bf);
  241.  
  242.   pause();
  243.  
  244.   cout << "Testing overloaded operators (__USWORD__, FLOAT64)..." << endl;
  245.   OperatorTest((__USWORD__)af, b);
  246.  
  247.   pause();
  248.   
  249.   cout << "Testing overloaded operators (FLOAT64, __SBYTE__)..." << endl;
  250.   OperatorTest(a, 'B');
  251.  
  252.   pause();
  253.  
  254.   cout << "Testing overloaded operators (__SBYTE__, FLOAT64)..." << endl;
  255.   OperatorTest('A', b);
  256.  
  257.   return 0;
  258. }
  259. // ----------------------------------------------------------- //
  260. // ------------------------------- //
  261. // --------- End of File --------- //
  262. // ------------------------------- //
  263.