Practice

KeyPad



What is the exact output of the following program?  Show your intermediate work.


 #include <iostream>
 using namespace std;
 #define BUFFER_SIZE 2

 struct Key {
   private:
     char letter;
     int  sCode;
   public:
     void setup(int d, char c) {
         letter = c;
         sCode  = d;
         cout << sCode << '=' << letter << ' ';
     }
     int  scanCode() const { return sCode; }
     char symbol() const { return letter; }
 };

 struct Keypad {
   private:
     Key* key;
     int  nKeys;
     char buffer[BUFFER_SIZE];
     int  iBuffer;
     int  shift;
   public:
     void setup(const char* c, const int code[]) {
         nKeys = strlen(c);
         key = new Key[nKeys];
         for (int i = 0; i < nKeys; i++) {
             if (c[i] >= 'A' && c[i] <= 'Z')
                 key[i].setup(code[i], c[i] + ' ');
             else
                 key[i].setup(code[i], c[i]);
             if (i % 3 == 2)
                 cout << endl;
         }
         cout << endl;
         iBuffer = 0;
         shift   = 0;
     }
     void flush() {
         for (int i = 0; i < iBuffer; i++)
             cout << buffer[i];
         cout << endl;
         iBuffer = 0;
     }
     void press(int code) {
         int found = 0;
         for (int i = 0; i < nKeys && found == 0; i++) {
             if (code == key[i].scanCode()) {
                 found = 1;
                 if (iBuffer == BUFFER_SIZE)
                     flush();
                 if (shift == 1)
                     buffer[iBuffer++] = key[i].symbol() - ' ';
                 else
                     buffer[iBuffer++] = key[i].symbol();
             }
         }
         if (found == 0)
             cout << "..." << endl;
     }
     void shiftOn()  { shift = 1; }
     void shiftOff() { shift = 0; }
     void closeUp() {
         flush();
         delete [] key;
         cout << "All Done!" << endl;
     }
 };

 int main() {
     Keypad keypad;
     int code[] = {12, 31, 10, 93, 39};

     keypad.setup("AbcdE", code);

     keypad.shiftOn();
     keypad.press(31);
     keypad.press(12);
     keypad.flush();

     keypad.shiftOff();
     keypad.press(10);
     keypad.press(93);
     keypad.press(39);
     keypad.closeUp();

     return 0;
 }

Show your output here:













Show your rough work on a separate page.


   Printer Friendly Version of this Page print this page     Top  Go Back to the Top of this Page



  Designed by Chris Szalwinski   Copying From This Site