C++ 重力加速的題目 )) 急

最近老師 丟一個 題目給我們做 要用 WHILE LOOP 來寫 但是我寫不出來 所以 請大大 幫忙~

這是題目...

g = G * (M / (R + h)2)

where G is the gravitational constant (6.672 X 10-11 N m2 /kg2), M is the mass of the earth (5.98 X 1024 kg), R is the mean radius of the Earth (6371 km), and h is the height above the Earth’s surface. If M is measured in kg and R and h in meters, then the resulting acceleration is in units of meters per second squared. Write a program to calculate the acceleration due to the Earth’s gravity in 500 km increments at heights from 0 km to 40,000 km above the surface of the Earth. Print out the results in a table of height versus acceleration with appropriate labels, including the units of the output values.

Notes:

Newton = 1 * (kg * m/s2)

Newton's second law of motion tells us that F=mg. We know that when it comes to gravitational force between two objects that

F= Gm1m2/d2

We can set these two equations equal to each other so that

mg = Gm1mearth/ r2

Where m on the left hand side is equal to m1 on the right hand side, so these terms will cancel.

g on the left hand side is the acceleration of gravity

If we solve for g, we get an equation that gives us the acceleration due to gravity that acts on an object.

g = Gmearth/ r2

When you look at this equation you should notice immediately that the acceleration of gravity will change only with an object's distance from the earth. The objects mass will not effect the acceleration of gravity.

2 Answers

Rating
  • 1 decade ago
    Favorite Answer

    給我你的 email

    C++的源碼傳給你~奇摩不能超過2000字~殘念

    2009-02-18 00:33:03 補充:

    使用無限補充法

    2009-02-18 00:33:46 補充:

    #include

    #include

    namespace Constants

    {

    inline const double G()

    {

    static const double g = 6.672E-11;

    return g;

    }

    2009-02-18 00:34:43 補充:

    還是算了~e-mail給偶吧

    2009-02-20 20:28:41 補充:

    #include

    #include

    namespace Constants

    {

    inline const double G()

    {

    static const double g = 6.672E-11;

    return g;

    }

    2009-02-20 20:29:54 補充:

    inline const double MassRatio()

    {

    static const double ratio = 2.0;

    return ratio;

    }

    2009-02-20 20:30:12 補充:

    inline const double EarthMass()

    {

    static const double earthMass = 5.98E+24;

    return earthMass;

    }

    2009-02-20 20:30:21 補充:

    inline const double EarthRadius()

    {

    static const double Radius = 6371.0E+3;

    return Radius;

    }

    2009-02-20 20:30:48 補充:

    inline const double dh()

    {

    static const double deltaH = 500.0;

    return deltaH;

    }

    2009-02-20 20:32:23 補充:

    typedef const double (*ReturnDoubleFun)();

    template

    2009-02-20 20:34:04 補充:

    /< ReturnDoubleFun FieldConstant,

    ReturnDoubleFun MainEigenValue,

    ReturnDoubleFun Ratio,

    ReturnDoubleFun OriginalPosition />

    2009-02-20 20:34:41 補充:

    class Fieldtor

    {

    private:

    const double eigenValue;

    double acceleration;

    double position;

    2009-02-20 20:35:02 補充:

    static double newAcceleration(const double newPosition)

    {

    return ( FieldConstant()*MainEigenValue() ) / pow(newPosition,Ratio());

    }

    2009-02-20 20:35:21 補充:

    public:

    Fieldtor(double eigenValue_Copy)

    :eigenValue(eigenValue_Copy)

    {

    position = OriginalPosition();

    acceleration = newAcceleration(position);

    }

    2009-02-20 20:35:43 補充:

    void setPosition(const double newPosition)

    {

    position = newPosition;

    acceleration = newAcceleration(position);

    }

    2009-02-20 20:36:13 補充:

    void showState()

    {

    const double fromOriginal = position-OriginalPosition();

    2009-02-20 20:36:29 補充:

    std::cout << "\nThe Position from Original is:" << fromOriginal << "km";

    std::cout << "\tThe Acceleration:" << acceleration << "km/s^2";

    }

    };

    2009-02-20 20:36:55 補充:

    typedef Fieldtor MassFieldtor;

    }

    2009-02-20 20:37:16 補充:

    int main(int argc, char *argv[])

    {

    using namespace Constants;

    MassFieldtor p(1.0);

    2009-02-20 20:37:38 補充:

    double wantTestPosition = EarthRadius();

    const double limit = EarthRadius()+40000.0;

    2009-02-20 20:37:48 補充:

    while( wantTestPosition <= limit )

    {

    p.setPosition(wantTestPosition);

    p.showState();

    wantTestPosition += dh();

    }

    2009-02-20 20:38:04 補充:

    std::cout<< '\n';

    system("PAUSE");

    return EXIT_SUCCESS;

    }

    2009-02-20 20:41:34 補充:

    前面要

    #include /

    #include /

    那個/符號要去掉,因為奇摩的語法不能直接貼出include後的那個符號

    同樣得

     /< ReturnDoubleFun FieldConstant,

    ReturnDoubleFun MainEigenValue,

    ReturnDoubleFun Ratio,

    ReturnDoubleFun OriginalPosition /> 那兩個/也要去掉

    2009-02-20 20:42:56 補充:

    前面要

    #include iostream

    #include math.h 那兩個括號要補上語法才正確~天殺的為什麼貼不出來

    2009-02-20 20:44:39 補充:

    將上述的無限補充碼~組合起來編譯後直接執行~就是你要的結果

    2009-02-20 20:52:03 補充:

    這個是場子(重力場,電場,磁場,核子場~一堆場)的泛型寫法~

    所以eigenvalue代表的是"那個場"的特徵值,如重力場的特徵值是質量,電場是電荷~

    因樣版參數的非型別參數,不能使用double,所以定義了一個可傳回double的函式指標,其數值於編譯時期可被決議,故場子可實現編譯時期多型

    2009-02-20 20:55:21 補充:

    只要使用不同樣版參數,可以重覆用在類似的題目,如電場中的電子加速度與佈場中心的距離關係表

    2009-02-20 20:59:06 補充:

    這個程式碼已簡化,其源碼使用到 template中的template技巧 將如座標系統和採用的數值分析模式全"政策樣式化"

    2009-02-20 21:05:56 補充:

    不好意思那個 typedef Fieldtor ...... MassFieldtor;

    .....中的資訊是空白的,應為 G,EarthMass,MassRatio,EarthRadius

    左右要加上樣版參數的那兩個天殺的括號

    直接寄給你不就好了~

    • Commenter avatarLogin to reply the answers
  • 1 decade ago

    int h=0;

    while loop (h>40000*1000)

    {

    g = G * (M / (R + h)2)

    print h,g

    h+=500*1000

    }

    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.