////////////////////////////////////////////////// ///////////////////////////////////
// 1.タイトル整数電力値11
// 1.1陰性とみなされ、0 !!!!ない ダブルパワー(ダブル dBaseの、INT iExponent) { ダブル DRES = 1.0 ; のため(int型 I = 0 ; IはiExponentを<Iは++ ) { DRES * = dBaseの; } リターンDRES; } //は二つの等しい二重か否かを判断する BOOL等しい(ダブル LHS、ダブルRHS) { がiF((LHS - RHS> - 0.0000001)&&(LHS - RHS < 0.0000001 )) { 返す 真; } を返す 偽。 } ダブル PowerWithUnsignedExponent(ダブル dBaseの、符号なし整数uiExponent) { ダブル dResult = 1.0 。 用(符号なし整数 I = 1 ; I <= uiExponent; iは++ ) { dResult * = dBaseの。 } 戻りdResult。 } // 1.2全面但不够高效的算法 ダブル POWER_1(二重のdBase、INT iExponent) { 二重 dResult = 0.0 。 もし(等号(dBaseの、0.0)&& iExponent < 0 ) { リターンdResult。 } 符号なし整数 absExponent =(符号なし整数)iExponent。 もし(iExponent < 0 ) { absExponent =(符号なし整数( - )iExponent)。 } DResult = PowerWithUnsignedExponent(dBaseの、absExponent)。 もし(iExponent < 0 ) { dResult= 1.0 / dResult; } 戻りdResultを; } // 1.3包括的かつ効率的なソリューション // EG入力iExponent = 32、PowerWithUnsignedExponentループ31回必要な場合 // もう一つのアプローチは、(a)32 - >( (A )16)2 - >(( (A)8)2)2 - > ... - >(((((A)2)2)2)2)2 - > 5つのラン ダブル NewPowerWithUnsignedExponent(ダブル dBaseの、符号なし整数uiExponent) { IF(0 == )uiExponent { 戻り 1.0 ; } そう IF(1 == uiExponent) { リターンdBaseの。 } ダブル dResult = NewPowerWithUnsignedExponent(dBaseの、uiExponent >> 1 )。 dResult * = dResult。 // 如果是基数 場合(uiExponent&0×01 ) { dResult * = dBaseの。 } 戻りdResult。 } ダブル Power_2(二重のdBase、INT iExponent) { ダブル dResult = 0.0 。 もし(等号(dBaseの、0.0)&& iExponent < 0 ) { リターンdResult; } 符号なし整数 absExponent =(符号なし整数)iExponent。 もし(iExponent < 0 ) { absExponent =(符号なし整数( - )iExponent)。 } DResult = NewPowerWithUnsignedExponent(dBaseの、absExponent)。 もし(iExponent < 0 ) { dResult = 1.0 / dResult。 } 戻りdResult。 } ボイドPowerTestFunc() { COUT <<" \ N \ N --------------- PowerTestFuncスタート--------------> " << てendl; INT aiArray [] = { - 3、 - 2、 - 1、0、1、2、3、4、5、6 }。 INT ILEN = はsizeof(aiArray)/ はsizeof(INT )。 ダブルのdBase = 5 ; 裁判所未満 << " 方法一:========================== >> " << てendl; 以下のために(int型 i = 0 ; iはILENを<; iは++ ) { のprintf(" [%のLF]的%D次方:%LFを\ n " 、dBaseの、aiArray [i]は、電源(dBaseの、aiArray [I])) ; } coutの << " 方法二:========================== >> " << てendl; 以下のために(int型 i = 0 ; iはILENを<; iは++ ) { のprintf(" [%のLF]的%D次方:%LFを\ n " 、dBaseの、aiArray [i]は、POWER_1(dBaseの、aiArray [I])) ; } coutの << "方法三:========================== >> " << ENDL; のため(int型 i = 0 ; iはILENを<; iは++ ) { printf(" [%のLF]的%D次方:%LFを\ n " 、dBaseの、aiArray [i]は、Power_2(dBaseの、aiArray [I])); } COUT << " \ n \ nは---- ----------- PowerTestFunc終了--------------> " << てendl; }