Changeset 35897fb for doc/theses/jiada_liang_MMath/CFAenum.tex
- Timestamp:
- May 1, 2024, 1:36:04 PM (2 months ago)
- 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. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/CFAenum.tex
rde3a579 r35897fb 24 24 \label{s:EnumeratorUnscoping} 25 25 26 In C, unscoped enumerators presents a \ Newterm{naming problem} when multiple enumeration types appear in the same scope with duplicate enumerator names.26 In C, unscoped enumerators presents a \newterm{naming problem} when multiple enumeration types appear in the same scope with duplicate enumerator names. 27 27 There is no mechanism in C to resolve these naming conflicts other than renaming one of the duplicates, which may be impossible. 28 28 … … 265 265 266 266 \VRef[Figure]{f:PlanetExample} shows an archetypal enumeration example illustrating most of the \CFA enumeration features. 267 Enumeration @Planet@ is a typedenumeration of type @MR@.267 @Planet@ is an enumeration of type @MR@. 268 268 Each 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. 269 The unnamed enumeration provides the gravitational-constant enumerator @G@. 270 Function @surfaceGravity@ uses the @with@ clause to remove @p@ qualification from fields @mass@ and @radius@. 271 The program main uses @SizeE@ to obtain the number of enumerators in @Planet@, and safely converts the random value into a @Planet@ enumerator. 272 The resulting random orbital body is used in a @choose@ statement. 273 The enumerators in the @case@ clause use position for testing. 274 The prints use @labelE@ to print the enumerators label. 275 Finally, a loop iterates through the planets computing the weight on each planet for a given earth weight. 276 The print statement does an equality comparison with an enumeration variable and enumerator. 271 277 272 278 \begin{figure} 279 \small 273 280 \begin{cfa} 274 281 struct MR { double mass, radius; }; 275 enum( MR) Planet {282 enum( @MR@ ) Planet { 276 283 // mass (kg) radius (km) 277 284 MERCURY = { 0.330_E24, 2.4397_E6 }, … … 285 292 NEPTUNE = { 102.4_E24, 24.746_E6 }, 286 293 }; 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 ); 294 enum( double ) { G = 6.6743_E-11 }; $\C{// universal gravitational constant (m3 kg-1 s-2)}$ 295 static double surfaceGravity( Planet p ) @with( p )@ { 296 return G * mass / ( radius \ 2u ); $\C{// exponentiation}$ 291 297 } 292 298 static double surfaceWeight( Planet p, double otherMass ) { … … 297 303 double earthWeight = convert( argv[1] ); 298 304 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"; 301 314 } 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 } 304 320 $\$$ planet 100 321 JUPITER is a gas-giant planet 305 322 Your weight on MERCURY is 37.7 kg 306 323 Your weight on VENUS is 90.5 kg 307 324 Your weight on EARTH is 100.0 kg 308 Your weight on MOON is 16.6 kg325 Your weight on the MOON is 16.6 kg 309 326 Your weight on MARS is 37.9 kg 310 327 Your weight on JUPITER is 252.8 kg
Note: See TracChangeset
for help on using the changeset viewer.