UNIT Package;

{------------------------------------------------------------------------------------}
{This Unit is designed by:	         Maysa-Maria Peterson
 Date:				October, 7 1997
 Contents: All the functions defined and implemented by the class are placed
 	here as a unit to demonstrate software reuse. The intent is to build
 	a library of functions and procedures for the team projects.       }
{------------------------------------------------------------------------------------}

 INTERFACE
 {Interface Part}


CONST
      Low = 1;

TYPE
      List = array[1..1000] of integer;
      LargeList = array[1..10000] of integer;
      RealList = array [1..10000] of real;
      HexString = String[4];
short_string = string[100];
long_string = string[201];
min_flag = (mins, hour);
temp_flag = (C, F);
weight_flag = (Gr, Lb);


 Function Min(L: LargeList; LE: Integer): Integer;
	{---------------------------------------------------------------------------}
	{PRE:	L is a list of values, in the form of an array                
       	        of 1..10000 integers LE is an integer defining the length of the array. 
	        The LE + 1 element of the array may/may not hold a flag variable     
             	 PROCESS:  The array is traversed until the minimum value in the 
 	           list is found. That value is returned. 
	 POST:	The minimum value in the list is returned                      }
	{--------------------------------------------------------------------------}


 Function Upper(Var LetterIn : Char) : Char;
	{--------------------------------------------------------------------------}
	{PRE:     Must have a lower case character                        
	 PROCESS: Takes the ordinal of the character, substracts         
	          32 then outputs the resulting character.               
	 POST: Outputs a UPPER case character.                                     }
	{--------------------------------------------------------------------------}


 Function Net(I: Real; x: Integer; num: RealList): Real;
	{--------------------------------------------------------------------------}
	{PRE:	The accepts as input: the interest rate as a real         
	 	an integer for the length of the array, 
	 	and an array of values. 
	 PROCESS:  Sums the amounts in dollars and then               
	               calculates the interest accrued on that sum                  
	 POST:	Return the new total amount with interest                     }
	{--------------------------------------------------------------------------}


 Function Evenodd(x: Integer): Boolean;
	{--------------------------------------------------------------------------}
	{PRE:      An integer number is given                                       
	 PROCESS:  Check if the number is even or odd
	 POST:	  Returns true if the number is even, false if it is odd    }
	{--------------------------------------------------------------------------}


 Function Repstr(Var astring: string; times: integer): string;
	{--------------------------------------------------------------------------}
	{PRE:     Sends in a string up to 100 characters and the number of times 
		 to repeat the string as an integer number.
  	PROCESS: concatinates a string to itself "times" times
 	POST:    a new string with text repeated.                                      }
	{--------------------------------------------------------------------------}


 Function Factorial(x: longint):longint;
	{--------------------------------------------------------------------------}
       	{PRE:     Passes in an integer  (long)
         	  PROCESS: factorials the integer
         	  POST:   Passes out an integer  (long)                                          }
	{--------------------------------------------------------------------------}


 Function Simplint(Var money, interest: real; years: integer) :real;
	{--------------------------------------------------------------------------}
	{PRE: 	The interest rate, the initial amount, and the number of
		year is given. No negative values allowed.
	 PROCESS:     The simple interest is calculated over time.
	 POST:	The new amount with interest is returned                     }
         	{-------------------------------------------------------------------------}


 Function Power (x: real; n: integer):real;
	{--------------------------------------------------------------------------}
	{PRE: 	An real number is passed in with an integer for the power
	 PROCESS:	Calculates a number to a certain power
	 POST:	Return x to the n power                                                   }
	{--------------------------------------------------------------------------}


Function Media(x:list; le:integer) :real;
 	{--------------------------------------------------------------------------}
	{PRE:    An array of up to 1000 integers , and an integer for
		identifying where the end of the array is.
	 PROCESS:	Calculates the median
	 POST:	Returns the median                                                          }
	{--------------------------------------------------------------------------}


 Function ConvHexToDec(var h: HexString) :longint;
	{--------------------------------------------------------------------------}
	{PRE:    Accepts a string of  up to length 4, which is a hexidecimal
		number 
	 PROCESS:	This function does the actual
 	 		converting from hexadecimal to decimal, by reading 
			a string from the screen and converting the integer, 
			digit by digit
	 POST:	Returns the decimal value                                               }
	{--------------------------------------------------------------------------}
	

Function GramsPounds (X : real; ConvertTo:weight_flag):real;
	{--------------------------------------------------------------------------}
               {PRE: an amount is entered must be > 0, and the flag indicates
		whether to convert from "C" to "F" or vice versa and the 
		values of the flag are in the set: [C,F]              
                PROCESS: Takes pounds and converts to grams and vice versa 
	 POST:					 		}
	{--------------------------------------------------------------------------}


Function Cat (s1, s2: short_string): long_string;
	{--------------------------------------------------------------------------}
	{PRE: Gets 2 sentences                                                                  
	  PROCESS: Concatinates the sentences                                      
	  POST: Sends the sentences back                                                  }
	{--------------------------------------------------------------------------}


