【问题标题】:How do I make use of a custom library in C如何在 C 中使用自定义库
【发布时间】:2021-11-06 15:49:23
【问题描述】:

我已经编写了一些关于 uni 赋值的代码,但我一直遇到这样的问题,即我的自定义函数没有提供任何其他输出然后为零。

基本上,我是在询问如何检索要在我的代码中使用的函数的结果。

在这里我将粘贴我的代码。

    #include <stdio.h>

    //math.h is included via the header file because the compiler liked it better.

    int V;

    int H;

    int R;

    int result;

    #include "A1_header.h"

    int main()
    {

    //small introduction
    printf("Welcome to the volume test game\n");
    printf("We will start with spheres. \n");
    printf("Please input your sphere radius here:");
    scanf("%d", &R);
    CalcSphVolume(&V, &R);
    printf("please input your result:");
    scanf("%d", &result);
    if(result == V){
        printf("Congratulations, your answer is correct!\n");
    }
    else{
        printf("Wrong answer, the correct result was %d \n", V);
    }
    return 0;
}

下面是我用来定义函数的 .h 文件。

    #ifndef Point

    #define Point

    #include <math.h>

    //these are the functions for Sphere calculations

    void CalcSphVolume(int V, int R) {
    V = 1.33*3.14(R*R*R);
    }

    void CalcSphRadius(int V, int R) {
    R = cbrt(V/4.1762);
    return;
    }

    //these are the functions for the Cone calculations

    void CalcConVolume(int V, int R, int H) {
    V = 0.33*(3.14*(R*R))H;
    return;
    }

    void CalcConHeight(int V, int R, int H) {
    H = V/(0.33*(3.14*(R*R)));
    } 

    void CalcConRadius(int V, int R, int H) {
    R = sqrt(V/(0.33*3.14H));
    }

    //these are the functions for the Cilinder calculations

    void CalcCilVolume(int V, int R, int H) {
    V = 3.14*H*(R*R);
    }

    void CalcCilHeight(int V, int R, int H) {
    H = V/(3.14(R*R));
    }

    void CalcCilRadius(int V, int R, int H) {
    R = sqrt(V/(3.14*H));
    }

    #endif

【问题讨论】:

  • 您的标题中缺少#endif,这也可能是当您在标题中包含math.h 时“编译器更喜欢它”的原因。

标签: c shared-libraries


【解决方案1】:

我不知道你是怎么运行代码的,因为一般情况下这段代码是编译不出来的。

这段代码有几处错误 -

    void CalcSphVolume(int V, int R) {
        V = 1.333.14(RRR);
    }

首先,我认为您的格式可能有错误,因为RRR 不是正确的计算方式,您想要的是(R*R*R),同样,1.333.14 不是可接受的数据类型。从上下文来看,因为它是一个球体的体积,所以应该是

    void CalcSphVolume(int V, int R) {
       V = 1.333 * 3.14 * (R*R*R);
    }

现在已经解决了,这个函数存在一些问题(与代码中的其他函数类似。)

  1. 您在这里混合类型 - 1.333 是双精度,但 V 是整数,因此您的实际答案将被隐式转换为整数,并且您将失去精度。所以V 应该是 double 类型。所以你会得到更准确的答案。
    void CalcSphVolume(double V, int R) {
       V = 1.333 * 3.14 * (R*R*R);
    }
  1. 这里要注意的另一件事是,您的函数参数是按值计算的,不接受指针。这意味着您在此 void 函数中获得的任何结果都将丢失,除非您明确返回它。局部变量只存在于函数的范围内。由于您正在传递指向函数的指针并尝试在函数外部填充值,因此您应该将函数和函数签名修改为
    void CalcSphVolume(double* V, int* R) {
        int r = *R // for clarity
       *V = 1.333 * 3.14 * (r*r*r);
    }

我们必须取消引用才能获得指针所持有的“实际”值。您不能以这种方式将算术逻辑应用于原始指针。

实现同样目的的另一种方法是

    double CalcSphVolume(int R) {
       double V = 1.333 * 3.14 * (R*R*R);
       return V; // or simply return 1.333 * 3.14 * (R*R*R)
    }

