1

为什么我的程序一直在计算相同的力?我正确地使用了公式,但是我不确定为什么我一直得到 127 达因的力。任何帮助,将不胜感激

#include <stdio.h>
#include <math.h>
const double gravity_constant = 6.673;
void force_calculate(double a, double b, double c, double d);
void input(double *a, double *b, double *c);
void display(double a, double b, double c, double d);
int main(int argc, char * argv[])
{
    double mass_1 = 0;
    double mass_2 = 0;
    double distance = 0;
    double force = 0;

    input(&mass_1, &mass_2, &distance);
    force = force_calculate(mass_1, mass_2, distance, force);
    display(mass_1, mass_2, distance, force);

    return 0;
}

void force_calculate(double a, double b, double c, double d)
{
    d = (gravity_constant*a*b)/(c*c);
    return;
}
void input(double *a, double *b, double *c)
{
    printf("What is the first mass in grams?\n");
    scanf("%lf", a);
    printf("What is the second mass in grams?\n");
    scanf("%lf", b);
    printf("What is the distance between the two masses in centimeters\n");
    scanf("%lf", c);
}
void display(double a, double b, double c, double d)
{
    printf("%fg is the first mass\n", a);
    printf("%fg is the second mass\n", b);
    printf("%fcm is the distance between the two masses\n", c);
    printf("%f is the force in dynes between both masses\n", d);
    return;
}
4

5 回答 5

1

第一种方法

通过force参考。将定义更改force

void force_calculate(double a, double b, double c, double *d)
{
    *d = (gravity_constant*a*b)/(c*c);
}

并这样称呼它

force_calculate(mass_1, mass_2, distance, &force);

第二种方法:

您可以简单地从force_calculate

double force_calculate(double a, double b, double c, double d)
{
    d = (gravity_constant*a*b)/(c*c);
    return d;
}
于 2013-10-21T22:28:52.410 回答
1

您应该返回值以便可以分配它(确保它也是正确的类型)。此外,您可以在此处删除使用力,因为它是计算的结果。

force = force_calculate(mass_1, mass_2, distance);
...
double force_calculate(double a, double b, double c)
{
 return (gravity_constant*a*b)/(c*c);
}
于 2013-10-21T22:29:09.653 回答
1

问题是您的force_calculate()函数的返回类型是并且您在您的函数定义void中分配它 应该是 forcemain()

void force_calculate(double a, double b, double c, double *d)
{
    *d = (gravity_constant*a*b)/(c*c);
}

不要忘记改变它的函数原型。函数调用应该是

force_calculate(mass_1, mass_2, distance, &force);  
于 2013-10-21T22:29:37.390 回答
1

运行此程序会给我以下运行时错误。

dc:15:11: 错误: void 值没有被忽略,因为它应该被忽略

我想,这可以解决它。

#include <stdio.h>
#include <math.h>
const double gravity_constant = 6.673;
double force_calculate(double a, double b, double c);
void input(double *a, double *b, double *c);
void display(double a, double b, double c, double d);
int main(int argc, char * argv[])
{
    double mass_1 = 0;
    double mass_2 = 0;
    double distance = 0;
    double force = 0;

    input(&mass_1, &mass_2, &distance);
    force = force_calculate(mass_1, mass_2, distance);
    display(mass_1, mass_2, distance, force);

    return 0;
}

double force_calculate(double a, double b, double c)
{
    double force;
    force = (gravity_constant*a*b)/(c*c);
    return force;
}
void input(double *a, double *b, double *c)
{
    printf("What is the first mass in grams?\n");
    scanf("%lf", a);
    printf("What is the second mass in grams?\n");
    scanf("%lf", b);
    printf("What is the distance between the two masses in centimeters\n");
    scanf("%lf", c);
}
void display(double a, double b, double c, double d)
{
    printf("%fg is the first mass\n", a);
    printf("%fg is the second mass\n", b);
    printf("%fcm is the distance between the two masses\n", c);
    printf("%f is the force in dynes between both masses\n", d);
    return;
} 
于 2013-10-22T03:04:06.340 回答
0

您定义force_calculate不返回任何内容(void),但在 main 方法中,您可以这样称呼它:

force = force_calculate(mass_1, mass_2, distance, force);

即,您要返回双倍。

将您的 force_calculate 更改为此声明:

double force_calculate(double a, double b, double c);

和 force_calculate 到这个定义:

double force_calculate(double a, double b, double c)
{
    return (gravity_constant*a*b)/(c*c);
}
于 2013-10-21T22:30:38.497 回答