home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / ostrldbl.cpp < prev    next >
C/C++ Source or Header  |  1998-06-17  |  2KB  |  57 lines

  1. /***
  2. * ostrldbl.cpp - definitions for ostream class operator<<(long double) functs
  3. *
  4. *       Copyright (c) 1991-1997, Microsoft Corporation.  All rights reserved.
  5. *
  6. *Purpose:
  7. *       Contains the member function definitions for ostream
  8. *       operator<<(long double).
  9. *
  10. *******************************************************************************/
  11.  
  12. #include <cruntime.h>
  13. #include <internal.h>
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <float.h>
  17. #include <iostream.h>
  18. #pragma hdrstop
  19.  
  20. #pragma check_stack(on)         // large buffer(s)
  21.  
  22. ostream& ostream::operator<<(long double f)
  23. {
  24. _WINSTATIC char obuffer[28];
  25. _WINSTATIC char fmt[12];
  26. _WINSTATIC char leader[4];
  27.     char * optr = obuffer;
  28.     int x = 0;
  29.     unsigned int curprecision = __min((unsigned)x_precision,LDBL_DIG);
  30.     if (opfx()) {
  31.         if (x_flags & ios::showpos)
  32.             leader[x++] = '+';
  33.         if (x_flags & ios::showpoint)
  34.             leader[x++] = '#';  // show decimal and trailing zeros
  35.         leader[x] = '\0';
  36.         x = sprintf(fmt,"%%%s.%.0uLg",leader,curprecision) - 1;
  37.         if ((x_flags & ios::floatfield)==ios::fixed)
  38.             fmt[x] = 'f';
  39.         else
  40.             {
  41.             if ((x_flags & ios::floatfield)==ios::scientific)
  42.                 fmt[x] = 'e';
  43.             if (x_flags & uppercase)
  44.                 fmt[x] = (char)toupper(fmt[x]);
  45.             }
  46.  
  47.         sprintf(optr,fmt,f);
  48.         x = 0;
  49.         if (*optr=='+' || *optr=='-')
  50.             leader[x++] = *(optr++);
  51.         leader[x] = '\0';
  52.         writepad(leader,optr);
  53.         osfx();
  54.         }
  55.     return *this;
  56. }
  57.