剑指offer——面试题16:数值的整数次方

  1 // 面试题16:数值的整数次方
  2 // 题目:实现函数double Power(double base, int exponent),求base的exponent
  3 // 次方。不得使用库函数,同时不需要考虑大数问题。
  4 
  5 #include <iostream>
  6 #include <cmath>
  7 
  8 bool g_InvalidInput = false;
  9 bool equal(double num1, double num2);
 10 double PowerWithUnsignedExponent(double base, unsigned int exponent);
 11 
 12 double Power(double base, int exponent)
 13 {
 14     g_InvalidInput = false;
 15 
 16     if (equal(base, 0.0) && exponent < 0)
 17     {
 18         g_InvalidInput = true;
 19         return 0.0;
 20     }
 21 
 22     unsigned int absExponent = (unsigned int) (exponent);
 23     if (exponent < 0)
 24         absExponent = (unsigned int) (-exponent);
 25 
 26     double result = PowerWithUnsignedExponent(base, absExponent);
 27     if (exponent < 0)
 28         result = 1.0 / result;
 29 
 30     return result;
 31 }
 32 
 33 /*
 34 double PowerWithUnsignedExponent(double base, unsigned int exponent)
 35 {
 36     double result = 1.0;
 37     
 38     for (int i = 1; i <= exponent; ++i)
 39         result *= base;
 40     return result;
 41 }
 42 */
 43 
 44 double PowerWithUnsignedExponent(double base, unsigned int exponent)
 45 {
 46     if (exponent == 0)
 47         return 1;
 48     if (exponent == 1)
 49         return base;
 50 
 51     double result = PowerWithUnsignedExponent(base, exponent >> 1);
 52     result *= result;
 53     if ((exponent & 0x1) == 1)
 54         result *= base;
 55 
 56     return result;
 57 }
 58 
 59 bool equal(double num1, double num2)
 60 {
 61     if ((num1 - num2 > -0.0000001) && (num1 - num2 < 0.0000001))
 62         return true;
 63     else
 64         return false;
 65 }
 66 
 67 // ====================测试代码====================
 68 void Test(const char* testName, double base, int exponent, double expectedResult, bool expectedFlag)
 69 {
 70     double result = Power(base, exponent);
 71     if (equal(result, expectedResult) && g_InvalidInput == expectedFlag)
 72         std::cout << testName << " passed" << std::endl;
 73     else
 74         std::cout << testName << " FAILED" << std::endl;
 75 }
 76 
 77 int main(int argc, char* argv[])
 78 {
 79     // 底数、指数都为正数
 80     Test("Test1", 2, 3, 8, false);
 81 
 82     // 底数为负数、指数为正数
 83     Test("Test2", -2, 3, -8, false);
 84 
 85     // 指数为负数
 86     Test("Test3", 2, -3, 0.125, false);
 87 
 88     // 指数为0
 89     Test("Test4", 2, 0, 1, false);
 90 
 91     // 底数、指数都为0
 92     Test("Test5", 0, 0, 1, false);
 93 
 94     // 底数为0、指数为正数
 95     Test("Test6", 0, 4, 0, false);
 96 
 97     // 底数为0、指数为负数
 98     Test("Test7", 0, -4, 0, true);
 99 
100     return 0;
101 }
View Code

 本人答案:

 1 #include"iostream"
 2 #include"stdio.h"
 3 #include"stdexcept"
 4 using namespace std;
 5 
 6 bool errorFlag=false;
 7 
 8 double GetPower(double base,int exponent)
 9 {
10     if(exponent==0)
11         return 1.0;
12     if(exponent==1)
13         return base;
14 
15     double result=GetPower(base,exponent>>1);
16     result*=result;
17     if(exponent&1)
18         result*=base;
19     return result;
20 }
21 
22 bool Equal(double num1,double num2)
23 {
24     if((num1-num2)>-0.000001&&(num1-num2)<0.000001)
25         return true;
26     return false;
27 }
28 
29 double Power(double base,int exponent)
30 {
31     errorFlag=false;
32     double result=0;
33   //  cout<<exponent<<endl;
34     if(Equal(base,0)&&exponent<0)
35     {
36         errorFlag=true;
37         return 0;
38     }
39     if(exponent<0)
40     {
41         result=1.0/GetPower(base,-exponent);
42     }
43     else
44     {
45         result=GetPower(base,exponent);
46     }
47     return result;
48 }
49 
50 void Test(char *testName,double base,int exponent,double expectResult,bool expectFlag)
51 {
52     //注意这里需要后判断flag,不然前面函数没执行,errorFlag值不会得到更新
53     if(Equal(Power(base,exponent),expectResult)&&errorFlag==expectFlag)
54     {
55         cout<<testName<<":passed."<<endl;
56     }
57     else
58     {
59         cout<<testName<<":failed."<<endl;
60     }
61 }
62 
63 int main()
64 {
65     Test("Test1",-2,-2,0.25,false);
66     Test("Test2",-2,3,-8,false);
67     Test("Test3",-2,0,1,false);
68     Test("Test4",0,-2,0,true);
69     Test("Test5",0,0,1,false);
70     Test("Test6",0,2,0,false);
71     Test("Test7",2,3,8,false);
72     Test("Test8",2,-1,0.5,false);
73     Test("Test9",2,0,1,false);
74     return 0;
75 }
View Code

 

posted @ 2019-02-19 20:06  Run_For_Love  阅读(198)  评论(0编辑  收藏  举报