/ 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) |