#include #include #include #include #include struct Cloth { std::string name; int size; }; std::ostream& operator<<(std::ostream& oss, Cloth const& c) { return oss << c.name << ": " << c.size; } class WashMachine { public: WashMachine() : items{} {} void start() { Cloth tangle{"", 0}; while ( ! items.empty() ) { Cloth c{ items.back() }; tangle.name += c.name; tangle.size += c.size; items.pop_back(); } items.push_back( tangle ); } void insert(Cloth const& o) { items.push_back(o); } bool is_empty() const { return items.size() == 0; } Cloth remove() { Cloth o{items.back()}; items.pop_back(); return o; } private: std::vector items; }; int main() { std::cout << "=== Test 0 ===" << std::endl; { Cloth c1{"PJ", 5}; Cloth c2{"JohnLongs", 10}; std::cout << c1 << ", " << c2 << std::endl; } std::cout << "=== Test 1 ===" << std::endl; { WashMachine m1{}; m1.insert(Cloth{"PJ", 7}); m1.insert(Cloth{"Sock", 3}); m1.insert(Cloth{"LongJohns", 10}); m1.insert(Cloth{"Bed Sheet", 90}); m1.insert(Cloth{"Shoe", 8}); m1.insert(Cloth{"T-Shirt", 8}); // Vi glömde avsiktligt att starta maskinen! while ( not m1.is_empty() ) { std::cout << m1.remove() << std::endl; } } std::cout << "=== Test 2 ===" << std::endl; { WashMachine m1{}; m1.insert(Cloth{"PJ", 7}); m1.insert(Cloth{"Sock", 3}); m1.insert(Cloth{"LongJohns", 10}); m1.insert(Cloth{"Bed Sheet", 90}); m1.insert(Cloth{"Shoe", 8}); m1.insert(Cloth{"T-Shirt", 8}); m1.start(); // Vi kom ihåg starta maskinen while ( not m1.is_empty() ) { std::cout << m1.remove() << std::endl; } } }