1

岩を崖から投げ落としたときに、岩がどれだけの距離と時間を移動するかを決定するプログラムを作成します。ここをクリックしてファイル toss.txt をデスクトップにコピーします (ファイル名を右クリックし、[名前を付けて保存] を選択します)。このファイルには、崖の高さがメートル単位で含まれています。

次に、プログラムは次のことを行います。

  1. ファイル toss.txt を開き、崖の高さを倍精度変数に読み取り、適切なラベルを付けて崖の高さの値を画面にエコー出力します。

  2. 岩が投げられる角度 (90 度は真上、0 度は真正面) と岩が投げられる速度 (マイル/時) をユーザーに尋ねます。

  3. 角度が 0 以上 90 以下であることを確認してください。そうでない場合、プログラムは終了し、適切なエラー メッセージが画面に出力されます。

  4. 速度が 100 mph 以下で 0 mph 以上であることを確認します。そうでない場合、プログラムは終了し、適切なエラー メッセージが画面に出力されます。

  5. 角度と速度が有効な場合、プログラムは次のように計算を完了します。

    1. マイル/時をメートル/秒に変換します。

    2. 角度をラジアンに変換します。

    3. 次の式を使用して移動時間を計算します。

      どこ

    4. 以下を使用して、水平方向に移動した距離を計算します。

  6. 適切なラベルを付けて、水平方向に移動した時間と距離を画面に出力します。

  7. 水平方向に移動した距離が崖の高さよりも大きいか、小さいか、または等しいかをユーザーに知らせる適切なメッセージを出力します。

    /* This program */
    
    using namespace std;
    
    #include<iostream>
    #include<cmath>
    #include<iomanip>
    #include<fstream>
    
    int readit ();
    int calcit (double, double, double);
    
    int main()
    {
        readit ();
    
        system ("pause");
    
        return 0;
    }
    
    int readit ()
    {
        double hite, angl, v;
    
        ifstream datain ( "toss.txt" );
    
        datain >> hite;
    
        cout << "The cliff height is " << hite << " meters"<< endl;
    
        cout << "Enter the angle in degrees (from horizontal) the rock is thrown: "
             << endl;
    
        cin >> angl;
    
        if (angl>=0 && angl<=90)
        {
           cout << endl << "The angle you have entered is "<<angl<< endl <<endl;
        } 
           else
        {
           cout << "The angle you have entered is not acceptable" << endl;
    
           return 0;
        }
    
        cout << "Enter the velocity in mph the rock is thrown: " << endl;
    
        cin >> v;
    
        if (v>=0 && v<=100)
        {
           cout << endl << "The velocity at which the rock is thrown is "<<v<<
                 " mph" << endl << endl;
        } 
           else
        {
           cout << "The velocity you have entered is not acceptable" << endl;
    
           return 0;
        }
    
        calcit (hite, angl, v);
    }
    
    int calcit (double hite, double angl, double v)
    {
        double tyme, dist;
    
        v = v * (1609.344/3600);
    
        angl = angl*(M_PI/180);
    
        tyme = -v*sin(angl) + (sqrt((v*sin(angl)*v*sin(angl)) + 2*9.8*hite)/9.8) + (2*(v*sin(angl))/9.8);
    
        dist = (tyme * v) * cos(angl);
    
        cout << tyme << " " << dist <<endl;
    
    
    
    
    }
    

岩が地面に衝突する前に岩が移動する正確な時間を取得しようとしていますが、間違った答えが返ってきます。方程式を回して、岩が空中にある時間をC ++言語に正しく影響するまで計算しているかどうかはわかりません。何かアイデアはありますか?私は本当にこのいまいましいプロジェクトを終わらせる必要があります。

4

4 に答える 4

2

私たちが持っている岩の y (0 より上の高さ) の方程式から始めます。

y = h + v*sin(a)*t - g/2*t^2

に変換する

g/2 T^2 - v*sin(a)*T - h == 0

