Changeset 29092213 for doc/theses


Ignore:
Timestamp:
Jun 25, 2024, 12:04:59 PM (6 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
41f4e2d
Parents:
3b69398
Message:

update planet example program

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/jiada_liang_MMath/CFAenum.tex

    r3b69398 r29092213  
    391391The unnamed enumeration provides the gravitational-constant enumerator @G@.
    392392Function @surfaceGravity@ uses the @with@ clause to remove @p@ qualification from fields @mass@ and @radius@.
    393 The program main uses @SizeE@ to obtain the number of enumerators in @Planet@, and safely converts the random value into a @Planet@ enumerator.
    394 The resulting random orbital-body is used in a \CFA @choose@ statement.
    395 The enumerators in the @case@ clause use position for testing.
    396 The prints use @labelE@ to print an enumerator's label.
    397 Finally, a loop iterates through the planets computing the weight on each planet for a given earth mass.
    398 The print statement does an equality comparison with an enumeration variable and enumerator.
     393The program main uses the pseudo function @countof@ to obtain the number of enumerators in @Planet@, and safely converts the random value into a @Planet@ enumerator using @fromInt@.
     394The resulting random orbital-body is used in a @choose@ statement.
     395The enumerators in the @case@ clause use enumerator position for testing.
     396The prints use @label@ to print an enumerator's name.
     397Finally, a loop enumerates through the planets computing the weight on each planet for a given earth mass.
     398The print statement does an equality comparison with an enumeration variable and enumerator (@p == MOON@).
    399399
    400400\begin{figure}
     
    402402\begin{cfa}
    403403struct MR { double mass, radius; };
    404 enum( @MR@ ) Planet {
     404enum( @MR@ ) Planet {                                           $\C{// typed enumeration}$
    405405        //                      mass (kg)   radius (km)
    406406        MERCURY = { 0.330_E24, 2.4397_E6 },
    407407        VENUS      = { 4.869_E24, 6.0518_E6 },
    408408        EARTH       = { 5.976_E24, 6.3781_E6 },
    409         MOON        = { 7.346_E22, 1.7380_E6 }, $\C{// not a planet}$
     409        MOON        = { 7.346_E22, 1.7380_E6 }, $\C{// not a planet}$
    410410        MARS         = { 0.642_E24, 3.3972_E6 },
    411411        JUPITER    = { 1898._E24, 71.492_E6 },
     
    413413        URANUS    = { 86.86_E24, 25.559_E6 },
    414414        NEPTUNE  = { 102.4_E24, 24.746_E6 },
    415 };
    416 enum( double ) { G = 6.6743_E-11 }; $\C{// universal gravitational constant (m3 kg-1 s-2)}$
     415        PLUTO       = { 1.303_E22, 1.1880_E6 }, $\C{// not a planet}$
     416};
     417enum( double ) { G = 6.6743_E-11 };                     $\C{// universal gravitational constant (m3 kg-1 s-2)}$
    417418static double surfaceGravity( Planet p ) @with( p )@ {
    418         return G * mass / ( radius \ 2 ); $\C{// exponentiation}$
     419        return G * mass / ( radius @\@ 2 );             $\C{// no qualification, exponentiation}$
    419420}
    420421static double surfaceWeight( Planet p, double otherMass ) {
     
    422423}
    423424int main( int argc, char * argv[] ) {
    424         if ( argc != 2 ) exit | "Usage: " | argv[0] | "earth-weight";
     425        if ( argc != 2 ) @exit@ | "Usage: " | argv[0] | "earth-weight";  // terminate program
    425426        double earthWeight = convert( argv[1] );
    426427        double earthMass = earthWeight / surfaceGravity( EARTH );
    427 
    428         Planet p = @fromInt@( prng( @SizeE@(Planet) ) ); $\C{// select a random orbiting body}$
    429         @choose( p )@ {
     428        Planet rp = @fromInt@( prng( @countof@( Planet ) ) ); $\C{// select random orbiting body}$
     429        @choose( rp )@ {                                                $\C{// implicit breaks}$
    430430          case MERCURY, VENUS, EARTH, MARS:
    431                 sout | @labelE( p )@ | "is a rocky planet";
    432           @case JUPITER, SATURN, URANUS, NEPTUNE:@
    433                 sout | labelE( p ) | "is a gas-giant planet";
     431                sout | @label( rp )@ | "is a rocky planet";
     432          case JUPITER, SATURN, URANUS, NEPTUNE:
     433                sout | label( rp ) | "is a gas-giant planet";
    434434          default:
    435                 sout | labelE( p ) | "is not a planet";
     435                sout | label( rp ) | "is not a planet";
    436436        }
    437         for ( @p; Planet@ ) {
    438                 sout | "Your weight on" | (@p == MOON@ ? "the" : "") | labelE(p)
    439                            | "is" | wd( 1,1, surfaceWeight( p, earthMass ) ) | "kg";
     437        for ( @p; Planet@ ) {                                   $\C{// enumerate}$
     438                sout | "Your weight on" | ( @p == MOON@ ? "the" : " " ) | label( p )
     439                           | "is" | wd( 1,1,  surfaceWeight( p, earthMass ) ) | "kg";
    440440        }
    441441}
     
    451451Your weight on URANUS is 90.5 kg
    452452Your weight on NEPTUNE is 113.8 kg
     453Your weight on PLUTO is 6.3 kg
    453454\end{cfa}
    454455\caption{Planet Example}
Note: See TracChangeset for help on using the changeset viewer.