/// @file   main.cpp
/// @author Jens Gruschel
/// @date   2016-07-14
/// Berechnung von Potenzen, Fakultaeten und Binomialkoeffizienten

#include <iostream>
#include <stdexcept>
#include <string>

using std::cout;
using std::endl;
using std::string;

// eigene Exception-Klasse
class negative_argument : public std::invalid_argument
{
public:
    explicit negative_argument(const string& what_arg) : std::invalid_argument(what_arg) {}
};

int fak(int n)
{
    // sicherstellen, dass n positiv ist
    if (n < 0) throw negative_argument("von negativen Zahlen kann man die Fakultaet nicht berechnen");

    // die Zahl 1 mit den Zahlen von 2 bis n multiplizieren
    int result = 1;
    for (int i = 2; i <= n; ++i)
    {
        result *= i;
    }
    return result;
}

int main()
{
    // Fakultaeten ausgeben
    // (beginnend mit -1, um einen Fehler zu provozieren)
    for (int i = -1; i <= 10; ++i)
    {
        try
        {
            cout << fak(i) << ", ";
        }
        catch (std::exception& e)
        {
            cout << e.what() << ", ";
        }
    }
    cout << endl;

    return 0;
}