// balint.cpp Implementation of LimitInt functions /******************************************************************************* // MODULE NAME: LimitInt // INTERFACE FILE: balint.h // IMPLEMENTATION FILE: balint.cpp // // PURPOSE: To provide an int type useful for the BAL program which can store // int values, printing width, and logical limits upon those ints. To // allow the client to store new values or new limits, to test the validity of // a stored value against the current limits, to report on the current value of // the int, to print in a formatted string the current value of the int, to // print the default value for the int, & to reset the value or limits or both // of the int // // FUNCTIONS: Name Purpose // LimitInt Parameterless constructor // LimitInt Constructor w/specified field width // LimitInt Constructur w/ field width, limit type, limit // value // LimitInt Constructor w/ field width, 2 limit types, 2 // limit values // isValid Indicate whether the value stored in the // LimitInt conforms to the limits on the object // isDefault Inidicate whether the default value for that // object is currently stored in the object // LessThan Test whether this object ought to precede // another of the same class // Equals Test whether this object has the same value as // another of the same class // GreaterThan Test whether this object ought to follow another // of the same class // Print Create a string with the appropriate width that // contains the current value of the object // Default Create a string with the appropriate width that // contains the default value for the object // valueIs Return the value currently stored in the object // Limits Create a string that tells what the contstraints // are on this object // setLimit Sets one limit to user-specified values, and the // other limit to the default (NO_LIMIT) // setLimit Set both limits and limit types to user- // specified values // SetSize Change the printing width of the object // Store Store a new int value into the value // ResetValue Change the current value of the object to the // default value for this object // ResetLimits Change the limits on this object to the default // limits (NO_LIMIT) for this object // Reset Change the value and limits of this object to // their default values // // // AUTHOR: Amy Langill *2B Date: 3/20/1999 *******************************************************************************/ #include "balint.h" /********************************CONSTRUCTORS**********************************/ /******************************************************************************* // FUNCTION NAME: LimitInt // // DESCRIPTION OF FUNCTION: Paramterless constructor // // DESCRIPTION OF ALGORITHM: Assigns default limits, limit types, & field size. // Based on field size, creates a default value and stores it into this->value // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: Object has been created with default limit types & values, // default field size and a default value appropriate for that field size. // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ LimitInt::LimitInt() { int radix=DECIMAL; // Base of the number. Used for storing default int counter; // used for count-controlled loop lowCompare=topCompare=DEFAULT_COMPARE; // Assign defaults lowerLimit=upperLimit=DEFAULT_INT_LIMIT; // size=DEFAULT_INT_SIZE; // value=0; // Initialize value before we use it for (counter=0;countervalue // // CALLED BY: client // CALLS: none // // PARAMETERS: / in / width - int that specifies the print size of the object // / in / comparison - LimitType that specifies what sort of // limit is being put on the value // / in / Limit - int that indicates magnitude of limit // // PRECONDITIONS: none // // POSTCONDITIONS: Object has been created w/default values for this->value & // 1 limit, and supplied values for size & other limit // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ LimitInt::LimitInt(LimitType comparison, int Limit, int width) { int radix=DECIMAL; // Base of the number. Used for storing default int counter; // used for count-controlled loop topCompare=DEFAULT_COMPARE; // Assign defaults upperLimit=DEFAULT_INT_LIMIT; // lowCompare=comparison; // Record user-supplied information lowerLimit=Limit; // size=width; // value=0; // Initialize value before we use it for (counter=0;counterisDefault()) // Don't error-check the default return intOK; switch (lowCompare) // check the value against the appropriate constraints for { // the first limit case NONE: break; // No limit == no problem case LESSTHAN: if (value>=lowerLimit) // value should be < lowerLimit intOK=false; break; case LESSTHANEQUALS: if (value>lowerLimit) // value should be <= lowerLimit intOK=false; break; case GREATERTHAN: if (value<=lowerLimit) // value should be > lowerLimit intOK=false; break; case GREATERTHANEQUALS: if (value= lowerLimit intOK=false; break; } if (!intOK) // If it didn't fit the first limit, there's no need return intOK; // to check the second one switch (topCompare) // check to see if value has appropriate relationship with { // the second limit case NONE: break; // No limit == no problem case LESSTHAN: if (value>=upperLimit) // value s/b < upperLimit intOK=false; break; case LESSTHANEQUALS: if (value>upperLimit) // value s/b <= upperLimit intOK=false; break; case GREATERTHAN: if (value<=upperLimit) // value s/b > upperLimit intOK=false; break; case GREATERTHANEQUALS: if (value= upperLimit intOK=false; break; } return intOK; // report whether the int fit its limits } /******************************************************************************* // FUNCTION NAME: isDefault // // DESCRIPTION OF FUNCTION: Test whetherthe object contains the default value // for the object // // DESCRIPTION OF ALGORITHM: Creates a default value for this object based on // the field size then compares it against the current value. // // CALLED BY: isValid, GreaterThan, LessThan, Equals, client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: If this->value is exactly equal to the value of the default // object, returns true. Otherwise, returns false. // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ bool LimitInt::isDefault() { int checkInt=0; // used to compare to value to see if value is the default int radix=DECIMAL; // radix used for assigning default value int counter; // used for count-controlled loop for (counter=0;counterisDefault())&&(another.isDefault())) return false; return (valueisDefault())&&(another.isDefault())) return false; return (value>another.value); } /******************************************************************************* // FUNCTION NAME: Equals // // DESCRIPTION OF FUNCTION: Indicate whether the value of the object is equal // to the value of another object of the same class // // DESCRIPTION OF ALGORITHM: If both objects contain default values, returns // true. If not, directly compares the values of the two objects for exact // equality // // CALLED BY: client // CALLS: isDefault // // PARAMETERS: / in / another - LimitInt this one is being compared to // // PRECONDITIONS: none // // POSTCONDITIONS: If both objects contain default values, or the values of // both objects are exactly equal, returns true. Returns false otherwise // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ bool LimitInt::Equals(LimitInt another) { if ((this->isDefault())&&(another.isDefault())) return true; return (value==another.value); } /******************************************************************************* // FUNCTION NAME: valueIs // // DESCRIPTION OF FUNCTION: Outputs the current int stored in the object // // DESCRIPTION OF ALGORITHM: Returns this->value // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: Returns the int currently stored in value // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ int LimitInt::valueIs() { return value; } /******************************************************************************* // FUNCTION NAME: Print // // DESCRIPTION OF FUNCTION: Output a string of the appropriate width which // contains the value stored in the object // // DESCRIPTION OF ALGORITHM: Using an output stringstream, converts the value // from an into to a string. If this value does not fit the width of the // field, the string is padded with enough spaces before the value to fill out // the width of the field. // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: A string of length this->size has been created which // contains at least the first size characters of this->value as a string // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ string LimitInt::Print() { ostringstream intString; string outputString=" "; // hold formatted string int counter; // for count-controlled loop intString << value; // convert int to string if ((intString.str()).length() chars of the string outputString = ((intString.str()).substr(0,size)); return outputString; // send the pretty string to the calling function } /******************************************************************************* // FUNCTION NAME: Default // // DESCRIPTION OF FUNCTION: Creates a string containing the default value for // this field of the appropriate width // // DESCRIPTION OF ALGORITHM: Creates an object of the same size with a default // value, then Prints it. // // CALLED BY: none // CALLS: LimitInt, Print // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: A string has been created of width this->size which contains // at least the first size characters of the default value for a LimitInt of // width this->size // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ string LimitInt::Default() { LimitInt defaultInt(size); return defaultInt.Print(); } /******************************************************************************* // FUNCTION NAME: Limits // // DESCRIPTION OF FUNCTION: Create a string that tells what the constraints are // on this object // // DESCRIPTION OF ALGORITHM: Depending on the type of limits defined in // lowCompare and topCompare, creates a string with a message indicating what // type of limits exist. // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: A string has been created which contains a message telling // what limits exist on the LimitInt // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ string LimitInt::Limits() { ostringstream limitstring; switch (lowCompare) { case NONE: limitstring << "No limit exists"; break; case LESSTHAN: limitstring << "Must be less than " << lowerLimit; break; case LESSTHANEQUALS: limitstring << "Must be less than or equal to " << lowerLimit; break; case GREATERTHAN: limitstring << "Must be greater than " << lowerLimit; break; case GREATERTHANEQUALS: limitstring << "Must be greater than or equal to " << lowerLimit; break; } switch (topCompare) { case NONE: break; case LESSTHAN: limitstring << '\n' << "Must be less than " << upperLimit; break; case LESSTHANEQUALS: limitstring << '\n' << "Must be less than or equal to " << upperLimit; break; case GREATERTHAN: limitstring << '\n' << "Must be greater than " << upperLimit; break; case GREATERTHANEQUALS: limitstring << '\n' << "Must be greater than or equal to " << upperLimit; break; } return limitstring.str(); } /*****************************TRANSFORMERS*************************************/ /******************************************************************************* // FUNCTION NAME: setLimit // // DESCRIPTION OF FUNCTION: Sets one limit to user-specified values and the // other limit to the default values. // // DESCRIPTION OF ALGORITHM: Assigns comparison to lowCompare, Limit to // lowerLimit, and default values to topCompare & upperLimit // // CALLED BY: client // CALLS: none // // PARAMETERS: / in / comparison - LimitType that indicates the relationship // between value and lowerLimit // / in / Limit - int magnitude of lowerLimit // // PRECONDITIONS: none // // POSTCONDITIONS: Lower limit of the LimitInt has been set according to the // user-supplied information and upper limit has been reset to its defaults // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ void LimitInt::setLimit(LimitType comparison, int Limit) { topCompare=DEFAULT_COMPARE; // Reset top limit to default upperLimit=DEFAULT_INT_LIMIT; // lowCompare=comparison; // Set lower limit with user-supplied info lowerLimit=Limit; // return; } /******************************************************************************* // FUNCTION NAME: setLimit // // DESCRIPTION OF FUNCTION: Sets both limits of the LimitInt to the user- // specified values. // // DESCRIPTION OF ALGORITHM: Assigns compare1 to lowCompare, Limit1 to // lowerLimit, compare2 to topCompare, Limit2 to upperLimit // // CALLED BY: client // CALLS: none // // PARAMETERS: / in / compare1 - LimitType that indicates the relationship // between value and lowerLimit // / in / Limit1 - int magnitude of lowerLimit // / in / compare2 - LimitType that indicates the relationship // between value and upperLimit // / in / Limit2 - int magnitude of upperLimit // // PRECONDITIONS: none // // POSTCONDITIONS: Both limits of the LimitInt have been set according to the // user-supplied information. // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ void LimitInt::setLimit(LimitType compare1,int Limit1,LimitType compare2, int Limit2) { lowCompare=compare1; // Record user-supplied limit information lowerLimit=Limit1; // topCompare=compare2; // upperLimit=Limit2; // return; } /******************************************************************************* // FUNCTION NAME: SetSize // // DESCRIPTION OF FUNCTION: Changes the printing width of the object // // DESCRIPTION OF ALGORITHM: If the object contains a default value, the // default is recalculated for the new field size. The new field width is // stored into this->size. // // CALLED BY: client // CALLS: none // // PARAMETERS: / in / width - int print size of the object // // PRECONDITIONS: // // POSTCONDITIONS: If the specified width is impossible (negative), the // value is changed to zero if it is a default and unchanged otherwise and // the field size is changed to 0. Otherwise, the field size is changed to // the given width. // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ void LimitInt::SetSize(int width) { int radix = DECIMAL; // for creating default int counter; // if (this->isDefault()) // If it contains a default, need to update it { value = 0; // Initialize value before using it in the loop for (counter=0;countersize=0; // so set it to zero (non-printing) else this->size = width; // set size to specified width return; } /******************************************************************************* // FUNCTION NAME: Store // // DESCRIPTION OF FUNCTION: Stores a new value into the object // // DESCRIPTION OF ALGORITHM: Assigns newValue to this->value // // CALLED BY: client // CALLS: none // // PARAMETERS: / in / newValue - int that is to be stored in the object // // PRECONDITIONS: none // // POSTCONDITIONS: this->value has been changed to the specified newValue // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ void LimitInt::Store(int newValue) { value = newValue; return; } /******************************************************************************* // FUNCTION NAME: ResetValue // // DESCRIPTION OF FUNCTION: Change the value stored in the object to the // default value for the object // // DESCRIPTION OF ALGORITHM: Sets the current value to zero, then adds a // INT_FILLER multiplied by the appropriate power of ten for each character in // the field width // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: An appropriate default value has been calculated for the // object and stored into this->value // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ void LimitInt::ResetValue() { int counter; // for controlling loop int radix=DECIMAL; // for storing default value value=0; // Initialize value before using it for (counter=0;countersize into this->value // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: The value and limits of the object have been set to default // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ void LimitInt::Reset() { int counter; // for controlling loop int radix=DECIMAL; // for storing default value lowCompare=topCompare=DEFAULT_COMPARE; // Reset all limit info to defaults lowerLimit=upperLimit=DEFAULT_INT_LIMIT; // value=0; // Initialize value before using it for (counter=0;counter