Sei sulla pagina 1di 4

CHAPTER 3.

COMPARISON OF PROGRAMMING LANGUAGES24

public d e l e g a t e int MakeRun ( ) ;

// / <summary>
// / The a c t o f consuming a l l t h o s e b e v e r a g e s .
// / </summary>
public c l a s s Binge
{
// / <summary>
// / What we ’ l l be d r i n k i n g .
// / </summary>
private s t r i n g b e v e r a g e ;

// / <summary>
// / The s t a r t i n g co un t .
// / </summary>
private int count = 0 ;

// / <summary>
// / The manner i n which t h e l y r i c s a r e
output .
// / </summary>
private Writer S i n g ;

// / <summary>
// / What t o do when i t ’ s a l l gone .
// / </summary>
private MakeRun RiskDUI ;

public e v e n t MakeRun O utO fBo ttl es ;

// / <summary>
// / I n i t i a l i z e s the binge .
// / </summary>
// / <param name="c ou n t">How many we ’ r e
consuming .
// / </param>
// / <param name="d i s a s t e r W a i t i n g T o H a p p e n">
// / Our i n s t r u c t i o n s , s h o u l d we s u c c e e d .
CHAPTER 3. COMPARISON OF PROGRAMMING LANGUAGES25

// / </param>
// / <param name=" w r i t e r ">How our d r i n k i n g
song w i l l be heard .</param>
// / <param name="b e v e r a g e ">What t o d r i n k
d u r i n g t h i s b i n g e .</param>
public Binge ( s t r i n g bevera ge , int count ,
Writer w r i t e r )
{
this . beverage = beverage ;
t h i s . count = count ;
this . Sing = w r i t e r ;
}

// / <summary>
// / Let ’ s g e t s t a r t e d .
// / </summary>
public void S t a r t ( )
{
while ( count > 0 )
{
Sing (
@"
{0} b o t t l e {1} o f {2} on t h e w a l l ,
{0} b o t t l e {1} o f { 2 } .
Take one down , p a s s i t around , " ,
count , ( count == 1 )
? "" : " s " , beverage ) ;

count −−;

i f ( count > 0 )
{
S i n g ( " {0} b o t t l e {1} o f {2}
on t h e w a l l . " ,
count , ( count == 1 )
? "" : " s " , beverage ) ;
}
else
CHAPTER 3. COMPARISON OF PROGRAMMING LANGUAGES26

{
S i n g ( "No more b o t t l e s o f
{0} on t h e w a l l . " ,
beverage , n u l l ) ;
}

Sing (
@"
No more b o t t l e s o f {0} on t h e w a l l ,
No more b o t t l e s o f { 0 } . " , beve rage , n u l l ) ;

i f ( t h i s . O utO fBo ttl es != n u l l )


{
count = t h i s . O utO fBo ttl es ( ) ;
S i n g ( " {0} b o t t l e s o f {1} on
t h e w a l l . " , count , b e v e r a g e ) ;
}
else
{
S i n g ( " F i r s t we weep , then we s l e e p
. ") ;
S i n g ( "No more b o t t l e s o f {0} on
the wall . " ,
beve rage , n u l l ) ;
}
}
}

// / <summary>
// / The song remains t h e same .
// / </summary>
c l a s s SingTheSong
{
// / <summary>
// / Any o t h e r number would be s t r a n g e .
// / </summary>
CHAPTER 3. COMPARISON OF PROGRAMMING LANGUAGES27

const int b o t t l e C o u n t = 9 9 ;

// / <summary>
// / The e n t r y p o i n t . S e t s t h e p a r a m e t e r s
o f t h e Binge and s t a r t s i t .
// / </summary>
// / <param name="a r g s ">unused </param>
s t a t i c void Main ( s t r i n g [ ] a r g s )
{
Binge b i n g e =
new Binge ( " b e e r " , b o t t l e C o u n t ,
new Writer ( C o n s o l e . WriteLine ) ) ;
b i n g e . Ou tOf Bot tle s += new MakeRun (
SevenEleven ) ;
binge . Start () ;
}

// / <summary>
// / There ’ s bound t o be one n e a r b y .
// / </summary>
// / <r e t u r n s >Whatever would f i t i n t h e
t r u n k .</ r e t u r n s >
s t a t i c int SevenEleven ( )
{
C o n s o l e . WriteLine ( "Go t o t h e s t o r e ,
g e t some more . . . " ) ;
return b o t t l e C o u n t ;
}
}
}

3.5 Python
#! / u s r / b i n / env python
# −∗− c o d i n g : i s o −8859−1 −∗−
"""
99 B o t t l e s o f Beer ( by Gerold Penz )
Python can be s i m p l e , t o o : −)

Potrebbero piacerti anche