/*****************************************************************************
 * FILE: stats.cpp                                                           *
 * CLASS PROVIDED: statistician (part of namespace CS256_Asgn_2)             *
 *    See stat.h for documentation                                           *
 *                                                                           *
 *****************************************************************************/
#include <cassert>
#include "stats.h"
#include <cstdlib>
using namespace std;

namespace CS256_Asgn_2
{
   //CONSTRUCTOR
	statistician::statistician()
	{
	   amount=0;
	   tot=0;
	   small=0;
	   large=0;
	}
	
	//MODIFICATION MEMBER FUNCTIONS
	void statistician::reset()
	{
	   amount=0;
	   small=0;
	   large=0;
	   tot=0;
	}
	void statistician::next(double r)
	{
	   amount++;
	   if( amount == 1)
	   {
	   	small=r;
	   	large=r;
	   	tot+=r;
	   }
	   else
	   {
	   		
	   	if(r < small) small=r;
	   	if(r > large) large=r;
	   	tot+=r;
	   }	
	}
	
	//CONSTANT MEMBER FUNCTIONS
	double statistician::mean() const
	{
	   assert( amount > 0);
	   return tot/amount;
	}
	
	int statistician::length() const
	{
	   return amount;
	}
	
	double statistician::minimum() const
	{
	   assert( amount > 0);
	   return small;
	}
	
	double statistician::maximum() const
	{
	   return large;
	}
	
	double statistician::sum() const
	{
	   return tot;
	}
	
	
	
	//OPERATORS
	statistician operator +(const statistician& s, 
                                  const statistician& t)
   {
      statistician k;
      k.amount = s.length() + t.length();
      k.tot = s.sum() + t.sum();   		
   
      if( s.length() < 1 || t.length() < 1)
      {
         k.large=0;
         k.small=0;
         return k;
      }
      else
      {
         
         //k.large = s.maximum() + t.maximum();
         //k.small = s.minimum() + t.minimum();
         if (s.maximum() > t.maximum())
         {   
            k.large = s.maximum();
         }   
         else
         {
            k.large = t.maximum();
         }
         if (s.minimum() > t.minimum())
   	   {
   	     	k.small = s.minimum();
   	   }
   	   else 
   	   {
   	     	k.small = t.minimum();
   	   }
   	   
   	   
   	   return k;
	   }	                    
   }
   
   statistician operator *(double scale, const statistician& s)
   {
   	statistician k;
   	if(s.length() > 0)  
      {
        k.large = scale*s.maximum();
   	  k.small = scale*s.minimum();
   	}
   	else
   	{
   		k.large=0;
   		k.small=0;
   	}	
      k.tot = scale*s.sum();
   	k.amount = scale*s.length(); 
   	return k;
   }
   
   bool operator ==(const statistician& s, const statistician& t)
   {
   	if(s.length() != t.length()) return 0;
   	if( s.length() < 1 && t.length() < 1)
        {
         
        }
        else
        {

           if(s.maximum() != t.maximum()) return 0;
           if(s.minimum() != t.minimum()) return 0;
           if(s.sum()!=t.sum()) return 0;
           return 1;
        }	  
        if(s.sum()!=t.sum()) return 0;
        return 1;
   }		                     
 }     

