chapter 5 programmer-defined function 程序员定义的函数 instroduction in this chapter, we...

128
CHAPTER 5 Programmer-defined functio n 程程程程程程程程 Instroduction In this chapter, we consider the basics of progra mmer-defined functions with value parameters. Our examination includes a discussion of invocation, parameters, and the local and global scopes. 本本本本 本本本本本本本本本本本本本本本本本 本本本本本本本本 本本本 本本本本本本本本本本本本 。、、。

Upload: brianna-carpenter

Post on 04-Jan-2016

324 views

Category:

Documents


4 download

TRANSCRIPT

CHAPTER 5 Programmer-defined function 程序员定义的函数

Instroduction

In this chapter, we consider the basics of programmer-defined functions

with value parameters. Our examination includes a discussion of invocati

on, parameters, and the local and global scopes.

本章讨论 使用值参的程序员定义函数的基本概念。还包括函数调用、参数、局部作用域和全局作用域。

5.1 BASICS 基本概念

functionfunction

parameters

Return value

Input stream data

OutputStream data

Model for flow of information to and from a function using value parameters

For example

float CircleArea ( float r )

{ const float Pi = 3.1415f ;

return Pi * r * r ;

}

5.1.1 Function definition syntax 函数定义语法

For example

float CircleArea ( float r )

{ const float Pi = 3.1415f ;

return Pi * r * r ;

}

5.1.1 Function definition syntax 函数定义语法

Function headerInterface

For example

float CircleArea ( float r )

{{ const float Pi = 3.1415f ;

return Pi * r * r ;

}}

5.1.1 Function definition syntax 函数定义语法

Function body

For example

float CircleArea ( float r )

{ const float Pi = 3.1415f ;

return Pi * r * r ;

}

5.1.1 Function definition syntax 函数定义语法

TypeType FunctionName ()

{ // statements

returnreturn expressionexpression ;

}

voidvoid FunctionName ()

{ // statements

returnreturn ;

}

The correspondence between the actual parameters in an invocation and

the formal parameters of a programmer-defined function is determined

by the relative positions of the parameters.

5.1.2 Invocation and flow of control 调用和控制流

#include<iostream.h>

int add(int , int ) ;

void main()

{ int add(int, int ) ;

int a, b, c ;

cin >> a>> b;

c = add(a,b) ;

cout << “c = ” << c << endl ;

}

int add(int i, int j )

{ i + + ; j + + ;

return ( i + j ); }

a b c

int a, b, c ;

Invocation function with value parameters

Invocation function with value parameters

#include<iostream.h>

int add(int , int ) ;

main()

{ int add(int, int ) ;

int a, b, c ;

cin >> a>> b;

c = add(a,b) ;

cout << “c = ” << c << endl ;

}

int add(int i, int j )

{ i + + ; j + + ;

return ( i + j ); }

a b c2 4

cin >> a>> b;

Invocation function with value parameters

#include<iostream.h>

int add(int , int ) ;

main()

{ int add(int, int ) ;

int a, b, c ;

cin >> a>> b;

c = add(a,b) ;

cout << “c = ” << c << endl ;

}

int add(int i, int j )

{ i + + ; j + + ;

return ( i + j ); }

a b c2 4

c = add(a,b) ;

Invocation function with value parameters

#include<iostream.h>

int add(int , int ) ;

main()

{ int add(int, int ) ;

int a, b, c ;

cin >> a>> b;

c = add(a,b) ;

cout << “c = ” << c << endl ;

}

int add(int i, int j )

{ i + + ; j + + ;

return ( i + j ); }

a b c

i j add

2 4

2 4

int add(int i, int j )

Invocation function with value parameters

2 4i j add

#include<iostream.h>

int add(int , int ) ;

main()

{ int add(int, int ) ;

int a, b, c ;

cin >> a>> b;

c = add(a,b) ;

cout << “c = ” << c << endl ;

}

int add(int i, int j )

{ i + + ; j + + ;

return ( i + j ); }

a b c2 4

2 433 55

{ i + + ; j + + ;

Invocation function with value parameters

#include<iostream.h>

int add(int , int ) ;

main()

{ int add(int, int ) ;

int a, b, c ;

cin >> a>> b;

c = add(a,b) ;

cout << “c = ” << c << endl ;

}

int add(int i, int j )

{ i + + ; j + + ;

return ( i + j ); }

a b c

i j add

2 4

2 433 55 8

3 + 5

return ( i + j ); }

Invocation function with value parameters

#include<iostream.h>

int add(int , int ) ;

main()

{ int add(int, int ) ;

int a, b, c ;

cin >> a>> b;

c = add(a,b) ;

cout << “c = ” << c << endl ;

}

int add(int i, int j )

{ i + + ; j + + ;

return ( i + j ); }

a b c

i j add

2 4 8

2 433 55 8

c = add(a,b) ;

Invocation function with value parameters

#include<iostream.h>

int add(int , int ) ;

main()

{ int add(int, int ) ;

int a, b, c ;

cin >> a>> b;

c = add(a,b) ;

cout << “c = ” << c << endl ;

}

int add(int i, int j )

{ i + + ; j + + ;

return ( i + j ); }

a b c2 4 8

c = add(a,b) ;

Invocation function with value parameters

#include<iostream.h>

int add(int , int ) ;

main()

{ int add(int, int ) ;

int a, b, c ;

cin >> a>> b;

c = add(a,b) ;

cout << “c = ” << c << endl ;

}

int add(int i, int j )

{ i + + ; j + + ;

return ( i + j ); }

a b c2 4 8

Output :c = 8

cout << “c = ” << c << endl ;

Computing the size of a donut.

The size of a cylinder is r2h.

The size of a donut can be calculated

by taking the difference in the sizes of

two cylinders.

5.2 A TASTY PROBLEM 一个诱人的问题

• The size of a cylinder

5.2 A TASTY PROBLEM 一个诱人的问题

// CylinderVolume(): compute the size of a cylinder with

// radius r and height h

float CylinderVolume(float r, float h)

{ const float Pi = 3.1415f;

return Pi * r * r * h;

}

• The size of a cylinder

5.2 A TASTY PROBLEM 一个诱人的问题

// DonutSize(): compute the size of a donut with outer

// edge radius Outer from the donut center, inner edge

