The value of the power of digital

#include<iostream>
using namespace std;

bool g_invalid = false;

bool equal_zero(double base)
{
    g_invalid = false;
    if (base <0.000001 && base >-0.000001)
        return true;
    return false;
}

double exe_pow(double base, int exponent)
{
    double num = 1.0;
    for (int i = 1; i <=exponent; ++i)
        num = num * base;
    return num;

}


double power(double base, int exponent)
{
    if (equal_zero(base) && (exponent < 0))
    {
        g_invalid = true;
        return 0.0;
    }
    if (exponent >0)
        return exe_pow(base, exponent);
    else
        return (1.0 / exe_pow(base, -exponent));
}


int main()
{
    double base;
    int exponent;
    cin >> base >> exponent;
    cout << power(base, exponent) << endl;
    system("pause");
    return 0;

}

First determines whether the power in base or base number is 0, and if not, then the calculated according to the formula, if the number of positive power is normally calculated, if negative, then this power is divided by a number with an absolute value of the answer.

 

发布了27 篇原创文章 · 获赞 8 · 访问量 2123

Guess you like

Origin blog.csdn.net/hgxy123/article/details/104188840