demo/old/pow_recr.c

50 lines
693 B
C

#include <stdio.h>
float pow( float base, int exp )
{
/**
* 该函数为 base != 0, exp >=1 编写
*/
if ( exp == 1 ) {
return base;
} else {
return base * pow( base, exp - 1 );
}
}
int main()
{
float a;
int b;
printf( "input a, b: " );
scanf( "%f %d", &a, &b );
/**
* 默认输入没有错误,且 b >= 0
*/
if ( base == 0 ) {
if ( exp == 0 ) {
printf( "err to calc 0^0\n" );
} else {
printf( "0" );
}
} else {
if ( exp == 0 ) {
printf( "1" );
} else if ( exp > 0 ) {
printf( "%f", exp( a, b ) );
} else {
printf( "%f", 1 / exp( a, 0 - b ) );
}
}
printf( "\n" );
return 0;
}