第六章 函数和递归入门
6.12
#include
double calculateCharges( double hours );
int main( ) {
cout << \; cin >> hours1 >> hours2 >> hours3; cout << fixed << setprecision( 2 );
cout << \ << setw( 20 ) << \ << setw( 20 ) << \<< endl; cout << \ << setw( 20 ) << hours1<< setw( 20 ) << calculateCharges( hours1 )< << calculateCharges( hours1 ) + calculateCharges( hours2 ) + double hours1, hours2, hours3; calculateCharges( hours3 ) < double calculateCharges( double hours ) { } if( ( hours >0 ) && ( hours <= 3 ) ) return 2.0; else if( ( hours>3 )&& ( hours <= 19 ) ) return 2.00+ceil( hours - 3 )*0.5; return 10; else 6.14 #include double roundToInteger( double ); double roundToTenths( double ); double roundToHundredths( double ); double roundToThousandths( double ); int main( ) { } double roundToInteger( double number ) { } double roundToTenths( double number ) { } double roundToHundredths( double number ) } double roundToThousandths( double number ) } { return floor( number*1000 + .5 ) / 1000; { return floor( number*100 + .5 ) / 100; return floor( number*10 + .5 ) / 10; return floor( number + .5 ) ; cout << \ << roundToInteger( x ) < cout << \ << roundToHundredths( x ) < 6.16 #include int main( ) { } x= -3 + rand( ) % 15; cout<<\<< x < x= 0 + rand( ) % 9; cout<<\<< x < x= 1 + rand( ) % 1; cout<<\<< x < 6.17 #include int main() { int array1[] = { 2, 4, 6, 8, 10 }; int array2[] = { 3, 5, 7, 9, 11 }; int array3[] = { 6, 10, 14, 18, 22 }; srand( time( 0 ) ); for (int i = 0; i < 3; i++) { cout<< \第ì¨2\ < cout<<\第ì¨2一°?组á¨|数oy据Y中D的ì?随?机¨2数oy:\< } return(0); } 6.18 #include int integerPower( int, int ); int main() { } int integerPower( int base, int exponent ) { } int value1 = 1; for( int counter = 1;counter <= exponent ; counter++ ) value1 *= base; return value1; cout << \ << x << \ << y << \ < cout << \; cin >> x; cout << \; cin >> y; 6.19 #include double hypotenuse( double, double ); int main() { double x,y; cout << \; cin >> x; cout << \; } cin >> y; cout << \ << fixed << setprecision(2)< double hypotenuse( double side1, double side2 ) { } return sqrt( pow( side1 ,2 ) + pow( side2 ,2 ) ); 6.20 #include bool multiple( double, double ); int main( ) { cout< cout << \ cout << \ // if( multiple( x,y )==1 ) // // } bool multiple( double number1, double number2 ) { } if( ceil( number2 / number1 ) == floor( number2 / number1 )) return true; return false; else // else goto loop; cout<<\; cin >> x >> y; loop: int x,y; 6.21 #include