繁体   English   中英

创建一个返回 pow b^e 的函数

[英]Create a function that return the pow b^e

我必须创建一个返回 b^e 值的函数。 这就是我所做的:

#include<stdio.h>
#include<math.h>

int funzione_potenza (int b, int e);

int main ()
{
    int b, e, potenza;

    printf("Inserisci la base: ");
    scanf("%d",&b);

    printf("Inserisci l'esponente: ");
    scanf("%d",&e);

    printf("La potenza e' %d",potenza);

    return 0;
}

int funzione_potenza (int b, int e)
{
    int potenza;

    potenza = pow(b,e);

    return potenza;
}

当我运行它时,它显示功率的错误值(例如 5343123) 有什么问题?

您不需要函数来调用内置的 pow 函数。 如果您正在尝试编写自己的函数,请尝试使用循环或递归构建解决方案。

假设您可以自由使用数学库,除非数字适合“int”,否则该方法将返回正确答案。 否则,考虑对更大的数字使用“long long int”。

如果您不被允许使用数学库,这里是一个正确的实现:

long long int power(int b, int e){
          if(e==0){
                 return 1;
           }
           else if(e%2==1){
                 long long int temp = power(b,e/2);
                 return temp*temp*b;
           }
           else{
                 long long int temp = power(b,e/2);
                 return temp*temp;
           }
 }

有两个问题:

首先,你得到了一个垃圾值,因为你从不调用funzione_potenza函数。

int main()
{
  int b, e, potenza;

  printf("Inserisci la base: ");
  scanf("%d", &b);

  printf("Inserisci l'esponente: ");
  scanf("%d", &e);

  potenza = funzione_potenza(b, e);       // <<<<<<<<<< insert this line

  printf("La potenza e' %d", potenza);

  return 0;
}

其次,您甚至不需要funzione_potenza这只是pow的包装器,您可以直接调用pow

...
scanf("%d", &e);

potenza = pow(b, e);

printf("La potenza e' %d", potenza);
...

您可以在不使用“pow”的情况下实现幂函数“pow”-function内置函数以及下面的代码。

#include <stdio.h>

//function prototype
int funzione_potenza (int b, int e);

int main ()
{
  int b, e, potenza;

  printf("Inserisci la base: ");
  scanf("%d",&b);

  printf("Inserisci l'esponente: ");
  scanf("%d",&e);

  potenza = funzione_potenza (b,e);

  printf("La potenza e' %d",potenza);

  return 0;
 }

 int funzione_potenza (int b, int e)
 {
    //local variables
    int i, potenza = b; //initialize potenza with b

    for (i = 0; i < e; i++ )
    {
       potenza = potenza * e; //multiply n-number of times to get the power
    } 

    return potenza;
 }

我的目的是展示你可以意识到这一点的方式,但这基本上是在重新发明轮子,所以不推荐。 在这里,我展示了如何尝试编写幂函数的粗略想法。

实现这一点的另一种方法是

int mpow(int b, int e) {
    if (e <= -1) {
        fprintf(stderr, "mpow:(b: integer, e:non-negative integer )");
        exit(1);
    }
    if (e == 0) return 1;
    if (b == 0) return 0;
    int ans = 1;
    for (int i = 1; i <= e; i++) {
        if (!checkIfOverflown(ans, b)) ans = ans * b;
        else {
            fprintf(stderr, "%s\n", "overflow in multiplication");
            exit(1);
        }

    }
    return ans;

}

pow返回double您不应该将其转换为整数(为什么要失去精度)。

为什么将pow包装在函数中? 您可以直接使用它。

我的功能正确吗?

这个函数是正确的,只要整数不会因为溢出东西而弄乱它。 对于b小值, e它有效。

还有一种更简单、更有效的方法是

int mpow(int b, int e) {
    int res = 1, flag = 0;
    if (e <= -1) {
        fprintf(stderr, "mpow:(b: integer, e:non-negative integer )");
        exit(1);
    }
    while (e > 0) {
        if (e % 2 == 1) {
            if (!checkIfOverflown(res, b))
                res = (res * b);
            else {
                flag = 1;
                break;
            }
        }
        if (!checkIfOverflown(b, b))
            b = (b * b);
        else {
            flag = 1;
            break;
        }
        e /= 2;
    }
    if( flag ){
        fprintf(stderr, "%s\n", "overflow in multiplication");
        exit(1);
    }
    return res;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM