27. COM and DHTML

Chapter 18 - Complete I/O in C++

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

Formatting Output
The next example continues the development of C++ formatted output initially introduced in Chapter 12. The first program demonstrates how to print a table of factorials using long doubles with the default right justification:
//
//  fact1.cpp
//  A C++ program that prints a table of
//  factorials for the numbers from 1 to 25.
//  Program uses the long double type.
//  Formatting includes precision, width and fixed
//  with default of right justification when printing.
//  Copyright (c) Chris H. Pappas and William H. Murray, 1998
//

#include <iostream.h>
#include <iomanip.h>
main( )
{
 long double number,factorial;

 number=1.0;
 factorial=1.0;

 cout.precision(0);           // no decimal place
 cout.setf(ios::fixed);       // use fixed format

 for(int i=0;i<25;i++) {
   factorial*=number;
   number=number+1.0;
   cout.width(30);            // width of 30 characters
   cout << factorial << endl;
 }

 return (0);
}
The precision( ), width( ), and setf( ) class members functions were repeated in the loop. The output from the program takes on the following form:
                        1
                        2
                        6
                       24
                      120
                      720
                     5040
                    40320
                   362880
                  3628800
                 39916800
                479001600
               6227020800
              87178291200
            1307674368000
           20922789888000
          355687428096000
         6402373705728000
       121645100408832000
      2432902008176640000
     51090942171709440000
   1124000727777607680000
  25852016738884976640000
 620448401733239439360000
15511210043330985984000000
The next program/output pair demonstrates how to vary output column width and override the default right justification:
//
//  fact2.cpp
//  A C++ program that prints a table of
//  factorials for the numbers from 1 to 15.
//  Program uses the long double type.
//  Formatting includes precision, width, alignment,
//  and format of large numbers.
//  Copyright (c) Chris H. Pappas and William H. Murray, 1998
//

#include <iostream.h>
#include <iomanip.h>

main( )
{
 long double number,factorial;

 number=1.0;
 factorial=1.0;

 cout.precision(0);           // no decimal point
 cout.setf(ios::left);        // left justify numbers
 cout.setf(ios::fixed);       // use fixed format
 for(int i=0;i<25;i++) {
   factorial*=number;
   number=number+1.0;
   cout.width(30);            // width of 30 characters
   cout << factorial << endl;
 }

 return (0);
}
The left-justified output takes on the following form:
1                             
2                             
6                             
24                            
120                           
720                           
5040                          
40320                         
362880                        
3628800                       
39916800                      
479001600                     
6227020800                    
87178291200                   
1307674368000                 
20922789888000                
355687428096000               
6402373705728000              
121645100408832000            
2432902008176640000           
51090942171709440000          
1124000727777607680000        
25852016738884976640000       
620448401733239439360000      
15511210043330985984000000
The next example prints out a table of numbers, their squares, and their square roots. The program demonstrates how easy it is to align columns, pad with blanks, fill spaces with zeros, and control numeric precision in C++.
//
//  sqrt.cpp
//  A C++ program that prints a table of
//  numbers, squares, and square roots for the
//  numbers from 1 to 15. Program uses the type
//  double. Formatting aligns columns, pads blank
//  spaces with ‘0’ character, and controls
//  precision of answer.
//  Copyright (c) Chris H. Pappas and William H. Murray, 1998
//

#include <iostream.h>
#include <iomanip.h>
#include <math.h>

main( )
{
 double number,square,sqroot;
 cout << “num\t” << “square\t\t” << “square root\n”;
 cout << “____________________________________\n”;

 number=1.0;
 cout.setf(ios::fixed);        // use fixed format

 for(int i=1;i<16;i++) {
   square=number
*number;       // find square
   sqroot=sqrt(number);        // find square root

   cout.fill(‘0’);             // fill blanks with zeros
   cout.width(2);              // column 2 characters wide
   cout.precision(0);          // no decimal place
   cout << number << “\t”;

   cout.width(6);              // column 6 characters wide
   cout.precision(1);          // print 1 decimal place
   cout << square << “\t\t”;
   cout.width(8);              // column 8 characters wide
   cout.precision(6);          // print 6 decimal places
   cout << sqroot << endl;

   number+=1.0;
 }
 return (0);
}
The formatted output takes on the following form:
num     square           square root
____________________________________
01      0001.0            1.000000
02      0004.0            1.414214
03      0009.0            1.732051
04      0016.0            2.000000
05      0025.0            2.236068
06      0036.0            2.449490
07      0049.0            2.645751
08      0064.0            2.828427
09      0081.0            3.000000
10      0100.0            3.162278
11      0121.0            3.316625
12      0144.0            3.464102
13      0169.0            3.605551
14      0196.0            3.741657
15      0225.0            3.872983

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net