Sword Point of Offer question 11th: the integer number of values
1 //============================================================================2 //Name:jz-c-11.cpp3 //Author:laughing_lz4 //Version:5 //Copyright:all Right Reserved6 //Description: integer number of values7 //============================================================================8 9#include <iostream>Ten using namespacestd; One A BOOLEqualDoubleNUM1,Doublenum2); - DoublePower (Double Base,intexponent) { - Doubleresult =1; the if(Exponent >0) { - if(Equal (Base,0.0)) { -result =0; -}Else { + while(Exponent >0) { -result = result *Base; +exponent--; A } at } -}Else if(Exponent = =0) { - if(Equal (Base,0.0)) {//here can not directly judge exponent = = 0, the reason: The computer represents a decimal error, judge whether two decimals is equal, can only judge the absolute value of their difference is not in a very small range, such as less than 0.0000001★★ -cout <<"no meaning"<<Endl; -result =0;//0 of the 0-time party doesn't make sense ★ -}Else { inresult =1; - } to}Else { + if(Equal (Base,0.0)) { -result =0; the}Else { *exponent =-exponent;//take absolute value $ while(Exponent >0) {Panax Notoginsengresult = result *Base; -exponent--; the } +result =1/result; A } the + } - returnresult; $ } $ BOOLEqualDoubleNUM1,DoubleNUM2) {//determine whether two decimals are equal ★ - if((Num1-num2 >-0.0000001) && (Num1-num2 <0.0000001)) { - return true; the}Else { - return false;Wuyi } the } - /* Wu * This function is not used here, the advantage of this function is that the efficiency of bit operation is higher than that of multiplication method and redundancy operation. - */ About /*Double powerwithunsignedexponent (double base, unsigned int exponent) { $ if (exponent = = 0) { - return 1; - } - if (exponent = = 1) { A return base; + } the double result = powerwithunsignedexponent (base, exponent >> 1);//Shift right instead of divided by 2★ - result *= result; $ if ((exponent & 0x1) = = 1) {//use and operation to determine the exponent is odd or even ★ the result *= base; the } the return result; the }*/ - in intMain () { the Doubleresult = Power (9,5); thecout << Result <<Endl; About return 0; the}
Jz-c-11