2017-02-02 8 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; 
} 

main関数では、結果を表示したいと思います。 構造体で定義された変数を印刷するにはどうすればよいですか? 私は%fを返しました。これは0.000000を返し、%dは乱数を返します。構造内の変数を印刷する方法は?

+0

コードから、可変速度がどのような方法でも計算または割り当てられていないことがわかります。 %fを使用して印刷すると0.000、%dを使用して印刷するとガベージ値が得られます。数式を共有して速度を計算できますか? –

+1

'userInput.time、userInput.weight、...など'を使用しますか? '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(ユーザ入力の(アドレス.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作品、変数itselvesとしばらくのprintfです。

それはUBは、メモリアドレスを印刷する正しい方法は、C99から始まる%zuであるため、 printf ("%d", memory_addres_of_variable);

にしようと後で、なぜあなたはアリー乱数を見ていANSI C 90

%luています。

+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, ...); 

そして、あなたは(Fフロートと呼ばれるタイプから派生)double変数に対して%fを使用する必要があり、%dintD 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); 

参考文献:

関連する問題