source: doc/theses/jiada_liang_MMath/test2.cfa @ 2e6b2a0

Last change on this file since 2e6b2a0 was 7f8c232, checked in by Peter A. Buhr <pabuhr@…>, 4 months ago

formatting

  • Property mode set to 100644
File size: 1.6 KB
RevLine 
[1725989]1#include <fstream.hfa>
2#include <stdlib.hfa>
[f3b67b6]3#include <enum.hfa>
[1725989]4
5struct MR { double mass, radius; };
6
[f3b67b6]7enum( MR ) Planet {                                                                             // typed enumeration
[1725989]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 },
[f3b67b6]12        MOON    = { 7.346_E22, 1.7380_E6 },                                     // not a planet
[1725989]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 },
[f3b67b6]18        PLUTO   = { 1.303_E22, 1.1880_E6 },                                     // not a planet
[1725989]19};
20
[f3b67b6]21enum( double ) { G = 6.6743_E-11 };                                             // universal gravitational constant (m3 kg-1 s-2)
[1725989]22
23static double surfaceGravity( Planet p ) with( p ) {
[f3b67b6]24        return G * mass / ( radius \ 2 );                                       // no qualification, exponentiation
[1725989]25}
26static double surfaceWeight( Planet p, double otherMass ) {
27        return otherMass * surfaceGravity( p );
28}
29
30int main( int argc, char * argv[] ) {
[f3b67b6]31        if ( argc != 2 ) exit | "Usage: " | argv[0] | "earth-weight"; // terminate program
[1725989]32
33        double earthWeight = convert( argv[1] );
34        double earthMass = earthWeight / surfaceGravity( EARTH );
35
[f3b67b6]36        Planet rp = fromInt( prng( countof( Planet ) ) );       // select random orbiting body
37        choose( rp ) {                                                                          // implicit breaks
[1725989]38          case MERCURY, VENUS, EARTH, MARS:
[62a38e7]39                sout | rp | "is a rocky planet";
[1725989]40          case JUPITER, SATURN, URANUS, NEPTUNE:
[62a38e7]41                sout | rp | "is a gas-giant planet";
[1725989]42          default:
[62a38e7]43                sout | rp | "is not a planet";
[1725989]44        }
45
[f3b67b6]46        for ( p; Planet ) {                                                                     // enumerate
[62a38e7]47                sout | "Your weight on" | ( p == MOON ? "the" : " " ) | p
[f3b67b6]48                         | "is" | wd( 1,1,  surfaceWeight( p, earthMass ) ) | "kg";
[1725989]49        }
50}
Note: See TracBrowser for help on using the repository browser.