通用方法
void recurs(argumentlist)
{
statments1
if (text)
recurs(argumemts)
statments2
}
示例一:
#include<iostream>
void countdown(int n);
int main()
{
countdown(5);
return 0;
}
void countdown(int n)
{
std::cout << "Counting down ..." << n <<std::endl;
if (n > 0)
{
countdown(n-1);
}
std::cout << n << " : kaboom!" << std::endl;
}
示例二:
#include<iostream>
#include"personku.h"
const int Len = 66;
const int Divs = 6;
void subdivide(char ar[], int low, int high, int level)
{
if (level == 0)
return;
int mid = (low + high) / 2;
ar[mid] = '|';
subdivide(ar, low, mid, level - 1);
subdivide(ar, mid, high, level - 1);
}
int main()
{
char ruler[Len];
int i;
for (i = 0; i < Len - 1; i++)
ruler[i] = ' ';
int max = Len - 2;
int min = 0;
ruler[min] = ruler[max] = '|';
ruler[Len - 1] = '\0';
std::cout << ruler << std::endl << std::endl;
for (i = 1; i <= Divs; i++)
{
subdivide(ruler, min, max, i);
std::cout << ruler << std::endl << std::endl;
}
return 0;
}