数字联系中的flmoon函数

时间:2015-08-07 17:09:48

标签: c

我在C:

中理解以下函数的实现时遇到问题
#include <math.h>
#define RAD (3.14159265/180.0)
#include "fulmoon.h"


void flmoon(int n, int nph, long *jd, float *frac) {
/*This programs begin with an introductory comment summarizing their purpose and explaining
their calling sequence. This routine calculates the phases of the moon. Given an integer n and
a code nph for the phase desired (nph = 0 for new moon, 1 for first quarter, 2 for full, 3 for last
quarter), the routine returns the Julian Day Number jd, and the fractional part of a day frac
to be added to it, of the nth such phase since January, 1900. Greenwich Mean Time is assumed.*/

int i;
float am,as,c,t,t2,xtra;
c=n+nph/4.0;
t=c/1236.85;
t2=t*t;
printf("jdhdhdhd");
as=359.2242+29.105356*c;
am=306.0253+385.816918*c+0.010730*t2;
*jd=2415020+28L*n+7L*nph;
xtra=0.75933+1.53058868*c+((1.178e-4)-(1.55e-7)*t)*t2;
if (nph == 0 || nph == 2){
    xtra += (0.1734-3.93e-4*t)*sin(RAD*as)-0.4068*sin(RAD*am);
    }
else (nph == 1 || nph == 3){
    xtra += (0.1721-4.0e-4*t)*sin(RAD*as)-0.6280*sin(RAD*am);
    }
i=(int)(xtra >= 0.0 ? floor(xtra) : ceil(xtra-1.0));
*jd += i;
*frac=xtra-i;

}

我尝试了什么
我创建了一个名为fulmoon.h的头文件,如下所示:

#ifndef header_h
#define header_h

#define myName "Amrit"

void flmoon(int n, int nph, long *jd, float *frac);


#endif

然后我在主文件中调用了函数flmoon。我所理解的是 * jd * frac 这些论点。当我试图计算它们时,它们怎么可能是输入参数。?
此示例取自名为Numerical recipes第1页的书。

3 个答案:

答案 0 :(得分:1)

参数是指针,因此在调用函数之前,需要创建两个保存结果的局部变量。您对函数的输入是指针,函数将在指向的变量中生成其输出。这是允许函数返回多个值的常用方法。像这样调用函数:

long jdResult;
float fracResult;
flmoon(42, 2, &jdResult, &fracResult); // & creates a pointer to a variable
printf("Results: %l and %f\n", jdResult, fracResult);

变量名可能是jdfrac,但我选择了不同的名称只是为了避免传递给函数的变量名必须与函数相同的常见误解。函数的参数名称。

答案 1 :(得分:1)

参数jdfrac是指针,这意味着您传递了两个局部变量的地址,这些变量在函数完成时会被填充

long jd;
float frac;
flmoon(1,2,&jd,&frac);
printf("jd=%l, frac=%f\n",jd,frac);

答案 2 :(得分:0)

没有参数*jd*frac*是该类型的一部分,将这些参数(jdfrac)指定为指针。查看函数签名:

void flmoon(int n, int nph, long *jd, float *frac);

这意味着你必须传递一些值(nnph)以及那两个指向该函数的指针。指针需要指向一些有效的内存,然后函数将存储它的计算结果:

*jd = // ... dereferencing, i.e. accessing the pointed memory

然后调用者可以访问结果(因为它在内存中传递给指向函数的指针):

float frac;
long jd;
flmoon(n, nph, &jd, &frac);
// & means address of
// jd and frac now contain the computed values

这种传递结果通常用于传递多个值(因为函数只能返回值)。

另外,我很惊讶。这是本书中使用的真实代码吗?我不认为这些代码是“好的”......不应该成为某本书的一部分。