source: doc/theses/jiada_liang_MMath/test2.cfa @ f678c53b

Last change on this file since f678c53b was 1725989, checked in by Peter A. Buhr <pabuhr@…>, 5 months ago

add enumeration test programs for different programming languages

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