A solution to Problem D (5 point)


Good things
  • The use of assert(). This is the greatest command you can ever use. Use it lots. It will become your friend. :-)
  • Simple structures are great. Here we make a little structure to make life a little easier.
  • Some C++ guns may ask why I didn't do:
    		       char f_vals[]={"HDSC"};
            
    I personally don't like going that. Documentation isn't just comments, it's easy to read code and asserts. Think of this as an assert(). If I use [MAXFACES+1] and accidently assign {"_HDSC"}, the compiler will pick up the extra value.
  • Not so good things
  • The usual...

  • #include < iostream.h >
    #include < strstream.h >
    #include < fstream.h >
    #include < assert.h >
    
    #define MAXLEN (2+1)
    #define MAXFACES 4
    #define MAXPLACES 13
    
    struct Card {
      char f,p;
    };
    
    Card cards[MAXPLACES*MAXFACES];
    char f_vals[MAXFACES+1]={"HDSC"};
    char p_vals[MAXPLACES+1]={"23456789TJQKA"};
    
    void main(void)
    {
      int pos;
    
      ifstream infile("PROBLEMD.DAT", ios::in);
      assert(infile);
    
      pos=0;
      for(int i=0;i < MAXFACES;i++)
        for(int j=0;j < MAXPLACES;j++){
          cards[pos].f=f_vals[i];
          cards[pos].p=p_vals[j];
          pos++;
        }
    
      char incard[MAXLEN];
      while(infile >> incard){
        for(int loc=0; loc < pos ; loc++)
          if(cards[loc].f==incard[0] && cards[loc].p==incard[1])
    	cards[loc].f=0;
      };
    
      for(int i=0;i < pos;i++)
        if(cards[i].f)
          cout << cards[i].f << cards[i].p << endl;
    }