// radius Inner from the donut center, and thickness

// Width

float DonutSize(float Outer, float Inner, float Width)

{ float OuterSize = CylinderVolume(Outer, Width);

float HoleSize = CylinderVolume(Inner, Width);

return OuterSize - HoleSize;

}

float CylinderVolume(float r, float h) ;

• compute the size of a donut

• The size of a cylinder

5.2 A TASTY PROBLEM 一个诱人的问题

// DonutSize(): compute the size of a donut with outer

// edge radius Outer from the donut center, inner edge

// radius Inner from the donut center, and thickness

// Width

float DonutSize(float Outer, float Inner, float Width)

{ float OuterSize = CylinderVolume(Outer, Width);

float HoleSize = CylinderVolume(Inner, Width);

return OuterSize - HoleSize;

}

float CylinderVolume(float r, float h) ;

• compute the size of a donut

return CylinderVolume(Outer, Width) - CylinderVolume(Inner, Width) ;return CylinderVolume(Outer, Width) - CylinderVolume(Inner, Width) ;

• The size of a cylinder

5.2 A TASTY PROBLEM 一个诱人的问题

float DonutSize(float Outer, float Inner, float Width) ;

float CylinderVolume(float r, float h) ;

• compute the size of a donut

• packaging, Compute size of a user-specified donut

#include <iostream>

#include <string>

using namespace std;

// prototyping

float DonutSize(float Outer, float Inner, float Width);

float CylinderVolume(float Radius, float Width);

// main(): manage computation and display of user- specified donut size

int main() { … } // Program 6.2

Define a function to compute the factorial of a number:

5.3 SOME USEFUL FUNCTIONS