在这里,您是按值传递,但将值计算机返回给调用者。所以你可以像这样使用这个函数 -

double Volume = CalcSphVolume(R);

在这种情况下更清楚,而不是必须到处传递指针。

对于您的用例,不需要使用指针 - 当您必须改变或传递大型对象时考虑使用指针(想象一个非常大的数组,因此您不想在每次使用它时都创建一个副本一个函数),不能在堆栈上声明。

【讨论】:

  • 请原谅我奇怪的格式,在粘贴我的代码时我没有注意到它留下了 * 符号。无论如何,您的回答很有帮助,我无法让它与您的第一个示例一起使用,但是在输入您的第二个示例并将 5 更改为 R 后,它可以完美运行
  • 我进行了编辑以格式化您的代码,一旦获得批准,它应该修复格式:)。您可以使用反引号来格式化代码。欢迎来到堆栈溢出!
  • 谢谢,为了清楚起见,我还添加了 * 符号 :)
【解决方案2】:

问题是您的函数参数接受的是整数而不是指向内存地址的指针。

你想要做的是:

 void CalcSphRadius(int *V, int *R) 
 {
     *R = cbrt((*V)/4.1762);
     return;
 }

现在,该函数正在接收指向 V 和 R 的指针,并将读取/写入它们的内存地址。

如上所示,不要忘记使用星号“*”取消对指针的引用,这样您就可以写入存储在这些地址中的值,而不仅仅是进行指针运算。

您还可以使用函数的返回类型来检索值。

 int CalcSphRadius(int V) 
 {
     return cbrt(V/4.1762);
 }

然后使用它们像这样分配变量:

R = CalcSphRadius(V);

【讨论】:

    【解决方案3】:
    1. 您的 .h 文件名应为 A1_header.h 另外,代码可以是这样的:
    #ifndef Point
    
    #define Point
    
    #include <math.h>
    #include <stdio.h>
    
    //these are the functions for Sphere calculations
    
    void CalcSphVolume(int V, int R);
    
    void CalcSphRadius(int V, int R);
    
    //these are the functions for the Cone calculations
    
    void CalcConVolume(int V, int R, int H);
    
    void CalcConHeight(int V, int R, int H);
    
    void CalcConRadius(int V, int R, int H);
    
    //these are the functions for the Cilinder calculations
    
    void CalcCilVolume(int V, int R, int H);
    
    void CalcCilHeight(int V, int R, int H);
    
    void CalcCilRadius(int V, int R, int H);
    
    #endif
    
    1. #include "A1_header.h" 在错误的位置。 应该是这样的
    //directives
    #include <stdio.h>
    #include "A1_header.h"
    
    int main(void)
    {
        variable declaration;
        
        statements
    }
    

    请尝试学习C程序的结构。

    你需要在主函数中声明变量。

    1. 当您调用函数 CalcSphVolume(V, R);为什么是变量的地址?应该是:
    printf("Please input your sphere radius here:");
    scanf("%d", &R);
    CalcSphVolume(V, R);
    
    1. 合理安排所有功能 例如:CalcCilHeight 函数应使用 * 正确格式化

      void CalcCilHeight(int V, int R, int H)
      {
          H = V/(3.14 * (R * R));
      }
      

    【讨论】:

    • 我很抱歉我的结构不好。这是我学习 C 的第一个阶段,我们还没有真正深入,所以我不知道应该在函数内部声明变量。至于格式,粘贴我的代码以某种方式摆脱了 * 符号,使其阅读起来非常混乱,我现在已经对其进行了编辑,希望现在更清晰。我的最后一个问题是你的替代代码如何让它来计算体积,因为你没有在那里声明使用的公式吗?那我把公式放在哪里呢?
    • 您可以将公式放在单独的.c 文件中,例如: CalcCilVolume.c 在第一行添加#include "A1_header.h"。在我看来,最好的方法是将每个函数编码为一个单独的 .c 文件,然后是一个 main.c 文件。 c 和 A1_header.h,最后是 Makefile。 Google 如何创建 Makefile。
    猜你喜欢
    • 1970-01-01
    • 2022-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-11
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多