Function Paragraph(s1, s2: short_string): long_string;
	{--------------------------------------------------------------------------}
	{  PRE: Gets 2 sentences
	  PROCESS: Concatinates the sentences together with a space in between 
	  POST: Sends the sentences back                                                 }
	{--------------------------------------------------------------------------}


Function Min_Hours (x: real; ConvertTo: min_flag) : real;
	{--------------------------------------------------------------------------}
 	{PRE: a number is entered, and the flag is set to convert to 
		"mins" or "hour"
 	  PROCESS: Converts Hours into minutes  and vice versa          
 	  POST:   The converted time is returned			 }
	{--------------------------------------------------------------------------}


Function Combinations(n, c: integer) :integer;
	{--------------------------------------------------------------------------}
 	{PRE:  two integers are given for computing the combination
 	  PROCESS: Find the combination of n total with c items selected
 	  POST:  the combination is returned, order is important             }
	{--------------------------------------------------------------------------}


Function Permutations(n, p: integer) :integer;
	{--------------------------------------------------------------------------}
 	{PRE: two integers are given for computing the permutation   
 	  PROCESS: Find the permutation of n total with p items selected
 	  POST: the permutation is returned, order is not important        }
	{--------------------------------------------------------------------------}


Function Temperature(x: integer; ConvertTo: temp_flag): integer;
	{--------------------------------------------------------------------------}
 	{PRE: a temperature is entered, and the convert_to flag is either 
		"F" or "C"
 	  PROCESS: Converts centigrade to Fahrenheit  and vice versa
 	  POST:   The new temperature is returned                                   }
	{--------------------------------------------------------------------------}


Function Max(L: LargeList; LE: Integer): Integer;
	{--------------------------------------------------------------------------}
 	{PRE:   An array of integers and the end of the array is given
 	  PROCESS: Find the maximum value of integers in a list no
		 larger than 10000 
 	  POST:  The maximum number in the list is returned                 }
	{--------------------------------------------------------------------------}




 IMPLEMENTATION
 {Implementation Part}

 FUNCTION Min(L: LargeList; LE: Integer): Integer;
 {Find the minimum value of integers in a list no larger than 10000}
   VAR
      Ans, i: Integer;
   BEGIN{Function Min }

      	Ans:= MAXINT;
      	FOR  i:= 1 TO LE DO    If L[i] < Ans Then Ans:= L[i];
      	Min:= Ans;

   END;{Function Min}


 FUNCTION Upper(Var LetterIn : Char) : Char;
 {Convert lowercase letters into uppercase}
   BEGIN {Upper}

       	LetterIn :=Char(Ord(LetterIn) - 32); 

   END; {Upper}


 FUNCTION Net(I: Real; x: Integer; num: RealList): Real;
 {accepts a list of real numbers, interest, and years
  and returns the new gross amount with interest}
    VAR
  	Sum: Real;
  	y: Integer;
    BEGIN  {function}

         Sum :=0;
         FOR y := 0 TO x DO  Sum := Sum +num[y];
         Sum := Sum * I + Sum;
         Net := Sum;

    END; {function net}


 FUNCTION Evenodd(x: Integer): Boolean;
 {Function returns true if the number is even and false if it is odd}
    VAR 
    	n: Real; 	{variable is real, but turns into a integer when it passes through truncation}

    BEGIN {begin Evenodd}
    	n:= x / 2;
    	n:=trunc(n);
    	IF n = (x / 2) THEN     {this shows that there is no remainder}
                     Evenodd:= true     {so it has to be even, because even numbers}
      	ELSE                    {divide by 2 evenly with no remainder}
      	     Evenodd:= false;   {if there was a remaider, n would not = x/2}

   END; {end Evenodd}


 FUNCTION Repstr(Var astring: string; times: integer): string;
 {Takes in a string of up to 80 characters and the number of times to repeat it}
 {and then appends the string to itself that many times }
     VAR
        i: integer;

     BEGIN {function repstr}
	FOR i:= 1 to times DO astring:= astring + astring;
        repstr:=  astring;

     END; {function repstr}


 FUNCTION Factorial(x: longint):longint;
 {Takes in an integer and calculates the factorial}
    VAR
       temp: longint;
       i:    integer;

    BEGIN  {function factorial}
      temp:= x;
      IF temp > 16 THEN  Factorial:= 0
      ELSE
           BEGIN
     	CASE temp OF
         	0:     Factorial:= 1;
         	1:     Factorial:= 1;
         	ELSE
	     BEGIN
     		FOR i:= x DOWNTO 2 DO
         		BEGIN
           		   temp:= temp * (x-1);
      		           x:=x-1;
         		END;  {for loop}
     		Factorial:= temp;
                END;  {else clause}

             END; {case statement}

         END;  {else clause for factorial <= 15}

  END; {function factorial}


 FUNCTION Simplint(Var money, interest: real; years: integer) :real;
 {compounding the simple interest, given the rate, number of years, and amount}
      VAR
	i: integer;  

      BEGIN  {function simplint}
              FOR i:= 1 TO years DO  money:= money * (1 + interest); 
              simplint:= money;

      END; {function simplint}


 FUNCTION Power (x: real; n: integer):real;
 {calculates a number to a certain power}
    VAR
         i: integer;
         Product: real;

    BEGIN{power function}
         Product:= 1;
         FOR i:= n DOWNTO 1 DO  Product:=Product * x;
         Power:= Product;

    END; {function power}


 FUNCTION Media(x:list; le:integer) :real;
 {accepts an array of up to 1000 integers, and the last item index and returns the median}
     VAR
           Num1, Num2: real;

   Begin
         IF Odd(Le) THEN
      	BEGIN
      	      Le:=(Le div 2)+1;
      	      Media:=x[Le];
     	END {if clause}
         ELSE
       	BEGIN
       	     Num1:=x[Le div 2];
                    Num2:=x[(Le div 2)+1];
                    Media:=(Num1+Num2)/2;
              END;{else clause}

   end;{Median}


 FUNCTION ConvHexToDec(var h: HexString) :longint;
 {This function does the actual
  converting from hexadecimal to decimal,
  by reading a string from the screen
  and converting the integer, digit by digit.}
    VAR
       numbers: string[22];
       num: string[1];
       NumDigits: integer;
       ans: longint;
       i,digit: integer;

    BEGIN
      numbers:='0123456789ABCDEFabcdef';
      NumDigits:=length(h);
      ans:=0;

      FOR  i:=1 TO NumDigits DO
            BEGIN      {Copies the next digit into position to be converted.}
      	   num:=copy(h,i,1);
           digit:=pos(num,numbers);
             {Verifies that input is a legitimate hexadecimal integer.}
                  IF digit=0 THEN
        	           BEGIN
          		        write('Error: your digit is not within the definition of');
         		        writeln(' hexadecimal.');
          		        halt;
        	          END;

       	 IF digit>15 THEN
         {Gives equal value to small and capital letters "a"-"f" or "A"-"F".}
          	        digit:=digit-6;
        	        ans:=ans*16+digit-1; 
        
                       {Totals the individually converted digit values.
                        Uses "ans*16" to give each position's power of 16. Uses "digit-1"
                        for assigning values to positions within the numbers string because
                        the list starts with "0", not "1".}
            END;   {for loop end}

  ConvHexToDec:=ans;

