【问题标题】:Have reviewed several answers and still don't get powers in C [duplicate]已经审查了几个答案,但仍然没有在 C 中获得权力 [重复]
【发布时间】:2019-10-28 02:24:04
【问题描述】:

我已经查看了多个 C 中的权力问题和答案,或4^2。我已经用不同的方式写出来了,但仍然不能正确。截至目前,我收到消息:

/tmp/cc9yBiPo.o: In function `main':
Functions2.c:(.text+0x2b8): undefined reference to `pow'
collect2: error: ld returned 1 exit status.

到目前为止,这是我的代码,就像我说的我尝试了不同的人的答案,但我比什么都更困惑。

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


int main()

{

int choice;
int a, b, c;
float d;


    do{
    printf("\t Menu \n");
    printf("1. Addition \n");
    printf("2. Subtraction \n");
    printf("3. Multiplication \n");
    printf("4. Division \n");
    printf("5. Modulo \n");
    printf("6. Eponents \n");
    printf("7. Exit \n");

    printf("Please choose a menu selection: \n");
    scanf("%d", &choice);

    switch(choice)
    {
    case 1:
    {
        printf("Enter two numbers to add: \n");
        scanf("%d%d", &a, &b);

        c = a + b;

        printf("The sum of the two numbers = %d \n", c);

    }break;

    case 2:
    {
        printf("Enter two numbers to subtract: \n");
        scanf("%d&d", &a, &b);

        c = a - b;

        printf("The difference between the two number = %d \n", c);

    }break;

    case 3:
    {
        printf("Enter two numbers to multiply: \n");
        scanf("%d%d", &a, &b);

        c = a * b;

        printf("The product of the two numbers = %d \n", c);
    }break;

    case 4:
    {
        printf("Enter two numbers to divide: \n");
        scanf("%d%d", &a, &b);

        d = (float) a/b;

        printf("The quotient of the two numbers = %f \n", d);

    }break;

    case 5:
    {
        printf("Enter two numbers to get a modulo: \n");
        scanf("%d%d", &a, &b);

        c = a % b;

        printf("The modulus of the two numbers = %d \n", c);
    }break;

    case 6:
    {
        printf("Enter a base: \n");
        scanf("%d", &a);

        printf("Enter an exponent: \n");
        scanf("%d", &b);

        c = pow(a, b);

        printf("The exponent of the two numbers = %d", c);
    }break;

    case 7:
    {
        printf("Thank you, you will now exit.");
    }break;

    default:
        printf("error \n");
    }
}   while (choice != 7);

return 0;
}

【问题讨论】:

标签: c


【解决方案1】:

你必须链接到数学库(libm)。

-lm 标志添加到您的复杂命令以与其链接。

检查这个question

【讨论】:

    【解决方案2】:

    您必须更改基数和指数的数据类型才能获得正确的结果。

    C语言中pow函数的语法是:

    双 pow(双 x, 双 y);

    同样,在 4 的情况下,将数据类型从 int 更改为 float

    #include <stdio.h>
    #include <math.h>
    
    
    int main()
    
    {
    
       double g,h,i;
       int choice;
       int a, b, c;
       float d,e,f;
    
    
       do{
            printf("\t Menu \n");
            printf("1. Addition \n");
            printf("2. Subtraction \n");
            printf("3. Multiplication \n");
            printf("4. Division \n");
            printf("5. Modulo \n");
            printf("6. Eponents \n");
            printf("7. Exit \n");
    
            printf("Please choose a menu selection: \n");
            scanf("%d", &choice);
    
            switch(choice)
             {
              case 1:
                 {
                    printf("Enter two numbers to add: \n");
                    scanf("%d%d", &a, &b);
    
                    c = a + b;
    
                    printf("The sum of the two numbers = %d \n", c);
    
                 }break;
    
             case 2:
                 {
                    printf("Enter two numbers to subtract: \n");
                    scanf("%d&d", &a, &b);
    
                    c = a - b;
    
                    printf("The difference between the two number = %d \n", c);
    
                 }break;
    
             case 3:
                 {
                   printf("Enter two numbers to multiply: \n");
                   scanf("%d%d", &a, &b);
    
                   c = a * b;
    
                   printf("The product of the two numbers = %d \n", c);
                 }break;
    
             case 4:
                 {
                   printf("Enter two numbers to divide: \n");
                   scanf("%f%f", &e, &f);
    
                   d = e/f;
    
                   printf("The quotient of the two numbers = %f \n", d);
    
                 }break;
    
             case 5:
                  {
                   printf("Enter two numbers to get a modulo: \n");
                   scanf("%d%d", &a, &b);
    
                   c = a % b;
    
                   printf("The modulus of the two numbers = %d \n", c);
                  }break;
    
              case 6:
                  {
                   printf("Enter a base: \n");
                   scanf("%lf", &h);
    
                   printf("Enter an exponent: \n");
                   scanf("%lf", &i);
    
                   g = pow(h, i);
    
                   printf("The exponent of the two numbers = %lf", g);
                  }break;
    
             case 7:
                  {
                   printf("Thank you, you will now exit.");
                  }break;
    
            default:
                   printf("error \n");
                  }
                  }   while (choice != 7);
    
    
    
            return 0;
                  }
    

    希望这会对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-03
      • 2020-11-28
      • 1970-01-01
      • 2022-06-20
      • 2014-01-10
      • 1970-01-01
      • 2017-03-21
      • 2023-03-06
      相关资源
      最近更新 更多