Advertisement
Guest User

C++ Help

a guest
Apr 7th, 2020
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. 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.
  3.  
  4. Solve this problem by choosing a different representation. Instead of an integer, use a string to hold the input.
  5. */
  6. #include <iostream>
  7. #include <string>
  8. using namespace std;
  9.  
  10. /**
  11.    A simulated lock with digit buttons.
  12. */
  13. class Lock
  14. {
  15. public:
  16.    /**
  17.       Simulates a digit button push.
  18.       @param button a digit 0 ... 9
  19.    */
  20.    void push(int button);
  21.  
  22.    /**
  23.       Simulates a push of the open button.
  24.       @return true if the lock opened
  25.    */
  26.    bool open();
  27. private:
  28.    string combination = "0042";
  29.    string input;
  30. };
  31.  
  32. void Lock::push(int button)
  33. {
  34.    string button_as_string = to_string(button);
  35.    . . .
  36. }
  37.  
  38. bool Lock::open()
  39. {
  40.    . . .
  41. }
  42.  
  43. int main()
  44. {
  45.    Lock my_lock;
  46.    my_lock.push(4);
  47.    my_lock.push(2);
  48.    cout << boolalpha;
  49.    cout << my_lock.open() << endl;
  50.    cout << "Expected: false" << endl;
  51.    my_lock.push(0);
  52.    my_lock.push(0);
  53.    my_lock.push(4);
  54.    my_lock.push(2);
  55.    cout << my_lock.open() << endl;
  56.    cout << "Expected: true" << endl;
  57.    my_lock.push(0);
  58.    my_lock.push(4);
  59.    my_lock.push(2);
  60.    cout << my_lock.open() << endl;
  61.    cout << "Expected: false" << endl;
  62.    my_lock.push(0);
  63.    my_lock.push(0);
  64.    my_lock.push(0);
  65.    my_lock.push(4);
  66.    my_lock.push(2);
  67.    cout << my_lock.open() << endl;
  68.    cout << "Expected: false" << endl;
  69.    my_lock.push(0);
  70.    my_lock.push(0);
  71.    my_lock.push(4);
  72.    my_lock.push(2);
  73.    cout << my_lock.open() << endl;
  74.    cout << "Expected: true" << endl;
  75.    return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement