如何在 C 中使用自定义库

How do I make use of a custom library in C

我已经编写了一些关于 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

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

你想做的是:

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

现在,该函数接收指向 V 和 R 的指针,并将 read/write 指向它们的内存地址。

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

您还可以通过使用 return 类型的函数来检索值。

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

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

R = CalcSphRadius(V);

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

此代码中有几处错误 -

    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 函数中获得的任何结果都将丢失,除非您明确 return 它。局部变量只存在于函数的范围内。由于您正在传递一个指向函数的指针并试图在函数外部填充值,因此您应该将函数和函数签名修改为
    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)
    }

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

double Volume = CalcSphVolume(R);

在这种情况下,这要清楚得多,而不必到处传递指针。

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

  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
}

Please try to learn the structure of the C program.

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

  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));
    }