Practice

Cards



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


 #include <iostream>
 #include <cstring>
 using namespace std;
 #define NC 3

 struct Card {
 private:
     int s;
     int r;
     char sr[4];
 public:
     int  set(const char* c) {
         int rc;
         char rr[] = "234567891JQKA";

         if (c[0]=='1') {
             sr[0] = '1';
             sr[1] = '0';
             sr[2] = c[2];
             rc = 3;
         } else {
             sr[0] = ' ';
             sr[1] = c[0];
             sr[2] = c[1];
             rc = 2;
         }
         sr[3] = '\0';
         s = 0;
         switch (sr[2]) {
             case 'S': s++;
             case 'H': s++;
             case 'D': s++;
             case 'C': s++;
         }
         r = 0;
         while (rr[r] != '\0' && c[0] != rr[r])
             r++;
         r += 11;

         return rc;
     }
     void disp() const {cout << r << s << ' ';}
     void show() const {cout << sr << ' ';}
     int  rn() const {return r;}
 };

 struct Hand {
 private:
     int n;
     Card c[NC];
 public:
     void set(const char* s) {
         int i, len = strlen(s);

         n = 0;
         i = 0;
         while (i < len)
             i += c[n++].set(&s[i]);
         show();
     }
     void disp() const {
         for (int i = 0; i < n; i++)
             c[i].disp();
         cout << endl;
     }
     void show() const {
         for (int i = 0; i < n; i++)
             c[i].show();
         cout << endl;
     }
     void sort() {
         Card temp;
         for (int i = n - 1; i >= 0; i--)
             for (int j = 0; j < i; j++)
                 if (c[j].rn() > c[j+1].rn()) {
                     temp = c[j];
                     c[j] = c[j+1];
                     c[j+1] = temp;
                 }
     }
 };

 int main() {
     Hand h;

     h.set("KD6S10C");
     h.disp();
     h.sort();
     h.disp();
     h.show();

     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