Advertisement
Guest User

C++ Help

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