Changeset 29092213 for doc/theses/jiada_liang_MMath
- Timestamp:
- Jun 25, 2024, 12:04:59 PM (5 months ago)
- Branches:
- master
- Children:
- 41f4e2d
- Parents:
- 3b69398
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/CFAenum.tex
r3b69398 r29092213 391 391 The unnamed enumeration provides the gravitational-constant enumerator @G@. 392 392 Function @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 @label E@ 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 .393 The 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@. 394 The resulting random orbital-body is used in a @choose@ statement. 395 The enumerators in the @case@ clause use enumerator position for testing. 396 The prints use @label@ to print an enumerator's name. 397 Finally, a loop enumerates 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 (@p == MOON@). 399 399 400 400 \begin{figure} … … 402 402 \begin{cfa} 403 403 struct MR { double mass, radius; }; 404 enum( @MR@ ) Planet { 404 enum( @MR@ ) Planet { $\C{// typed enumeration}$ 405 405 // mass (kg) radius (km) 406 406 MERCURY = { 0.330_E24, 2.4397_E6 }, 407 407 VENUS = { 4.869_E24, 6.0518_E6 }, 408 408 EARTH = { 5.976_E24, 6.3781_E6 }, 409 MOON = { 7.346_E22, 1.7380_E6 }, 409 MOON = { 7.346_E22, 1.7380_E6 }, $\C{// not a planet}$ 410 410 MARS = { 0.642_E24, 3.3972_E6 }, 411 411 JUPITER = { 1898._E24, 71.492_E6 }, … … 413 413 URANUS = { 86.86_E24, 25.559_E6 }, 414 414 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 }; 417 enum( double ) { G = 6.6743_E-11 }; $\C{// universal gravitational constant (m3 kg-1 s-2)}$ 417 418 static double surfaceGravity( Planet p ) @with( p )@ { 418 return G * mass / ( radius \ 2 ); $\C{//exponentiation}$419 return G * mass / ( radius @\@ 2 ); $\C{// no qualification, exponentiation}$ 419 420 } 420 421 static double surfaceWeight( Planet p, double otherMass ) { … … 422 423 } 423 424 int 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 425 426 double earthWeight = convert( argv[1] ); 426 427 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}$ 430 430 case MERCURY, VENUS, EARTH, MARS: 431 sout | @label E(p )@ | "is a rocky planet";432 @case JUPITER, SATURN, URANUS, NEPTUNE:@433 sout | label E(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"; 434 434 default: 435 sout | label E(p ) | "is not a planet";435 sout | label( rp ) | "is not a planet"; 436 436 } 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"; 440 440 } 441 441 } … … 451 451 Your weight on URANUS is 90.5 kg 452 452 Your weight on NEPTUNE is 113.8 kg 453 Your weight on PLUTO is 6.3 kg 453 454 \end{cfa} 454 455 \caption{Planet Example}
Note: See TracChangeset
for help on using the changeset viewer.