Codes of lecture 3 - Source Code files


/****************************************************************
*                                                               *
*                         associations.cc                       *
*                                                               *
****************************************************************/

#include "associations.h"

void Country::add_capital(City *a_c)
{
  if (_update_in_progress) return;    // Break mutual recursion on updates
    
  if(has_capital != NULL)
    {
      remove_capital();               // Remove current capital since the
                                      // the relationship is One-to-One
    }

  _update_in_progress = 1;            // Update this end of Association
  has_capital = a_c;

  has_capital->add_country(this);     // Update the other end of Association
  _update_in_progress = 0;
}

void Country::remove_capital()
{
  if (_update_in_progress) return;    // Break mutual recursion on updates
    
  _update_in_progress = 1;            // Update the other end of Association
  if(has_capital != NULL)
    {
      has_capital->remove_country();  
    }

  has_capital = NULL;                 // Update this end of Association
  _update_in_progress = 0;
}

void City::add_country(Country *a_c)
{
  if (_update_in_progress) return;    // Break mutual recursion on updates
    
  if(is_capital != NULL)
    {
      remove_country();               // Remove current capital since the
                                      // the relationship is One-to-One
    }

  _update_in_progress = 1;            // Update this end of Association
  is_capital = a_c;

  is_capital->add_capital(this);     // Update the other end of Association
  _update_in_progress = 0;
}

void City::remove_country()
{
  if (_update_in_progress) return;    // Break mutual recursion on updates
    
  _update_in_progress = 1;            // Update the other end of Association
  if(is_capital != NULL)
    {
      is_capital->remove_capital();  
    }

  is_capital = NULL;                 // Update this end of Association
  _update_in_progress = 0;
}

void Line::add_points(PointList *a_c)
{
  if (intersects == NULL)
  intersects = new PointList();
  
  Point_Link *temp = a_c->get_value();

  while(temp != NULL)
    {
      add_point(temp->get_value());
      temp = temp->get_next();
    }
}

void Line::add_point(Point *a_c)
{
  if (intersects == NULL)
  intersects = new PointList();
  
  if (_update_in_progress) return;    // Break mutual recursion on updates
    
  _update_in_progress = 1;            // Update this end of Association

  int i = a_c->add_line(this);        // Update the other end of Association

  if(i)
    intersects->add(a_c);             // Update this end of Association

  _update_in_progress = 0;
}

void Line::add_point(Point *a_c, Line *a_l)
{
  if (_update_in_progress) return;    // Break mutual recursion on updates
    
  _update_in_progress = 1;            
  int i = a_c->add_line(this, a_l);   // Update the other end of Association

  if(i)
    intersects->add(a_c);             // Update this end of Association

   _update_in_progress = 0;
}

void Line::remove_points(PointList *a_c)
{
  
  Point_Link *temp = a_c->get_value();

  while(temp != NULL)
    {
      remove_point(temp->get_value());
      temp = temp->get_next();
    }
}

void Line::remove_point(Point *a_c)
{
  if (_update_in_progress) return;    // Break mutual recursion on updates
    
  _update_in_progress = 1;            // Update this end of Association
  intersects->remove(a_c);

  a_c->remove_line(this);                // Update the other end of Association
  _update_in_progress = 0;
}

void Line::print_intersections()
{
  intersects->print();
}

void Point::add_lines(LineList *a_c)
{
  if(a_c->no_elements() < 2) 
    {
      cout << "You need at least 2 lines." << endl;
      return ;
    }
  if (is_intersection == NULL)
  is_intersection = new LineList();
  
  Line_Link *temp = a_c->get_value();

  while(temp != NULL)
    {
      add_line(temp->get_value());
      temp = temp->get_next();
    }
}

int Point::add_line(Line *a_c)
{
  if (no_lines < 2) 
    {
      cout << "You need to add a least two lines through a LineList." << endl;
      return 0;
    }
  if (_update_in_progress) return 1;    // Break mutual recursion on updates
    
  _update_in_progress = 1;            // Update this end of Association
  is_intersection->add(a_c);

  a_c->add_point(this);                // Update the other end of Association
  _update_in_progress = 0;
  ++no_lines;
  return 1;
}

int Point::add_line(Line *a_c, Line *a_l)
{
  if (a_c == NULL || a_l == NULL) 
    {
      cout << "You need to add a least two lines through a LineList." << endl;
      return 0;
    }
  if (_update_in_progress) return 1;    // Break mutual recursion on updates
    
  _update_in_progress = 1;            // Update this end of Association

  if (is_intersection == NULL)
  is_intersection = new LineList();
  
  is_intersection->add(a_c);
  ++no_lines;
  is_intersection->add(a_l);
  ++no_lines;

  a_c->add_point(this);                // Update the other end of Association
  a_l->add_point(this); 
  _update_in_progress = 0;
  return 1;
}

void Point::remove_lines(LineList *a_c)
{
  if(no_lines - a_c->no_elements() < 2)
    {
      cout << "You have to leave at least 2 lines or remove all." << endl;
      return;
    }
  Line_Link *temp = a_c->get_value();
  
  while(temp != NULL)
    {
      remove_line(temp->get_value());
      temp = temp->get_next();
    }
}