END;  {ConvHexToDec}


FUNCTION GramsPounds (X : real; ConvertTo:weight_flag):real;
 {Takes pounds and converts to grams}
 {Precondition: Pounds entered must be > 0}
   CONST
         G = 453.6;
    BEGIN (*Grams*)
          IF ConvertTo = Gr THEN GramsPounds := X * G
          ELSE  GramsPounds := X / G;

    END;(*Grams*)


FUNCTION Cat (s1, s2: short_string): long_string;
 {Pre: Gets 2 sentences}
 {Process: Concatinates the sentences}
 {Post: Sends the sentences back}
  BEGIN
	cat:= s1 + s2; {makes the final sentence}

  END; 


FUNCTION Paragraph(s1, s2: short_string): long_string;
 {Pre: Gets 2 sentences}
 {Process: Concatinates the sentences together with a space in between}
 {Post: Sends the sentences back}
  CONST
	space = ' ';
  BEGIN
	paragraph:= s1 + space +  s2; {makes the final sentence}

  END; 


FUNCTION Min_Hours (x: real; ConvertTo: min_flag) : real;
 {PROCESS: Converts Hours into minutes  and vice versa}
     BEGIN
     	IF ConvertTo = mins THEN  Min_Hours:= x * 60 
    	ELSE  Min_Hours:= x / 60;

     END;



FUNCTION Combinations(n, c: integer) :integer;
 {finds the combination of c items out of n total}
     BEGIN
	Combinations:= Factorial(n) DIV (Factorial(c) * Factorial(n-c) );

     END;


FUNCTION Permutations(n, p: integer) :integer;
 {finds the permutations of p items out of n total}
     BEGIN
	Permutations:= Factorial(n) DIV Factorial(p);

     END;


FUNCTION Temperature(x: integer; ConvertTo: temp_flag): integer;
 {converts temperature from/to centigrade and fahrenheit}
     BEGIN
	IF ConvertTo = F THEN   Temperature:= x * 9 DIV 5 + 32
	ELSE Temperature:=  x * 5 DIV 9 - 32;

     END;{Temperature}


FUNCTION Max(L: LargeList; LE: Integer): Integer;
 {Find the maximum value of integers in a list no larger than 10000}
     VAR
      Ans, i: Integer;
     BEGIN{Function Min }
      	 Ans:= -(MAXINT);
      	 FOR  i:= 1 TO LE DO    If L[i] > Ans Then Ans:= L[i];
      	 Max:= Ans;

     END;{Function Min}


END. {package}


