source: doc/theses/jiada_liang_MMath/planet.cfa @ 2980ccb8

Last change on this file since 2980ccb8 was 1697c40, checked in by JiadaL <j82liang@…>, 5 months ago

merge local changes

  • Property mode set to 100644
File size: 1.8 KB
RevLine 
[525f7ad]1#include <fstream.hfa>
2#include <stdlib.hfa>
3#include <enum.hfa>
4
5struct MR { double mass, radius; };
6
7enum( MR ) Planet {
8        //          mass (kg)  radius (km)
9        MERCURY = { 0.330_E24, 2.4397_E6 },
10        VENUS   = { 4.869_E24, 6.0518_E6 },
11        EARTH   = { 5.976_E24, 6.3781_E6 },
12        MOON    = { 7.346_E22, 1.7380_E6 }, // not a planet
13        MARS    = { 0.642_E24, 3.3972_E6 },
14        JUPITER = { 1898._E24, 71.492_E6 },
15        SATURN  = { 568.8_E24, 60.268_E6 },
16        URANUS  = { 86.86_E24, 25.559_E6 },
17        NEPTUNE = { 102.4_E24, 24.746_E6 },
18};
19
20enum( double ) { G = 6.6743_E-11 }; // universal gravitational constant (m3 kg-1 s-2)
21
22static double surfaceGravity( Planet p ) with( p ) {
23        return G * mass / ( radius \ 2 ); // exponentiation
24}
25static double surfaceWeight( Planet p, double otherMass ) {
26        return otherMass * surfaceGravity( p );
27}
28
29int main( int argc, char * argv[] ) {
30        if ( argc != 2 ) exit | "Usage: " | argv[0] | "earth-weight";
31
32        double earthWeight = convert( argv[1] );
33        double earthMass = earthWeight / surfaceGravity( EARTH );
34
[1697c40]35        Planet p = fromInt( prng( countof( Planet ) ) ); // select a random orbiting body
[525f7ad]36//      Planet p = fromInt( prng( 9 ) ); // select a random orbiting body
37        choose( p ) {
38          case MERCURY, VENUS, EARTH, MARS:
[1697c40]39                sout | label( p ) | "is a rocky planet";
[525f7ad]40          case JUPITER, SATURN, URANUS, NEPTUNE:
[1697c40]41                sout | label( p ) | "is a gas-giant planet";
[525f7ad]42          default:
[1697c40]43                sout | label( p ) | "is not a planet";
[525f7ad]44        }
45
46
[1697c40]47        for ( p; Planet ) {
[525f7ad]48                sout | "Your weight on "
[1697c40]49                 | (p == MOON ? "the" : "") | label(p)
[525f7ad]50                         | "is" | wd( 1,1, surfaceWeight( p, earthMass ) ) | "kg";
51        }
52}
Note: See TracBrowser for help on using the repository browser.