| [eb5dbfd] | 1 | #include <fstream.hfa> | 
|---|
|  | 2 | #include <stdlib.hfa> | 
|---|
|  | 3 |  | 
|---|
|  | 4 | struct MR { double mass, radius; }; | 
|---|
|  | 5 |  | 
|---|
|  | 6 | enum( MR ) Planet {                                                                             // typed enumeration | 
|---|
|  | 7 | //          mass (kg)  radius (km) | 
|---|
|  | 8 | MERCURY = { 0.330_E24, 2.4397_E6 }, | 
|---|
|  | 9 | VENUS   = { 4.869_E24, 6.0518_E6 }, | 
|---|
|  | 10 | EARTH   = { 5.976_E24, 6.3781_E6 }, | 
|---|
|  | 11 | MOON    = { 7.346_E22, 1.7380_E6 },                                     // not a planet | 
|---|
|  | 12 | MARS    = { 0.642_E24, 3.3972_E6 }, | 
|---|
|  | 13 | JUPITER = { 1898._E24, 71.492_E6 }, | 
|---|
|  | 14 | SATURN  = { 568.8_E24, 60.268_E6 }, | 
|---|
|  | 15 | URANUS  = { 86.86_E24, 25.559_E6 }, | 
|---|
|  | 16 | NEPTUNE = { 102.4_E24, 24.746_E6 }, | 
|---|
|  | 17 | PLUTO   = { 1.303_E22, 1.1880_E6 },                                     // not a planet | 
|---|
|  | 18 | }; | 
|---|
|  | 19 |  | 
|---|
|  | 20 | enum( double ) { G = 6.6743_E-11 };                                             // universal gravitational constant (m3 kg-1 s-2) | 
|---|
|  | 21 |  | 
|---|
|  | 22 | static double surfaceGravity( Planet p ) with( p ) { | 
|---|
|  | 23 | //      return G * mass / ( radius \ 2 );                                       // no qualification, exponentiation | 
|---|
|  | 24 | return G * p.mass / ( p.radius \ 2 );                           // qualification, exponentiation | 
|---|
|  | 25 | } | 
|---|
|  | 26 | static double surfaceWeight( Planet p, double otherMass ) { | 
|---|
|  | 27 | return otherMass * surfaceGravity( p ); | 
|---|
|  | 28 | } | 
|---|
|  | 29 |  | 
|---|
| [10b5970] | 30 | int main() { | 
|---|
| [eb5dbfd] | 31 | //      if ( argc != 2 ) exit | "Usage: " | argv[0] | "earth-weight"; // terminate program | 
|---|
|  | 32 | //      double earthWeight = convert( argv[1] ); | 
|---|
|  | 33 | double earthWeight; | 
|---|
|  | 34 | sin | earthWeight;                                                                      // test-suite input comes for indirection | 
|---|
|  | 35 | double earthMass = earthWeight / surfaceGravity( EARTH ); | 
|---|
|  | 36 |  | 
|---|
|  | 37 | //      Planet rp = fromInt( prng( countof( Planet ) ) );       // select random orbiting body | 
|---|
|  | 38 | Planet rp = fromInt( countof( Planet ) - 1 );           // non-random for test suite | 
|---|
|  | 39 | choose( rp ) {                                                                          // implicit breaks | 
|---|
|  | 40 | case MERCURY, VENUS, EARTH, MARS: | 
|---|
|  | 41 | sout | rp | "is a rocky planet"; | 
|---|
|  | 42 | case JUPITER, SATURN, URANUS, NEPTUNE: | 
|---|
|  | 43 | sout | rp | "is a gas-giant planet"; | 
|---|
|  | 44 | default: | 
|---|
|  | 45 | sout | rp | "is not a planet"; | 
|---|
|  | 46 | } | 
|---|
|  | 47 |  | 
|---|
|  | 48 | for ( p; Planet ) {                                                                     // enumerate | 
|---|
|  | 49 | sout | "Your weight on" | ( p == MOON ? "the" : " " ) | p | 
|---|
|  | 50 | | "is" | wd( 1,1,  surfaceWeight( p, earthMass ) ) | "kg"; | 
|---|
|  | 51 | } | 
|---|
|  | 52 | } | 
|---|