最終条件 を解くときy(T)=0

これにより、

T = v*sin(a)/g + sqrt(v*sin(a)*v*sin(a) + 2*g*h)/g

方程式の最初の部分がどこ-v*sin(angl)から来たのかわかりません。他のすべては問題なく見えます。したがって、コードではなく、開始した方程式にあるようです。

于 2011-02-27T17:51:26.097 に答える
0

コードを少し「クリーンアップ」するために、いくつかのことを提案します。

  • 関数が int を返す場合は、実際に何かを返すことを確認してください。(main は必須ではありませんが、他の関数は必須です)。

  • v * sin(ang1) を一度計算してから、それを数式で使用します。より効率的であるだけでなく、コードをより明確にします。

  • Piに「定数」を与えたように、9.8(重力?)のように使用している他の数値でそれを行います

于 2011-02-27T17:58:01.180 に答える
0

必要な方程式は次のとおりです。

s =ut + 1/2 at^2

s = Total distance traveled.   (Height of the cliff)
u = Starting velocity          (In your case negative as you are throwing
                                away from the target. And take into account
                                that not all the starting velocity is away
                                from the target (eg angle 0 mean u = 0))
a = acceleration               (9.81 m/s2)
t = time                       (The value you want to calculate).

式を並べ替えて t を解く

s = 0 の場合の t の解を見つけるには...
この式は基本的な二次方程式です。

y = a.x^2 + b.x + c

Where:
    x/y     are variables.
    a/b/c   are constants.

y が 0 の二次方程式の解は次のとおりです。

x = [ -b ± sqrt(b^2 - 4ac) ] / 2a

Notice the ± symbol. There are actually two solutions to the problem.
You should be able to deduce which one is correct for you as the other
is probably negative.

In your particular case the map is:

   x ==> t
   y ==> 0

   a ==> 1/2.g
   b ==> u
   c ==> -s
于 2011-02-27T17:58:46.560 に答える
-1

コードに紛らわしい式がある場合は、意味が明らかになるまで変数名を追加してください。同じ変数に異なる値を割り当て直さない限り、プログラムが混乱することはありません。

int calcit (double hite_meters, double angl_deg, double v_mph)
{
    double const gravity = 9.8;
    double v_ms = v_mph * (1609.344/3600);
    double angl_rad = angl_deg * (M_PI/180);
    double v_vertical = v_ms * sin( angl_rad );
    double time_up = v_vertical / gravity; // [m/s] / [m/s^2] = [s]
    double time_down_over_cliff = time_up;
    // use quadratic formula t = ( -v - ( v^2 - 4gd )^1/2 ) / 2g:
    double time_under_cliff = ( - v_vertical
         - sqrt( ( v_vertical * v_vertical )
               - ( 4 * - gravity * hite_meters ) ) // negative gravity = down
        ) / ( 2 * - gravity ); // ( [m/s] + ([m/s]^2 - [m/s^2]*[m])^1/2 ) / [m/s^2]
                             // = [m/s] / [m/s^2] = [s]
    double time_total = time_up + time_down_over_cliff + time_under_cliff;
    double v_horizontal = v_ms * cos( angl_rad );
    double dist_horizontal = v_ms * time_total;

    cout << time_total << " " << dist_horizontal <<endl;
}

コードのすべての行は、関連する新しい情報を生成します。新しい単位に変換するときは、新しい名前の新しい変数を導入します。複数の単位を含む数式は、コメントで説明されている単位の種類を取得します。これは、そうでなければ私があなたを捕まえるのを助けることができない単位変換エラーを起こすのを助けるはずです.

この種のコードを書くと、より多くのタイピングが必要になりますが、頭を悩ませたり助けを求めたりする時間を節約できます。

プログラム自体はそれほど効率的ではありません。さらに重要なことは、簡単に変更できるため、数回の改訂後に非効率的な混乱に陥らないことです。

于 2011-02-27T18:26:05.360 に答える