/* An enumeration is just a listing of all of the items, in no particular order. An enumerator is something that produces an enumeration. Every item in the set must be produced eventually, but it doesn't matter when. And of course it can't produce any one item twice. */ #include #include class EnumNat { protected: string val; public: EnumNat() { val = "0"; } void reset() { val = "0"; } bool run_out() { return false; } string current() { return val; } void move_on() { int pos = val.length() - 1; while (pos>=0 && val[pos]=='9') { val[pos] = '0'; pos -= 1; } if (pos >= 0) val[pos] += 1; // safe using ASCII, but not perfect. else { string one = "1"; val = one + val; } } }; class EnumEven { protected: EnumNat e; public: void reset() { e.reset(); } bool run_out() { return e.run_out(); } string current() { return e.current(); } void move_on() { e.move_on(); e.move_on(); } }; class EnumInt { protected: EnumNat e; char turn; public: EnumInt() { turn = '+'; } void reset() { e.reset(); turn = '+'; } bool run_out() { return turn == '+' && e.run_out(); } string current() { if (turn == '+') return e.current(); else return string("-") + e.current(); } void move_on() { if (turn == '+') { e.move_on(); turn = '-'; } else turn = '+'; } }; void main() { EnumNat en; while (! en.run_out()) { string ns = en.current(); cout << ns << "\n"; en.move_on(); } } void main2() { EnumNat en; EnumEven ee; while (! en.run_out() && ! ee.run_out()) { string ns = en.current(); string es = ee.current(); cout << ns << "\t" << es << "\n"; en.move_on(); ee.move_on(); } if (en.run_out()) if (ee.run_out()) cout << "Even and Natural numbers same finite cardinality\n"; else cout << "There are more even numbers than natural numbers\n"; else if (ee.run_out()) cout << "There are more natural numbers than even numbers\n"; } void main3() { EnumNat en; EnumInt ei; while (! en.run_out() && ! ei.run_out()) { string ns = en.current(); string is = ei.current(); cout << ns << "\t" << is << "\n"; en.move_on(); ei.move_on(); } if (en.run_out()) if (ei.run_out()) cout << "Natural numbers and integers same finite cardinality\n"; else cout << "There are more integers than natural numbers\n"; else if (ei.run_out()) cout << "There are more natural numbers than integers\n"; } void main4() { EnumInt ei; while (! ei.run_out()) { string ns = ei.current(); cout << ns << "\n"; ei.move_on(); } cout << "We ran out of integers!\n"; }