c - 质量之间的引力

标签 c function pointers double

为什么我的程序一直在计算相同的力?我正确地使用了公式,但是我不确定为什么我一直得到 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;
}

最佳答案

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

force = force_calculate(mass_1, mass_2, distance);
...
double force_calculate(double a, double b, double c)
{
 return (gravity_constant*a*b)/(c*c);
}

关于c - 质量之间的引力,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19505894/

相关文章:

C - 为什么我的合并功能不起作用?

比较C中的两个数组?

c - 用罗马数字表示年份

PHP mail()发送2份

c - Malloc 用于 void 指针数组的单个元素

c - 为什么 "++x ||++y &&++z"先计算 "++x",即使运算符 "&&"的优先级高于 "||"

function - Scala 是否有类似于 Haskell 的 `$` 的运算符?

function - 正确使用参数

c - strtok 和指向数组的指针

c - 解析选项的开关盒