Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2341524
  • 博文数量: 816
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 5010
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-17 17:57
文章分类

全部博文(816)

文章存档

2011年(1)

2008年(815)

分类:

2008-12-17 18:06:26

/ Chapter 12 of C++ How to Program
// bases.cpp

#include

using std::cout;
using std::cin;
using std::endl;
/* Include all necessary using statements from iostream */

#include
/* Include all necessary using statements from iomanip */

#include

/* Write a programmer-defined manipulator width8 that sets
   the width to 8 */

/* Write a programmer-defined manipulator width12 that sets
   the width to 12 */

// function to convert decimal number to binary number
int convertBinary( int x )
{
   int power;
   int result = 0;
   
   for ( power = 0; x >= pow( 2, power ); power++ ) ;

   for ( ; power >= 0; power-- ) {
      result *= 10;

      if ( x / pow( 2, power ) >= 1 ) {
         result++;
         x -= pow( 2, power );

      } // end if

   } // end for

   return result;

} // end function convertBinary

// function printTable definition
void printTable( int a[], const int size )
{
   cout << "\nTable with the numbers in various bases\n";

   /* Write a statement to left justify the following table */
   /* Write a statement to print the header, use the width8
      manipulator for spacing */
   
   for ( int i = 0; i < size; i++ )
      /* Write a statement to output the table of bases   */
      /* Octadecimal output should specify the base       */
      /* Hexadecimal output should include only uppercase */

   cout << "\nTable of formatted and unformatted "
        << "floating points\n";

   /* Write a statement to print header, use the width12
      manipulator for spacing */

   for ( int j = 0; j < size; j++ ) {
      double x = static_cast< double > ( a[ j ] );
      /* Write a statement to output the formatted vs.
         unformatted table */
      /* Formatted output should print in scientific notation,
         showing the decimal point and the sign, and
         have a precision of 2 */
      /* Undo formatting changes */

   } // end for

} // end function printTable

int main()
{
   int x[ 5 ] = { 0 };

   for ( int counter = 0; counter < 5; counter++ ) {
      cout << "enter number " << counter + 1 << ": ";
      cin >> x[ counter ];

      /* Write a statement to test if stream errors occurred */
      /* If there were stream errors, input another number   */

   } // end for

   printTable( x, 5 );

   return 0;

} // end main


--------------------next---------------------

阅读(1047) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~