void Point::remove_line(Line *a_c)
{
  if(no_lines - 1  < 2)
    {
      cout << "You have to leave at least 2 lines or remove all" << 
	" with remove_lines()." << endl;
      return;
    }
  if (_update_in_progress) return;    // Break mutual recursion on updates
    
  _update_in_progress = 1;            // Update this end of Association
  is_intersection->remove(a_c);

  a_c->remove_point(this);                // Update the other end of Association
  _update_in_progress = 0;
  --no_lines;
}

void Point::print_intersections()
{
  is_intersection->print();
}

void Season::add_links()
{
  pitcher->set_season(this);     // Update the Association
  team->set_season(this);
  year->set_season(this);
}

void Season::remove_links()
{
  pitcher->set_season(NULL);     // Update the Association
  team->set_season(NULL);
  year->set_season(NULL);
}

main()
{
  Country *can = new Country("Canada");
  City    *ota = new City("Otawa", can);

  cout << can->get_name() << " has capital " << can->get_capital()->get_name() 
    << endl;
   
  Pitcher *har = new Pitcher("Harry Eisenstat");
  Team *tem = new Team("Cleveland Indians");
  Year *ye = new Year("1939");
  
  har->add_season(tem, ye);
  
  cout << har->get_name() << " Play for the " << 
    har->get_season()->get_team()->get_name() <<
      " in the " << har->get_season()->get_year()->get_name() << " season." << endl;
  
  Line *L1 = new Line("L1"); 
  Line *L2 = new Line("L2");

  Point *p1 = new Point("P1", L1, L2);

  cout << "Point " << p1->get_name() << " is the intersection of:" << endl;
  p1->print_intersections();

  Line *L3 = new Line("L3", p1);

  cout << "Point " << p1->get_name() << " is the intersection of:" << endl;
  p1->print_intersections();

  Point *p2 = new Point("P2");

  Line *L4 = new Line("L4", p2, L1);

  cout << "Line " << L1->get_name() << " is intersected in:" << endl;
  L1->print_intersections();

  p1->remove_line(L1);

  cout << "Line " << L1->get_name() << " is intersected in:" << endl;
  L1->print_intersections();

  cout << "Point " << p1->get_name() << " is the intersection of:" << endl;
  p1->print_intersections();

  p1->remove_line(L2);

}


/**********************************************************************
*                                                                     *
*                          lists.cc                                   *
*                                                                     *
**********************************************************************/

#include
#include
#include"lists.h"
#include"associations.h"

void PointList::add(Point *a_p)
{
  if (head == NULL)
    {
      head = new Point_Link();
      head->put_value(a_p);
    }
  else
    {
     Point_Link *temp = head;
     while ( temp->get_next() != NULL)
       {
	 temp = temp->get_next();
       }
     Point_Link *temp2 = new Point_Link(temp);
     temp2->put_value(a_p);
     temp->set_next(temp2);
   }
}

void PointList::remove(Point *a_p)
{
  if (head == NULL)
       cerr << "There is no point in the list." << endl;
  else
    {
      Point_Link *temp = head;
      while ( temp != NULL && temp->get_value() != a_p)
	{
	  temp = temp->get_next();
	}
      if(temp == NULL)
	cerr << "Point " << a_p->get_name() << 
	  " is not associated with this line." << endl;
      else 
	{
	  if (temp == head)
	    {
	      Point_Link *t2 = temp->get_next();
	      head = t2;
	      t2->set_previous(NULL);
	    }	      
	  else
	    {
	      Point_Link *t1 = temp->get_previous();
	      Point_Link *t2 = temp->get_next();
	      t1->set_next(t2);
	      t2->set_previous(t1);
	    }
	  delete temp;
	}
    }
}

void PointList::print()
{
  if (head == NULL)
       cerr << "There is no Line in the list." << endl;
  else
    {
      Point_Link *temp = head;
      while ( temp != NULL)
	{
	  cout << temp->get_value()->get_name() << endl;
	  temp = temp->get_next();
	}
    }
}

void LineList::add(Line *a_p)    // Do you see how much repetions there are
{                                // between the two lists.
  if (head == NULL)
    {
      head = new Line_Link();
      head->put_value(a_p);
      number++;
    }
  else
    {
     Line_Link *temp = head;
     while ( temp->get_next() != NULL)
       {
	 temp = temp->get_next();
       }
     Line_Link *temp2 = new Line_Link(temp);
     temp2->put_value(a_p);
     temp->set_next(temp2);
     number++;
   }
}

void LineList::remove(Line *a_p)
{
  if (head == NULL)
       cerr << "There is no Line in the list." << endl;
  else
    {
      Line_Link *temp = head;
      while ( temp != NULL && temp->get_value() != a_p)
	{
	  temp = temp->get_next();
	}
      if(temp == NULL)
	cerr << "Line " << a_p->get_name() << 
	  " is not associated with this line." << endl;
      else 
	{
	  if (temp == head)
	    {
	      Line_Link *t2 = temp->get_next();
	      head = t2;
	      t2->set_previous(NULL);
	    }	      
	  else
	    {
	      Line_Link *t1 = temp->get_previous();
	      Line_Link *t2 = temp->get_next();
	      t1->set_next(t2);
	      t2->set_previous(t1);
	    }
	  delete temp;
	  number--;
	}
    }
}

void LineList::print()
{
  if (head == NULL)
       cerr << "There is no Line in the list." << endl;
  else
    {
      Line_Link *temp = head;
      while ( temp != NULL)
	{
	  cout << temp->get_value()->get_name() << endl;
	  temp = temp->get_next();
	}
    }
}