1nif1...)1n(n

0nif1!n

// Factorial() : determine n! for parameter nint function ( int n ){ int nfactorial = 1 ; while ( n > 1 ) { nfactorial *= n ; --n; } return nfactorial ;}

How checkthe value of n

whether is sensible ?

How checkthe value of n

whether is sensible ?

// Program 6.4 : Has a scope prblem#include <iostream.h>

void Mystery ( int a, int b ) ; //prototype

int main()

{ int i = 10 , j = 20; // local object definition

Mystery ( i , j ) ; // call Mystery with local object i and j

cout << a << endl << b << endl ; // no define a and b in main()

return 0 ;

}

5.5 THE LOCAL SCOPE 局部作用域

void Mystery ( int aa , int bb )

{ cout << aa << endl << bb << endl ;

aa = 1; bb = 1 ;

cout << aa << endl << bb << endl ;

return ;

}

// Program 6.4 : Has a scope prblem#include <iostream.h>

void Mystery ( int a, int b ) ;

int main()

{ int i = 10 , j = 20;

Mystery ( i , j ) ;

cout << a << endl << b << endl ;

return 0 ;

}

5.5 THE LOCAL SCOPE 局部作用域

void Mystery ( int aa , int bb )

{ cout << aa << endl << bb << endl ;

aa = 1; bb = 1 ;

cout << aa << endl << bb << endl ;

return ;

}

10 20i j

aa bb

// Program 6.4 : Has a scope prblem#include <iostream.h>

void Mystery ( int a, int b ) ;

int main()

{ int i = 10 , j = 20;

Mystery ( i , j ) ;

cout << a << endl << b << endl ;

return 0 ;

}

5.5 THE LOCAL SCOPE 局部作用域

void Mystery ( int aa , int bb )

{ cout << aa << endl << bb << endl ;

aa = 1; bb = 1 ;

cout << aa << endl << bb << endl ;

return ;

}

10 20i j

aa bb10 20

// Program 6.4 : Has a scope prblem#include <iostream.h>

void Mystery ( int a, int b ) ;

int main()

{ int i = 10 , j = 20;

Mystery ( i , j ) ;

cout << a << endl << b << endl ;

return 0 ;

}

5.5 THE LOCAL SCOPE 局部作用域

void Mystery ( int aa , int bb )

{ cout << aa << endl << bb << endl ;

aa = 1; bb = 1 ;

cout << aa << endl << bb << endl ;

return ;

}

10 20i j

aa bb10 20

// Program 6.4 : Has a scope prblem#include <iostream.h>

void Mystery ( int a, int b ) ;

int main()

{ int i = 10 , j = 20;

Mystery ( i , j ) ;

cout << a << endl << b << endl ;

return 0 ;

}

5.5 THE LOCAL SCOPE 局部作用域

void Mystery ( int aa , int bb )

{ cout << aa << endl << bb << endl ;

aa = 1; bb = 1 ;

cout << aa << endl << bb << endl ;

return ;

}

10 20i j

a ? b ?a ? b ?a ? b ?a ? b ?

A local object can be used only

• in the block and in the nested block in which it has been defined.

• in a statement or nested block that occurs after its definition.

5.5.1 Local scope rules 局部作用域规则

Block

Is a list of statements nested within curly braces .

{

statements

}

• In different blocks, names are independent one another.

5.5.2 Name reuse with object 对象名的重用

// Example# include <iostream.h>main ( ){void f ( ) ; int a , b = 1 ; // local object within main() cout << “a = ” << a << “, b = ” << b << endl ; f ( ) ;}void f ( ){ int a = 2 ; b = 3 ; // local object within f() cout << “a = ” << a << “, b = ” << b << endl ; return ;}

• In different blocks, names are independent one another.

5.5.2 Name reuse with object 对象名的重用

• In nested blocks, as soon as the nested block that reused the name is

complete, the declaration of the encompassing block is back in effect.

#include<iostream.h>

void main()

{ int a = 10, b = 20 ;

cout << a << '\t' << b << endl ;

{ char a = 'A' ; int b=30 ;{ char a = 'A' ; int b=30 ;

a++ ; b++ ;a++ ; b++ ;

cout<<a<<'\t'<<b<<endl;cout<<a<<'\t'<<b<<endl;

}}

a++; b++;

cout<<a<<'\t'<<b<<endl;

}

For example

10 20

B 31

11 21

Output

• In different blocks, names are independent one another.

5.5.2 Name reuse with object 对象名的重用

• In nested blocks, as soon as the nested block that reused the name is

complete, the declaration of the encompassing block is back in effect.

#include<iostream.h>

void main()

{ int a = 10, b = 20 ;

cout << a << '\t' << b << endl ;

char a = 'A' ; int b=30 ;char a = 'A' ; int b=30 ; // illegal

a++; b++;

cout<<a<<'\t'<<b<<endl;

}

For example

• A name cannot be redefined in the same block.

5.6 THE GLOBAL SCOPE

• An object defined in the global scope is called a global object.

• A global object may be referenced by simply using its name in any

desired block.

• Scope operator :: indicates that the definition being used is the

global one.

• Global object are always initialized.

#include<iostream.h>

int a ; char c ;int a ; char c ; //global scope, initializedvoid main()void main(){ cout << "global_a0: " << { cout << "global_a0: " << a a << '\t' << "global_c0: " << << '\t' << "global_c0: " << c c << endl ;<< endl ; //using global objects

{ int a a ; char c c ; //withins statement block cout <<"local_a0: " << a a << '\t' << "local_c0: " << cc << endl ; aa = 10 ; cc = 'K' ; // using local objects cout << "local_a1: " << aa << '\t' << "local_c1: " << cc << endl ; ::a = 123 ; ::c = 'p' ;::a = 123 ; ::c = 'p' ; //using global objects cout << "global_a1: " << ::a::a << '\t' << "global_c1: " << ::c::c << endl ; } cout << "global_a2: " << cout << "global_a2: " << aa << '\t' << "global_c2: " << << '\t' << "global_c2: " << cc << endl ; << endl ;}}

For example1

Global_a0: 0 Global_c0:

Local_a0: -858993460 Local_c0: ?

Local_a1: 10 Local_c1: K

Global_a1: 123 Global_c1: p

Global_a2: 123 Global_c2: p

Output

For example2#include<iostream.h>int a, b = 1 ;int a, b = 1 ; // can be using within global void main(){ void f1(); void f2(); cout << "a1 = " << a << ", b1 = "<< b << endl ; f1() ; f2() ;}int i , j = 1 ;int i , j = 1 ; // can be using within f1 and f2void f1(){ cout << "a2 = " << ++a << ", b2 = "<< ++b << endl ; cout << "i1 = " << i << ", j1 = " << j << endl; return ;}void f2(){ cout << "a3 = " << ++a << ", b3 = " << ++b << endl ; cout << "i2 = " << ++i << ", j2 = " << ++j << endl ; return ;}

a1 = 0, b1 = 1

a2 = 1, b2 = 2

i1 = 0, j1 = 1

a3 = 2, b3 =3

i2 = 1, j2 = 2

Output

For example3

a = 0, b = 1

a = 2, b = 3

Output

#include <iostream.h>

int a, b = 1 ; // global objects

void main()

{ void f() ;

cout << "a = " << a << ", b = " << b << endl ;

f() ;

}

void f()

{ int a = 2; b = 3 ; // local objects

cout << "a = " << a << ", b = " << b << endl ;

return ;

}

A reference parameter passes an alias ( 别名 ) to the actual parameter.

5.7 REFERENCE PARAMETERS 引用参数

The interface of a function is

FunctionType FunctionName ( ParameterList ) ;

The ParameterList of value parameter is

ParameterType ParameterName

The ParameterList of reference parameter is

ParameterType & ParameterName

Indicates a reference parameter

// Swap two values#include<iostream.h>

void Swap ( int & , int & ) ;

void main()

{ int a = 3 , b = 8 ;

cout << "a=" << a << ", b=" << b << endl ;

Swap ( a , b ) ;

cout << "after swapping...\n" ;

cout << "a=" << a << ", b=" << b << endl ;

}

void Swap ( int & x& x , int & y& y )

{ int temp = xx ;

xx = yy ;

yy = temp ;

}

3

a

8

b

xx

yy

// Swap two values#include<iostream.h>

void Swap ( int & , int & ) ;

void main()

{ int a = 3 , b = 8 ;

cout << "a=" << a << ", b=" << b << endl ;

Swap ( a , b ) ;

cout << "after swapping...\n" ;

cout << "a=" << a << ", b=" << b << endl ;

}

void Swap ( int & x& x , int & y& y )

{ int temp = xx ;

xx = yy ;

yy = temp ;

}

3

a

8

b

xx

yy

// Swap two values#include<iostream.h>

void Swap ( int & , int & ) ;

void main()

{ int a = 3 , b = 8 ;

cout << "a=" << a << ", b=" << b << endl ;

Swap ( a , b ) ;

cout << "after swapping...\n" ;

cout << "a=" << a << ", b=" << b << endl ;

}

void Swap ( int & x& x , int & y& y )

{ int temp = xx ;

xx = yy ;

yy = temp ;

}

8

a

3

b

xx

yy

a = 3, b = 8

after swaooing …

a = 8, b = 3

Output

// Swap two values#include<iostream.h>

void Swap ( int & , int & ) ;

void main()

{ int a = 3 , b = 8 ;

cout << "a=" << a << ", b=" << b << endl ;

Swap ( a , b ) ;

cout << "after swapping...\n" ;

cout << "a=" << a << ", b=" << b << endl ;

}

void Swap ( int & x& x , int & y& y )

{ int temp = xx ;

xx = yy ;

yy = temp ;

}

Note

The actual parameters

that be connect with

reference parameters

must be names.

Follows are error:

Swap(a+b, b );Swap(a+b, b );

Swap(a, 23 ); Swap(a, 23 );

// compare#include<iostream.h>

void Swap ( int & , int & ) ;

void main()

{ int a = 3 , b = 8 ;

cout << "a=" << a << ", b=" << b << endl ;

Swap ( a , b ) ;

cout << "after swapping...\n" ;

cout << "a=" << a << ", b=" << b << endl ;

}

3

a

8

b

void Swap ( int xx , int yy )

{ int temp = xx ;

xx = yy ;

yy = temp ;

}

xx

y

// compare#include<iostream.h>

void Swap ( int & , int & ) ;

void main()

{ int a = 3 , b = 8 ;

cout << "a=" << a << ", b=" << b << endl ;

Swap ( a , b ) ;

cout << "after swapping...\n" ;

cout << "a=" << a << ", b=" << b << endl ;

}

3

a

8

b

void Swap ( int xx , int yy )

{ int temp = xx ;

xx = yy ;

yy = temp ;

}

xx

y

// compare#include<iostream.h>

void Swap ( int & , int & ) ;

void main()

{ int a = 3 , b = 8 ;

cout << "a=" << a << ", b=" << b << endl ;

Swap ( a , b ) ;

cout << "after swapping...\n" ;

cout << "a=" << a << ", b=" << b << endl ;

}

3

a

8

b

void Swap ( int xx , int yy )

{ int temp = xx ;

xx = yy ;

yy = temp ;

}

3

xx

8

y

// compare#include<iostream.h>

void Swap ( int & , int & ) ;

void main()

{ int a = 3 , b = 8 ;

cout << "a=" << a << ", b=" << b << endl ;

Swap ( a , b ) ;

cout << "after swapping...\n" ;

cout << "a=" << a << ", b=" << b << endl ;

}

3

a

8

b

void Swap ( int xx , int yy )

{ int temp = xx ;

xx = yy ;

yy = temp ;

}

8

xx

3

y

// compare#include<iostream.h>

void Swap ( int & , int & ) ;

void main()

{ int a = 3 , b = 8 ;

cout << "a=" << a << ", b=" << b << endl ;

Swap ( a , b ) ;

cout << "after swapping...\n" ;

cout << "a=" << a << ", b=" << b << endl ;

}

3

a

8

b

void Swap ( int xx , int yy )

{ int temp = xx ;

xx = yy ;

yy = temp ;

}

a = 3, b = 8

after swaooing …

a = 3, b = 8

Output

#include <iostream>

#include <string>

using namespace std;

// Swap(): swap two values

void Swap(int &x, int &y)

{ int tmp = x;

x = y; y = tmp;

return;

}

// Sort3(): sort three numbers into non-descending order

void Sort3(int &a, int &b, int &c)

{ if (a > b) Swap(a, b);

if (a > c) Swap(a, c);

if (b > c) Swap(b, c);

return;

}

// Program 6.7: Input three numbers and output them in sorted order

// read three numbers and output them in sorted order

int main()

{ cout << "Please enter three integers: " << flush;

int Input1; int Input2; int Input3;

cin >> Input1 >> Input2 >> Input3;

int Output1 = Input1;

int Output2 = Input2;

int Output3 = Input3;

// Sort the three numbers

Sort3(Output1, Output2, Output3);

// Output the sorted numbers

cout << Input1 << " " << Input2 << " " << Input3<< " in sorted order is "

<< Output1 << " " << Output2 << " " << Output3<< endl;

return 0;

}

// Program 6.7: Input three numbers and output them in sorted order

• When we extract data from a stream or insert data into a

stream, we are changing the stream. This change must be

reflected in the stream object.

• Thus whenever we need to pass a stream to a function, we

must pass the stream by reference.

5.8 PASSING OBJECTS BY REFERENCE

按引用传递对象

For example

void OutputValue ( ostream & out , int Value )

{ out << " Value is " << Value << endl ; }

call

OutputValue ( cout , 256 ) ;

OutputValue ( cerr , 25+6 ) ;

OutputValue ( clog , 25/6 ) ;

ofstream outfile ( "MyOutfile" ) ;

OutputValue ( outfile , 25/6 ) ;

For example

void OutputValue ( ostreamostream & out , int Value )

{ out << " Value is " << Value << endl ; }

call

OutputValue ( cout , 256 ) ;

OutputValue ( cerr , 25+6 ) ;

OutputValue ( clog , 25/6 ) ;

ofstreamofstream outfile ( "MyOutfile" ) ;

OutputValue ( outfile , 25/6 ) ;

Note

The type of actual parameter

is ofstream, but the type of f

ormal parameter is ostream.

They are different.

ofstream class is a derived c

lass of ostream.

Note

The type of actual parameter

is ofstream, but the type of f

ormal parameter is ostream.

They are different.

ofstream class is a derived c

lass of ostream.

For example

// Reads three values from stream

bool ReadValues ( istream & in , int &v1 , int &v2 , int &v3)

{ if ( in == cin )

cout << “Please enter three numbers" ;

if ( in >> v1 >> v2 >> v3 )

return true ;

else

return false ;

}

// Program 6.8: Sorting numbers read from a file#include <iostream>#include <fstream>#include <string>using namespace std;// ReadValues(): read three values, if from cin, then prompt userbool ReadValues(istream &in, int &v1, int &v2, int &v3){ if (in == cin) cout << "Please enter three numbers" << flush; if (in >> v1 >> v2 >> v3) return true; else return false;}// Swap(): swap two valuesvoid Swap(int &x, int &y) { int tmp = x; x = y; y = tmp; return; }// Sort3(): sort three numbers into non-descending ordervoid Sort3(int &a, int &b, int &c) { if (a > b) Swap(a, b); if (a > c) Swap(a, c); if (b > c) Swap(b, c); return;}

int main()

{ // Open the input file

ifstream fin("mydata.nbr");

if (!fin)

{ cerr << "Could not open mydata.nbr" << endl; return 1; }

int Input1, Input2, Input3;

if (!ReadValues(fin, Input1, Input2, Input3))

{ cerr << "Could not read three values" << endl; return 1; }

int Output1 = Input1;

int Output2 = Input2;

int Output3 = Input3;

// Sort the three numbers

Sort3(Output1, Output2, Output3);

// Output the sorted numbers

cout << Input1 << " " << Input2 << " " << Input3 << " in sorted order is "

<< Output1 << " " << Output2 << " " << Output3<< endl;

return 0;

}

A user type a five-digit number. The first three digits are added together.

The remainder of this sum when divided by the fourth digit should be

equal to the fifth digit.

Example:

12330 // OK

23531 // OK

12345 // Error

5.9 VALIDATING TELEPHONE ACCESS CODE

验证电话访问密码

// Program 6.9: Validate a telephone access code#include <iostream>

#include <string>

#include <ctype.h>

using namespace std;

// Prototypes for utility functions that main will use

bool Get(istream &in, int &d1, int &d2, int &d3, int &d4, int &d5);

bool Valid(int d1, int d2, int d3, int d4, int d5);

int main()

{ int d1, d2, d3, d4, d5;

if (Get(cin, d1, d2, d3, d4, d5) && Valid(d1, d2, d3, d4, d5)) // Call functions

{ cout << "OK!\n" ; return 0 ; }

else

{ cout << "Error!" ; return 1 ; }

}

// Inputs are five characters. They are turned into integral reference parameters

bool Get(istream &sin, int &d1, int &d2, int &d3,int &d4, int &d5)

{ char c1; char c2; char c3; char c4; char c5;

if (sin >> c1 >> c2 >> c3 >> c4 >> c5)

{ if (isdigitisdigit(c1) && isdigit(c2) && isdigit(c3)&& isdigit(c4) && isdigit(c5))

{ d1 = c1 - '0'; d2 = c2 - '0'; d3 = c3 - '0'; d4 = c4 - '0'; d5 = c5 - '0';

return true; }

else return false;

}

else return false;

}

// Examine the digit inputs and determine whether they represent a valid access code

bool Valid(int d1, int d2, int d3, int d4, int d5)

{ if (d4 == 0) return false;

else return ((d1 + d2 + d3) % d4) == d5;

}

Library function isdigit()isdigit()

• Definition in the ctype.h

• If its parameter is a decimal d

igit, it returns true; otherwise r

eturns false.

Library function isdigit()isdigit()

• Definition in the ctype.h

• If its parameter is a decimal d

igit, it returns true; otherwise r

eturns false.

// Inputs are five characters. They are turned into integral reference parameters

bool Get(istream &sin, int &d1, int &d2, int &d3,int &d4, int &d5)

{ char c1; char c2; char c3; char c4; char c5;

if (sin >> c1 >> c2 >> c3 >> c4 >> c5)

{ if (isdigitisdigit(c1) && isdigit(c2) && isdigit(c3)&& isdigit(c4) && isdigit(c5))

{ d1 = c1 - '0'; d2 = c2 - '0'; d3 = c3 - '0'; d4 = c4 - '0'; d5 = c5 - '0';

return true; }

else return false;

}

else return false;

}

// Examine the digit inputs and determine whether they represent a valid access code

bool Valid(int d1, int d2, int d3, int d4, int d5)

{ if (d4 == 0) return false;

else return ((d1 + d2 + d3) % d4) == d5;

}

• An object has the const modifier, it is “read only”.

• If the const modifier be used a parameter, the called function does not

modify the actual object.

5.10 CONSTANT PARAMETERS 常量参数

void Example ( const int a , int b , int c )

{ b = a + 3 ; // legal assignment

a = c + 5 ;a = c + 5 ; // illegal assignment

return ;

}

• An object has the const modifier, it is “read only”.

• If the const modifier be used a parameter, the called function does not

modify the actual object.

5.10 CONSTANT PARAMETERS 常量参数

Preserve the object of actual parameter :

• Value parameters

a copy of the object is made and the copy is passed to the called function.

• Constant parameters

pass the object as a constant reference parameter. It is efficiency and

safety.

// Program 6.10 :// Reformat a name from LastName, FirstName to FirstName <space> LastName

#include<iostream>

#include<string>

using namespace std ;

void ParseName (string &FirstName, string & LastName, const string & FullNamconst string & FullNam

ee)

{ int i=FullName.find(","); // i is order

LastName=FullName.substr(0,i); // i is length

FirstName=FullName.substr(i+2, FullName.size());

return ;

}

int main()

{ string Name="Stroustrup, Bjarne";

string FirstName , LastName;

ParseName(FirstName, LastName, NameName);

Name=FirstName+" "+LastName;

cout<<Name<<endl;

return 0;

}

// Complier limitations on enforcing the const modifier

#include<iostream.h>

void foo (int &p1, int p2)

{ p1=p2+p1+10;

return;

}

void example( /*const*/ int & CValue)

{ foo(CValue,3);

return;

}

void main()

{ int Con=100;

example(Con);

cout<<Con<<endl;

}

A const object can not

be passed by reference to

another function.

• Using default parameters,we can write a function that has a

default behavior.

• The default parameters must appear after any mandatory

parameters.

5.11 DEFAULT PARAMETERS 默认 参数

// using a default parameter to control output

void OutputValues ( ostream & out , int Value1 , int Value2 ,

bool DoubleSpace = false )

{ out << “Value 1 is " <<Value1 << endl ;

if ( DoubleSpace ) out << endl ;

out << “Value 2 is " <<Value2 << endl ;

if ( DoubleSpace ) out << endl ;

}

5.11 DEFAULT PARAMETERS 默认 参数

// Other example# include <iostream.h>

double power ( double real, int n = 2 ) ;

void main ( )

{ double r = 3.0 ;

cout << power ( r ) << endl ;

cout << power ( r, 3 ) << endl ;

}

double power ( double real , int n )

{ if ( n == 0 )

return 1.0 ;

double result = real ;

for ( int i = 2 ; i <= n ; i ++ )

result *= real ;

return result;

}

Declare default parameters:

int f ( ) ;

……

void delay ( int k , int time = f ( ) ) ; OKOK !!OKOK !!

Declare default parameters:

int f ( ) ;

……

void delay ( int k , int time = f ( ) ) ;

void ferror1 ( int x , int y = 1int y = 1 , int z ) ; ErrorError !!ErrorError !!

Declare default parameters:

int f ( ) ;

……

void delay ( int k , int time = f ( ) ) ;

void ferror1 ( int x , int y = 1int y = 1 , int z ) ;

void ferror2 ( int x , int y = 0 ) ;

void ferror2 ( int x ) ;

Call : ferror2 ( 3 ) ;ferror2 ( 3 ) ; // Who can be call ?

ErrorError !!ErrorError !!

The C++ compiler convert actual parameter expression to the type of

formal parameters .

5.12 CASTING OF FUNCTION PARAMETERS

函数参数的类型转换

For example

void fun ( int a , double x ) ;

fun ( 155.8/3 , 45000 ) ;

Function overloading

Create several functions of the same that behave differently.

• A identifier can be name for several functions.

• Compiler can confirm a function that is called in accordance with

parameters.

• Can not confirm overloading functions if the type of that return are

different only.

5.13 FUNCTION OVERLOADING 函数重载

// The number of parameters are parity, but type are different

# include <iostream.h>

int abs ( int aint a ) ;

double abs ( double fdouble f ) ;

void main ( )

{ cout << abs ( -5 ) << endl ;

cout << abs ( -7.8 ) << endl ;

}

int abs ( int a )

{ return a < 0 ? -a : a ; }

double abs ( double f )

{ return f < 0 ? -f : f ; }

? :? : triary ( 三元 ) operator

operand1 ?? operand2 :: operand3

If operand1 is true, the value of expre

ssion is operand2, otherwise that is o

perand3.

Example

int a=1, b=2, c ;

c = a ? a+b : a-b ;

// if (a) c=a+b; else c=a-b;

? :? : triary ( 三元 ) operator

operand1 ?? operand2 :: operand3

If operand1 is true, the value of expre

ssion is operand2, otherwise that is o

perand3.

Example

int a=1, b=2, c ;

c = a ? a+b : a-b ;

// if (a) c=a+b; else c=a-b;

// The number of parameters different# include <iostream.h>

int max ( int a , int b ) ;int max ( int a , int b ) ;

int max ( int a , int b, int c ) ;int max ( int a , int b, int c ) ;

void main ( )

{ cout << max ( 5, 3 ) << endl ;

cout << max (4, 8, 2 ) << endl ;

}

int max ( int a , int b )

{ return a > b ? a : b ; }

int max ( int a , int b, int c )

{ int t ;

t = max ( a , b )max ( a , b ) ;

return max ( t , c )max ( t , c ) ;

}

// Rewrite program : Swap two values#include<iostream.h>void Swap ( int & , int & ) ; void main(){ int a = 3 , b = 8 ; Swap ( a , b ) ; cout << "after swapping...\n" << "a=" << a << ", b=" << b << endl ; float x = 0.618 , y = 2.71828 ; Swap ( x , y ) ; cout << "after swapping...\n" << “x=" << x << ", y=" << y << endl ;}void Swap ( int & x , int & y ) { int temp = x ; x = y ; y = temp ;}void Swap ( float & x , float & y ) { float temp = x ; x = y ; y = temp ;}

void ferror2 ( int x , int y = 0 ) ;

void ferror2 ( int x ) ;

Call : ferror2 ( 3 ) ;ferror2 ( 3 ) ; // Who can be call ?

void ferror2 ( int x , int y = 0 ) ;

void ferror2 ( int x ) ;

Call : ferror2 ( 3 ) ;ferror2 ( 3 ) ; // Who can be call ?

5.14 RECURSIVE FUNCTION 递归函数

Recursive definition

A object can be defined by itself or itself partly

Example1:

Natural number

1 is a natural number, the next natural numbers are natural number

Example2:

The definition of factorial

1nif)!1n(n

0nif1!n)n(Factorial

• Recursion is the the ability of a function to call itself.

• The recursive function can be executed, because the local objects and

value parameters push into stack (压入堆栈) when the function is

recursive called. They are pop out (弹出) in opposite order when

unwinding.

5.14 RECURSIVE FUNCTION 递归函数

Recursive function

• Recursive model

• The condition of executing the recursion

• The condition of stopping the recursion

int Factorial ( int n ) {

if ( n = = 0 )

return 1 ;

else

return n * Factorial ( n - 1 ) ;

}

1nif)!1n(n

0nif1!n)n(Factorial

1nif)!1n(n

0nif1!n)n(Factorial

int Factorial ( int n ) {

if ( n = = 0 )

return 1 ;

else

return n * Factorial ( n - 1 ) ;

}

Recursive model

1nif)!1n(n

0nif1!n)n(Factorial

int Factorial ( int n ) {

if ( n = = 0 )

return 1 ;

else

return n * Factorial ( n - 1 ) ;

}

condition of stopping Recursive

1nif)!1n(n

0nif1!n)n(Factorial

int Factorial ( int n ) {

if ( n = = 0 )

return 1 ;

else

return n * Factorial ( n - 1 ) ;

}

Modify the condition Recursive

int Factorial ( int n ) {

if ( n = = 0 ) return 1 ;

else return n * Factorial ( n - 1 ) ;

}

k 3

F (3)

Output 3! = 6Output 3! = 6

n 3

F (3) 3* F (2)3* F (2)

nn 22

F(2)F(2) 2*2* F(1)F(1)

n 1

F (0) 1

Compute Factorial (3) = 3!

F(1)F(1)

n 0

1*1* F(0)F(0)

int Factorial ( int n ) {

if ( n = = 0 ) return 1 ;

else return n * Factorial ( n - 1 ) ;

}

k 3

F (3)

Output 3! = 6Output 3! = 6

n 3

F (3) 3* F (2)3* F (2)

nn 22

F(2)F(2) 2*2* F(1)F(1)

n 1

F (0) 1

Compute Factorial (3) = 3!

F(1)F(1)

n 0

1*1* F(0)F(0)1

int Factorial ( int n ) {

if ( n = = 0 ) return 1 ;

else return n * Factorial ( n - 1 ) ;

}

k 3

F (3)

Output 3! = 6Output 3! = 6

n 3

F (3) 3* F (2)3* F (2)

nn 22

F(2)F(2) 2*2* F(1)F(1)

n 1

Compute Factorial (3) = 3!

F(1)F(1)

int Factorial ( int n ) {

if ( n = = 0 ) return 1 ;

else return n * Factorial ( n - 1 ) ;

}

k 3

F (3)

Output 3! = 6Output 3! = 6

n 3

F (3) 3* F (2)3* F (2)

nn 22

F(2)F(2) 2*2* F(1)F(1)

n 1

Compute Factorial (3) = 3!

F(1)F(1)

F (1) = 1F (1) = 1

1

int Factorial ( int n ) {

if ( n = = 0 ) return 1 ;

else return n * Factorial ( n - 1 ) ;

}

k 3

F (3)

Output 3! = 6Output 3! = 6

n 3

F (3) 3* F (2)3* F (2)

nn 22

F(2)F(2)

Compute Factorial (3) = 3!

F(2) = 2F(2) = 2

2

int Factorial ( int n ) {

if ( n = = 0 ) return 1 ;

else return n * Factorial ( n - 1 ) ;

}

k 3

F (3)

Output 3! = 6Output 3! = 6

n 3

F (3)

Compute Factorial (3) = 3!

int Factorial ( int n ) {

if ( n = = 0 ) return 1 ;

else return n * Factorial ( n - 1 ) ;

}

k 3

F (3)

Output 3! = 6Output 3! = 6

n 3

F (3)

Compute Factorial (3) = 3!

F(3) = 6F(3) = 6

int Factorial ( int n ) {

if ( n = = 0 ) return 1 ;

else return n * Factorial ( n - 1 ) ;

}

k 3

F (3)

Output 3! = 6Output 3! = 6

Compute Factorial (3) = 3!

int Factorial ( int n ) {

if ( n = = 0 ) return 1 ;

else return n * Factorial ( n - 1 ) ;

}

k 3

F (3)

Output 3! = 6Output 3! = 6

Compute Factorial (3) = 3!

3! = 6

int Factorial ( int n ) {

if ( n = = 0 ) return 1 ;

else return n * Factorial ( n - 1 ) ;

}

Compute Factorial (3) = 3!

k 3

F (3)

k 3

F (3)

n 3

F (3) 3* F (2)3* F (2)

nn 22

F(2)F(2) 2*2* F(1)F(1)

n 1

F (0) 1

F(1)F(1)

n 0

1*1* F(0)F(0)

F (1) = 1F (1) = 1F(2) = 2F(2) = 2F(3) = 6F(3) = 6Output 3! = 6Output 3! = 6

// Compute Factorial

#include<iostream.h>

int Factorial ( int ) ;

void main ()

{ int k ;

cout << "Compute Factorial(k) , Please input k: " ;

cin >> k ;

cout << k << "! = " << Factorial(k) << endl ;

}

int Factorial ( int n ) {

if ( n == 0 )

return 1 ;

else

return n * Factorial ( n - 1 ) ;

}

The generally format of recursive function

F ( x1 , x2 , … , xn ) if ( P1 ) E1

else if ( P2 ) E2

……

else if ( Pm ) Em

else Em+1

Pi ( i = 1, 2, …… , m ) is test expression

Ei ( i = 1, 2, …… , m +1 ) is expression

F() can presence within PF() can presence within Pi i and Eand Eii

Other examplesOther examples

The mathematical definition of the nth Fibonacci number is

2nif

2nif

1nif

FFF

1F

1F

F

2n1nn

n

n

n

int Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Other examplesOther examplesint Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Fibonacci (5)

Fibonacci (4) Fibonacci (3)

Fibonacci (3) Fibonacci (2)

Fibonacci (2) Fibonacci (1)

n stack

5432

Other examplesOther examplesint Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Fibonacci (5)

Fibonacci (4) Fibonacci (3)

Fibonacci (3) Fibonacci (2)

Fibonacci (2) Fibonacci (1)

n stack

5432

1

Other examplesOther examplesint Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Fibonacci (5)

Fibonacci (4) Fibonacci (3)

Fibonacci (3) Fibonacci (2)

Fibonacci (1)

n stack

543

1

1

Fibonacci (1)

Other examplesOther examplesint Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Fibonacci (5)

Fibonacci (4) Fibonacci (3)

Fibonacci (3) Fibonacci (2)

Fibonacci (1)

n stack

543

1

1

Fibonacci (1)

1

Other examplesOther examplesint Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Fibonacci (5)

Fibonacci (4) Fibonacci (3)

Fibonacci (3) Fibonacci (2)

n stack

543

2

1 1

Other examplesOther examplesint Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Fibonacci (5)

Fibonacci (4) Fibonacci (3)

Fibonacci (2)

n stack

54

22

Fibonacci (2)

Other examplesOther examplesint Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Fibonacci (5)

Fibonacci (4) Fibonacci (3)

Fibonacci (2)

n stack

54

22

Fibonacci (2)

1

Other examplesOther examplesint Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Fibonacci (5)

Fibonacci (4) Fibonacci (3)

n stack

54

2

1

3

Other examplesOther examplesint Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Fibonacci (5)

Fibonacci (3)

n stack

5

3

Fibonacci (3)

Fibonacci (1)Fibonacci (2) 32

Other examplesOther examplesint Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Fibonacci (5)

Fibonacci (3)

n stack

5

3

Fibonacci (3)

Fibonacci (1)Fibonacci (2) 32

1

Other examplesOther examplesint Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Fibonacci (5)

Fibonacci (3)

n stack

5

3

Fibonacci (3)

Fibonacci (1) 3

1

Fibonacci (1)1

Other examplesOther examplesint Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Fibonacci (5)

Fibonacci (3)

n stack

5

3

Fibonacci (3)

Fibonacci (1) 3

1

Fibonacci (1)1

1

Other examplesOther examplesint Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Fibonacci (5)

Fibonacci (3)

n stack

5

3

Fibonacci (3)

3

1 1

2

Other examplesOther examplesint Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Fibonacci (5)n stack

5

3

2

5

Other examplesOther examplesint Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Fibonacci (5)n stack

5

Other examplesOther examplesint Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Fibonacci (5)

Fibonacci (4) Fibonacci (3)

Fibonacci (3) Fibonacci (2) Fibonacci (1)Fibonacci (2)

Fibonacci (2) Fibonacci (1)

5

3 2

2 1

1 1

1 1

Other examplesOther examples

// Input a string, output in opposite order

# include <iostream.h>

void reverse ()

{ char ch ; // local object

cin >> ch;

if ( ch != '.' )

reverse() ;

cout << ch ;

}

void main ()

{ cout << " Input a string : " << endl ;

reverse() ;

cout << endl ;

}

Other examplesOther examples

// Revers decimal positive number

# include <iostream.h>

void reverse ( int n )

{ cout << n % 10 ; // output a last number at right

if ( n/10 != 0 )

reverse ( n/10 ); // compute quotient, recursive

}

void main ()

{ int k ;

cout << "Input a integer number( > 0 ) : "<< endl ;

cin >> k ;

reverse ( k ) ;

cout << endl ;

}

Other examplesOther examples

// Hanoi tower

A B C

Other examplesOther examples

// Hanoi tower

A B C

Other examplesOther examples

// Hanoi tower

A B C

Other examplesOther examples

// Hanoi tower

A B C

Other examplesOther examples

// Hanoi tower

A B C

Other examplesOther examples

// Hanoi tower

A B C

Other examplesOther examples

// Hanoi tower

A B C

Other examplesOther examples

// Hanoi tower # include < iostream.h >void hanoi ( int n, char a, char b, char c ) { if ( n >= 1 ) { hanoi ( n-1, a, c, b ) ; cout << a << " --> " << c << endl ; hanoi ( n-1, b, a, c ) ; } }void main () { int m ; cout << " Input the number of diskes: " << endl ; cin >> m ; hanoi ( m, 'A' , 'B' , 'C' ) ; }

Other examplesOther examples// Hanoi tower void hanoi ( int n, char a, char b, char c ) { if ( n >= 1 ) { hanoi ( n-1, a, c, b) ; cout<<a<<" --> "<<c<<endl; hanoi (n-1, b, a, c) ; } }

Stack

n a b cn a b c

H ( 3, A, B, C )

3 A B C

H ( 2, A, C, B )2 A C B

H ( 1, A, B, C )

1 A B C

Output

A CH(3,A,B,C)

H(n-1,a,c,b)

H(n-1,a,c,b)

cout

Other examplesOther examples// Hanoi tower void hanoi ( int n, char a, char b, char c ) { if ( n >= 1 ) { hanoi ( n-1, a, c, b) ; cout<<a<<" --> "<<c<<endl; hanoi (n-1, b, a, c) ; } }

Stack

n a b cn a b c

H ( 3, A, B, C )

3 A B C

H ( 2, A, C, B )2 A C B

H ( 1, A, B, C )

1 A B C

Output

A C

A B

H(3,A,B,C)

H(n-1,a,c,b)

H(n-1,a,c,b)

cout

Other examplesOther examples// Hanoi tower void hanoi ( int n, char a, char b, char c ) { if ( n >= 1 ) { hanoi ( n-1, a, c, b) ; cout<<a<<" --> "<<c<<endl; hanoi (n-1, b, a, c) ; } }

Stack

n a b cn a b c

H ( 3, A, B, C )

3 A B C

H ( 2, A, C, B )2 A C B

Output

A C

A B

H(3,A,B,C)

H(n-1,a,c,b)

H ( 1, C, A, B )

1 C A B

H(n-1,b,a,c)

C B

cout

Other examplesOther examples// Hanoi tower void hanoi ( int n, char a, char b, char c ) { if ( n >= 1 ) { hanoi ( n-1, a, c, b) ; cout<<a<<" --> "<<c<<endl; hanoi (n-1, b, a, c) ; } }

Stack

n a b cn a b c

H ( 3, A, B, C )

3 A B C

H ( 2, A, C, B )2 A C B

Output

A C

A B

H(3,A,B,C)

H(n-1,a,c,b)

H ( 1, C, A, B )

1 C A B

H(n-1,b,a,c)

C B

Other examplesOther examples// Hanoi tower void hanoi ( int n, char a, char b, char c ) { if ( n >= 1 ) { hanoi ( n-1, a, c, b) ; cout<<a<<" --> "<<c<<endl; hanoi (n-1, b, a, c) ; } }

Stack

n a b cn a b c

H ( 3, A, B, C )

3 A B C

H ( 2, A, C, B )2 A C B

Output

A C

A B

H(3,A,B,C)

H(n-1,a,c,b) C B

A C

cout

Other examplesOther examples// Hanoi tower void hanoi ( int n, char a, char b, char c ) { if ( n >= 1 ) { hanoi ( n-1, a, c, b) ; cout<<a<<" --> "<<c<<endl; hanoi (n-1, b, a, c) ; } }

Stack

n a b cn a b c

H ( 3, A, B, C )

3 A B C

Output

A C

A B

H(3,A,B,C)

C BH ( 2, B, A, C )2 B A C

H ( 1, B, C, A )

1 B C AH(n-1,b,a,c)

H(n-1,a,c,b)

A C

B A

cout

Other examplesOther examples// Hanoi tower void hanoi ( int n, char a, char b, char c ) { if ( n >= 1 ) { hanoi ( n-1, a, c, b) ; cout<<a<<" --> "<<c<<endl; hanoi (n-1, b, a, c) ; } }

Stack

n a b cn a b c

H ( 3, A, B, C )

3 A B C

Output

A C

A B

H(3,A,B,C)

C BH ( 2, B, A, C )2 B A C

H ( 1, B, C, A )

1 B C AH(n-1,b,a,c)

H(n-1,a,c,b)

A C

B A

B C

cout

Other examplesOther examples// Hanoi tower void hanoi ( int n, char a, char b, char c ) { if ( n >= 1 ) { hanoi ( n-1, a, c, b) ; cout<<a<<" --> "<<c<<endl; hanoi (n-1, b, a, c) ; } }

Stack

n a b cn a b c

H ( 3, A, B, C )

3 A B C

Output

A C

A B

H(3,A,B,C)

C BH ( 2, B, A, C )2 B A C

H(n-1,b,a,c)A C

B AH ( 1, A, B, C )

1 A B C

H(n-1,b,a,c) B C

A Ccout

Other examplesOther examples// Hanoi tower void hanoi ( int n, char a, char b, char c ) { if ( n >= 1 ) { hanoi ( n-1, a, c, b) ; cout<<a<<" --> "<<c<<endl; hanoi (n-1, b, a, c) ; } }

Stack

n a b cn a b c

H ( 3, A, B, C )

3 A B C

Output

A C

A B

H(3,A,B,C)

C BH ( 2, B, A, C )2 B A C

H(n-1,b,a,c)A C

B AH ( 1, A, B, C )

1 A B C

H(n-1,b,a,c) B C

A C

Other examplesOther examples// Hanoi tower void hanoi ( int n, char a, char b, char c ) { if ( n >= 1 ) { hanoi ( n-1, a, c, b) ; cout<<a<<" --> "<<c<<endl; hanoi (n-1, b, a, c) ; } }

Stack

n a b cn a b c

H ( 3, A, B, C )

3 A B C

Output

A C

A B

H(3,A,B,C)

C BH ( 2, B, A, C )2 B A C

H(n-1,b,a,c)A C

B A

B C

A C

Other examplesOther examples// Hanoi tower void hanoi ( int n, char a, char b, char c ) { if ( n >= 1 ) { hanoi ( n-1, a, c, b) ; cout<<a<<" --> "<<c<<endl; hanoi (n-1, b, a, c) ; } }

Stack

n a b cn a b c

H ( 3, A, B, C )

3 A B C

Output

A C

A B

H(3,A,B,C)

C B

A C

B A

B C

A C

Over