Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- It does not support combinations that start with one or more zeroes. For example, a combination of 0042 can be opened by entering just 42.
- Solve this problem by choosing a different representation. Instead of an integer, use a string to hold the input.
- */
- #include <iostream>
- #include <string>
- using namespace std;
- /**
- A simulated lock with digit buttons.
- */
- class Lock
- {
- public:
- /**
- Simulates a digit button push.
- @param button a digit 0 ... 9
- */
- void push(int button);
- /**
- Simulates a push of the open button.
- @return true if the lock opened
- */
- bool open();
- private:
- string combination = "0042";
- string input;
- };
- void Lock::push(int button)
- {
- string button_as_string = to_string(button);
- . . .
- }
- bool Lock::open()
- {
- . . .
- }
- int main()
- {
- Lock my_lock;
- my_lock.push(4);
- my_lock.push(2);
- cout << boolalpha;
- cout << my_lock.open() << endl;
- cout << "Expected: false" << endl;
- my_lock.push(0);
- my_lock.push(0);
- my_lock.push(4);
- my_lock.push(2);
- cout << my_lock.open() << endl;
- cout << "Expected: true" << endl;
- my_lock.push(0);
- my_lock.push(4);
- my_lock.push(2);
- cout << my_lock.open() << endl;
- cout << "Expected: false" << endl;
- my_lock.push(0);
- my_lock.push(0);
- my_lock.push(0);
- my_lock.push(4);
- my_lock.push(2);
- cout << my_lock.open() << endl;
- cout << "Expected: false" << endl;
- my_lock.push(0);
- my_lock.push(0);
- my_lock.push(4);
- my_lock.push(2);
- cout << my_lock.open() << endl;
- cout << "Expected: true" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement