CSE 109   Test 1  Wednesday 19 February 2003
===================SUGGESTED ANSWERS====================================
1. Consider an ADT, Tally, for tallying votes for a referendum.  It can
record a yes or a no and returns the number of yeses and noes.  Further, one
can add two to get a third Tally which has the sums of the yeses and noes
from the first two.  Write the declaration and the definitions (code) for a
class Tally, which implements this ADT and for which the following code will
compile and behave "reasonably."
Tally a,b,c;
 a.yes(); a.yes(); b.no(); a.no();
 c=a+b;
 cout<<"Noes: "<<c.noes()<<", yeses: "<<c.yeses()<<endl;
========================================================================
#include <fstream.h>
class Tally
{public:
  Tally();
  void yes();
  void no();
  int yeses();
  int noes();
  Tally operator +(const Tally &t);
 protected:
   int yesCt,noCt;
};

Tally::Tally():yesCt(0),noCt(0)
{}

void Tally::yes(){yesCt++;}

void Tally::no(){noCt++;}

int Tally::noes(){return noCt;}

int Tally::yeses(){return yesCt;}

Tally Tally::operator+(const Tally &t)
{Tally temp;
 temp.yesCt=yesCt+t.yesCt;
 temp.noCt=noCt+t.noCt;
 return temp;
}

2.  Write the declaration and definitions for a subclass of Tally, BTally,
which adds the capacity to record and display abstentions and to have the
three counters (yes, no, abstain) reset to 0.  The following code should
compile and behave reasonably.
 BTally x;
 x.yes(); x.abstain();
 cout<<"Y: "<<x.yeses()<<", N: "<<x.noes()<<", ABST: "<<x.abstentions()<<endl;
===========================================================================

class BTally:public Tally
{public:
  BTally();
  void abstain();
  int abstentions();
 private:
  int abstCt;
};

BTally::BTally():abstCt(0){}

void BTally::abstain(){abstCt++;}

int BTally::abstentions(){return abstCt;}

3.  Write a program which displays the first character of each file specified
on the command line when the program is called, provided the file exists.
For example, if the program is stored in 'prog' and files a, b, and c exist,
while d does not, the command
  prog a b c d
would list the first character in files a, b, and c.
=====================================================================
#include <fstream.h>
int main(int ct,char **arg)
{ifstream f;
 for(int j=1;j<ct;j++)
  {f.open(arg[j]);
   cout<<"Checking file '"<<arg[j]<<"' ";
   if(f.good())
     cout<<"'"<<char(f.get())<<"'";
   cout<<endl;
   f.close();
  }
}

4.  Below is part of the declaration for the class BinaryT.  Write the
declaration and definition (code) for the member function of BinaryT called
size() which returns the number of nodes in the binary tree.  Below the
declaration  of BinaryT is an example of how size() would be called.
#include <fstream.h>
class BinaryT
{private:
  class Node
   {public:
     Node(double x=0);
     double key;
     Node * child[2];
   };
  Node *root;
  static void assert(bool b,char*mess);
  static const int LEFT=0,RIGHT=1;
  .....
  .....
};

BinaryT x;
 for(int j=0; j<10; j++)
   x.add(j-5);
 cout<<"The size of the tree is "<<x.size()<<endl;
===============================================================
  int size(Node *rt);

  int size();


int BinaryT::size(){return size(root);}

int BinaryT::size(Node *rt)
{if(rt==NULL)
  return 0;
 else
  return 1+size(rt->child[LEFT])+size(rt->child[RIGHT]);
}
