home *** CD-ROM | disk | FTP | other *** search
/ Prima Shareware 3 / DuCom_Prima-Shareware-3_cd1.bin / PROGRAMO / C / ECKELT / 5 / FORMAT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-23  |  3.5 KB  |  101 lines

  1. // File from page 238 in "Thinking in C++" by Bruce Eckel
  2. //////////////////////////////////////////////////
  3. // From the compressed package ECKELT01.ZIP 2/21/95
  4. // Copyright (c) Bruce Eckel, 1995 
  5. // Source code file from the book "Thinking in C++", 
  6. // Prentice Hall, 1995, ISBN: 0-13-917709-4
  7. // All rights reserved EXCEPT as allowed by the following 
  8. // statements: You may freely use this file for your own 
  9. // work, including modifications and distribution in 
  10. // executable form only. You may copy and distribute this 
  11. // file, as long as it is only distributed in the complete 
  12. // (compressed) package with the other files from this 
  13. // book and you do not remove this copyright and notice. 
  14. // You may not distribute modified versions of the source 
  15. // code in this package. This package may be freely placed 
  16. // on bulletin boards, internet nodes, shareware disks and 
  17. // product vendor disks. You may not use this file in 
  18. // printed media without the express permission of the 
  19. // author. Bruce Eckel makes no 
  20. // representation about the suitability of this software 
  21. // for any purpose. It is provided "as is" without express 
  22. // or implied warranty of any kind. The entire risk as to 
  23. // the quality and performance of the software is with 
  24. // you. Should the software prove defective, you assume 
  25. // the cost of all necessary servicing, repair, or 
  26. // correction. 
  27. // If you think you've found an error, please 
  28. // email all modified files with loudly commented changes 
  29. // to: eckel@aol.com (please use the same 
  30. // address for non-code errors found in the book).
  31. //////////////////////////////////////////////////
  32.  
  33. //: FORMAT.CPP -- Formatting functions
  34. #include <fstream.h>
  35. #define D(a) T << #a << endl; a
  36. ofstream T("format.out");
  37.  
  38. main() {
  39.   D(int i = 47;)
  40.   D(float f = 2300114.414159;)
  41.   char* s = "Is there any more?";
  42.  
  43.   D(T.setf(ios::unitbuf);)
  44.   D(T.setf(ios::stdio);)
  45.  
  46.   D(T.setf(ios::showbase);)
  47.   D(T.setf(ios::uppercase);)
  48.   D(T.setf(ios::showpos);)
  49.   D(T << i << endl;) // Default to dec
  50.   D(T.setf(ios::hex, ios::basefield);)
  51.   D(T << i << endl;)
  52.   D(T.unsetf(ios::uppercase);)
  53.   D(T.setf(ios::oct, ios::basefield);)
  54.   D(T << i << endl;)
  55.   D(T.unsetf(ios::showbase);)
  56.   D(T.setf(ios::dec, ios::basefield);)
  57.   D(T.setf(ios::left, ios::adjustfield);)
  58.   D(T.fill('0');)
  59.   D(T << "fill char: " << T.fill() << endl;)
  60.   D(T.width(10);)
  61.   T << i << endl;
  62.   D(T.setf(ios::right, ios::adjustfield);)
  63.   D(T.width(10);)
  64.   T << i << endl;
  65.   D(T.setf(ios::internal, ios::adjustfield);)
  66.   D(T.width(10);)
  67.   T << i << endl;
  68.   D(T << i << endl;) // Without width(10)
  69.   
  70.   D(T.unsetf(ios::showpos);)
  71.   D(T.setf(ios::showpoint);)
  72.   D(T << "prec = " << T.precision() << endl;)
  73.   D(T.setf(ios::scientific, ios::floatfield);)
  74.   D(T << endl << f << endl;)
  75.   D(T.setf(ios::fixed, ios::floatfield);)
  76.   D(T << f << endl;)
  77.   D(T.setf(0, ios::floatfield);) // Automatic
  78.   D(T << f << endl;)
  79.   D(T.precision(20);)
  80.   D(T << "prec = " << T.precision() << endl;)
  81.   D(T << endl << f << endl;)
  82.   D(T.setf(ios::scientific, ios::floatfield);)
  83.   D(T << endl << f << endl;)
  84.   D(T.setf(ios::fixed, ios::floatfield);)
  85.   D(T << f << endl;)
  86.   D(T.setf(0, ios::floatfield);) // Automatic
  87.   D(T << f << endl;)
  88.  
  89.   D(T.width(10);)
  90.   T << s << endl;
  91.   D(T.width(40);)
  92.   T << s << endl;
  93.   D(T.setf(ios::left, ios::adjustfield);)
  94.   D(T.width(40);)
  95.   T << s << endl;
  96.  
  97.   D(T.unsetf(ios::showpoint);)
  98.   D(T.unsetf(ios::unitbuf);)
  99.   D(T.unsetf(ios::stdio);)
  100. }
  101.