2017-02-02 120 views
2
#include <stdio.h> 
#include <math.h> 
#define G 9.81 

typedef struct 
{ 
    double weight; 
    double drag; 
    double time; 
} USER_INPUT; 

double calculateVelocity(USER_INPUT); 

int main(int argc, char **argv) 
{ 
    USER_INPUT userInput; 
    double velocity; 

    printf("Please enter weight, drag and time: "); 
    scanf("%lf %lf %lf", &userInput.weight, &userInput.drag, &userInput.time); 

    velocity = calculateVelocity(userInput); 

    printf("At t = %f , the parachutist with weight %d kg\nand a drag coefficient %8.2f kg/s \n will have a velocity of %8.2f m/s^2", &userInput.time, &userInput.weight, &userInput.drag, velocity); 

    return 0; 
} 

double calculateVelocity(USER_INPUT data) 
{ 
    double velocity; 
    // TODO compute velocity 
    return velocity; 
} 

在主函数中,我想显示结果。 如何打印结构中定义的变量? 我试过%f,它返回0.000000,而%d返回一个随机数。如何在结构中打印变量?

+0

从代码中,我看到变速不是以任何方式计算或分配的。使用%f打印时,使用%f和垃圾值打印时会得到0.000。你能分享你的公式来计算速度吗? –

+1

可能使用'userInput.time,userInput.weight,... etc.'?注意你的'calculateVelocity'返回一个不确定的值,从中读取是未定义的行为。 – greatwolf

回答

1

你正在做的错误,而印刷,

请注意'&'用于获取任何变量的地址,而不是值。所以,当你打印:

printf("At t = %f , the parachutist with weight %d kg\nand a drag coefficient %8.2f kg/s \n will have a velocity of %8.2f m/s^2", &userInput.time, &userInput.weight, &userInput.drag, velocity); 

你实际上是打印变量adderss:

& userInput.time((userInput.time)的地址), & userInput.weight(地址(userInput .weight)),& userInput.drag((userInput.drag)的地址)。

要打印他们的价值观,不解决,因此删除“&”,而打印:

即;

printf("At t = %f , the parachutist with weight %d kg\nand a drag coefficient %8.2f kg/s \n will have a velocity of %8.2f m/s^2", userInput.time, userInput.weight, userInput.drag, velocity); 
0

不知道所有的物理或公式,所以我有点伪造,但我有你的程序编译和运行,所以它应该是很容易的,你可以从这里修复它。随意提问有关指针的问题,以及为什么地址不在不同的地方使用。

#include <stdio.h> 
#include <math.h> 

#define G 9.81 
#define P 1.00 // ? 
#define A 1.00 // ? 

typedef struct { 
    double weight; 
    double drag; 
    double time; 
} userInput_t; 

double calculateVelocity(userInput_t *); // Terminal velocity? 

double calculateVelocity(userInput_t *data) { 
    return sqrt((2 * data->weight * G)/(P * A * data->drag)); 
} 

int main(void) { 

    userInput_t userInput; 

    printf("Please enter weight, drag and time: "); 
    scanf("%lf %lf %lf", &userInput.weight, &userInput.drag, &userInput.time); 


    double velocity = calculateVelocity(&userInput); 

    printf("\nAt t = %f, the parachutist with weight %f kg\n" 
      "and a drag coefficient %f kg/s\n" 
      "will have a velocity of %f m/s^2\n", 
       userInput.time, userInput.weight, userInput.drag, velocity); 
} 
1

int printf(const char *format, ...); int scanf(const char *format, ...);

scanf作品与变量的内存地址,同时用printf的变量itselves。

它UB试​​图 printf ("%d", memory_addres_of_variable);

因为正确的方法来打印内存地址与%zu从C99开始,后来和%lu与ANSI C 90.

这就是为什么你AREE看到一个随机数。

+1

*“,因为打印存储器地址的正确方法是使用%zu从C99及更高版本开始,%lu使用ANSI C90。”*不可以。 '%zu'用于打印'size_t'和'lu'作为'unsigned long'。与内存地址无关。对于'void *'使用'%p',或者对于'uintptr_t'使用'“%”PRIuPTR“u”'。 – user694733

1

该问题与struct无关。您正在通过地址的变量而不是它们的printf。所以解决办法很简单:在printf调用中删除&之前的变量名称。

这是一个常见的错误:scanf需要变量的地址才能够改变它们的值,而printf只是取值。不幸的是,即使原型不让它明显:

int printf(const char *format, ...); 
int scanf(const char *format, ...); 

而且你需要使用%fdouble变量(从名为˚F高高飞翔的类型派生的),%d用于intd ecimal)。

结果:

printf("At t = %f , the parachutist with weight %f kg\nand a drag coefficient %8.2f kg/s \n will have a velocity of %8.2f m/s^2", userInput.time, userInput.weight, userInput.drag, velocity); 

参考文献: