Ignore:
Timestamp:
May 1, 2024, 1:36:04 PM (2 months ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
d414664
Parents:
de3a579 (diff), 69867ad9 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

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

    rde3a579 r35897fb  
    2424\label{s:EnumeratorUnscoping}
    2525
    26 In C, unscoped enumerators presents a \Newterm{naming problem} when multiple enumeration types appear in the same scope with duplicate enumerator names.
     26In C, unscoped enumerators presents a \newterm{naming problem} when multiple enumeration types appear in the same scope with duplicate enumerator names.
    2727There is no mechanism in C to resolve these naming conflicts other than renaming one of the duplicates, which may be impossible.
    2828
     
    265265
    266266\VRef[Figure]{f:PlanetExample} shows an archetypal enumeration example illustrating most of the \CFA enumeration features.
    267 Enumeration @Planet@ is a typed enumeration of type @MR@.
     267@Planet@ is an enumeration of type @MR@.
    268268Each of the planet enumerators is initialized to a specific mass/radius, @MR@, value.
    269 The unnamed enumeration projects the gravitational-constant enumerator @G@.
    270 The program main iterates through the planets computing the weight on each planet for a given earth weight.
     269The unnamed enumeration provides the gravitational-constant enumerator @G@.
     270Function @surfaceGravity@ uses the @with@ clause to remove @p@ qualification from fields @mass@ and @radius@.
     271The program main uses @SizeE@ to obtain the number of enumerators in @Planet@, and safely converts the random value into a @Planet@ enumerator.
     272The resulting random orbital body is used in a @choose@ statement.
     273The enumerators in the @case@ clause use position for testing.
     274The prints use @labelE@ to print the enumerators label.
     275Finally, a loop iterates through the planets computing the weight on each planet for a given earth weight.
     276The print statement does an equality comparison with an enumeration variable and enumerator.
    271277
    272278\begin{figure}
     279\small
    273280\begin{cfa}
    274281struct MR { double mass, radius; };
    275 enum( MR ) Planet {
     282enum( @MR@ ) Planet {
    276283        //                      mass (kg)   radius (km)
    277284        MERCURY = { 0.330_E24, 2.4397_E6 },
     
    285292        NEPTUNE  = { 102.4_E24, 24.746_E6 },
    286293};
    287 enum( double ) { G = 6.6743E-11 }; $\C{// universal gravitational constant (m3 kg-1 s-2)}$
    288 
    289 static double surfaceGravity( Planet p ) with( p ) {
    290         return G * mass / ( radius * radius );
     294enum( double ) { G = 6.6743_E-11 }; $\C{// universal gravitational constant (m3 kg-1 s-2)}$
     295static double surfaceGravity( Planet p ) @with( p )@ {
     296        return G * mass / ( radius \ 2u ); $\C{// exponentiation}$
    291297}
    292298static double surfaceWeight( Planet p, double otherMass ) {
     
    297303        double earthWeight = convert( argv[1] );
    298304        double mass = earthWeight / surfaceGravity( EARTH );
    299         for ( p; Planet ) {
    300                 sout | "Your weight on" | labelE(p) | "is" | wd(1,1, surfaceWeight( p, mass )) | "kg";
     305
     306        Planet p = @fromInt@( prng( @SizeE@(Planet) ) ); $\C{// select a random orbiting body}$
     307        @choose( p )@ {
     308          case MERCURY, VENUS, EARTH, MARS:
     309                sout | @labelE( p )@ | "is a rocky planet";
     310          @case JUPITER, SATURN, URANUS, NEPTUNE:@
     311                sout | labelE( p ) | "is a gas-giant planet";
     312          default:
     313                sout | labelE( p ) | "is not a planet";
    301314        }
    302 }
    303 
     315        for ( @p; Planet@ ) {
     316                sout | "Your weight on" | (@p == MOON@ ? "the" : "") | labelE(p)
     317                           | "is" | wd(1,1, surfaceWeight( p, mass )) | "kg";
     318        }
     319}
    304320$\$$ planet 100
     321JUPITER is a gas-giant planet
    305322Your weight on MERCURY is 37.7 kg
    306323Your weight on VENUS is 90.5 kg
    307324Your weight on EARTH is 100.0 kg
    308 Your weight on MOON is 16.6 kg
     325Your weight on the MOON is 16.6 kg
    309326Your weight on MARS is 37.9 kg
    310327Your weight on JUPITER is 252.8 kg
Note: See TracChangeset for help on using the changeset viewer.