Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Count to 10, /prog/

Name: Anonymous 2012-07-25 16:31

1

Name: Anonymous 2012-07-26 19:33

#include <string>
#include <vector>
#include <stdexcept>
#include <sstream>
#include <iostream>
#include <algorithm>

using namespace std;

class Number
{
protected:
    unsigned num; // Extend this with bignum capabilities in the future

public:
    Number()
    {
        num = 0;
    }
   
    Number(const Number& rhs) : num(rhs.num)
    {
        // Do nothing
    }
   
    string to_string()
    {
        ostringstream os;
        os << num;
        return os.str();
    }
   
    friend class NumberIncrementer;
};

Number zero;

class NumberIncrementer
{
public:
    void increment(Number *num)
    {
        if (num == 0) {
            throw runtime_error("Unknown error.");
        }
       
        num->num = num->num + 1;
    }
};

class NumberGenerator
{
private:
    Number *num;
    NumberIncrementer *inc;

public:
    NumberGenerator()
    {
        num = new Number(zero);
        inc = new NumberIncrementer();
        inc->increment(num);
    }
   
    ~NumberGenerator()
    {
        delete num;
        delete inc;
    }
   
    bool increment()
    {
        try {
            inc->increment(num);
        } catch (exception& e) {
            return false;
        }
        return (bool)this;
    }
   
    Number get()
    {
        return *num;
    }
};

NumberGenerator ng;

struct func_t {
    void operator()(int)
    {
        Number n;
        n = ng.get();
        cout << n.to_string() << endl;
        ng.increment();
    }
} func;

int main()
{
    vector<int> i;
   
    i.push_back(1);
    i.push_back(2);
    i.push_back(3);
    i.push_back(4);
    i.push_back(5);
    i.push_back(6);
    i.push_back(7);
    i.push_back(8);
    i.push_back(9);
    i.push_back(10);
   
    for_each(i.begin(), i.end(), func);
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List