書中頁數:P186
代碼名稱:LocalMath.h LocalMath.cc fact.cc
//LocalMath.h
#ifndef LOCALMATH_H
#define LOCALMATH_H
//definition in LocalMath.cc
int fact(int); // iterative definition of factorial
int factorial(int); // recrusive version of factorial
int gcd(int, int); // find greatest common divisor
#endif
//LocalMath.cc
#include "LocalMath.h"
// return the greatest common divisor
int gcd(int v1, int v2)
{
while (v2) {
int temp = v2;
v2 = v1 % v2;
v1 = temp;
}
return v1;
}
// factorial of val is val * (val - 1) * (val - 2) . . . * ((val - (val - 1)) * 1)
int fact(int val)
{
int ret = 1; // local variable to hold the result as we calculate it
while (val > 1)
ret *= val--; // assign ret * val to ret and decrement val
return ret; // return the result
}
// recursive version of factorial:
// calculate val!, which is 1 * 2 * 3 . . . * val
int factorial(int val)
{
if (val > 1)
return factorial(val-1) * val;
return 1;
}
//fact.cc
#include <iostream>
using std::cout; using std::endl;
// declarations of our factorial functions
// definitions are in LocalMath.cc
#include "LocalMath.h"
int main()
{
cout << factorial(5) << endl;
cout << fact(5) << endl;
cout << factorial(0) << endl;
cout << fact(0) << endl;
return 0;
}