// balfloat.cpp Implementation of LimitInt functions /******************************************************************************* // MODULE NAME: LimitFloat // INTERFACE FILE: balfloat.h // IMPLEMENTATION FILE: balfloat.cpp // // PURPOSE: To provide an float type useful for the BAL program which can store // float values, prfloating width, and logical limits upon those floats. 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 float, to print in a formatted string the current value of the float, to // print the default value for the float, & to reset the value or limits or both // of the float // // FUNCTIONS: Name Purpose // LimitFloat Parameterless constructor // LimitFloat Constructor w/specified field width // LimitFloat Constructur w/ field width, limit type, limit // value // LimitFloat Constructor w/ field width, 2 limit types, 2 // limit values // isValid Indicate whether the value stored in the // LimitFloat 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 float 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 "balfloat.h" /********************************CONSTRUCTORS**********************************/ /******************************************************************************* // FUNCTION NAME: LimitFloat // // DESCRIPTION OF FUNCTION: Parameterless constructor // // DESCRIPTION OF ALGORITHM: Assigns default values to each data member of the // object // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: Object has been created w/default values for each data // member // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ LimitFloat::LimitFloat() { 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_FLT_LIMIT; // size=DEFAULT_FLT_SIZE; // value=0.0; // Initialize value before we use it for (counter=0;counterisDefault()) // Don't error-check the default value return fltOK; switch (lowCompare) // check the value against the appropriate constraints for { // the first limit case NONE: // No limit == no problem break; case LESSTHAN: if (!(valuelowerLimit) // value should be <= lowerLimit fltOK=false; break; case GREATERTHAN: if (!(value>lowerLimit)) // value should be > lowerLimit fltOK=false; break; case GREATERTHANEQUALS: if (value= lowerLimit fltOK=false; break; } if (!fltOK) // If it didn't fit the first limit, there's no need return fltOK; // 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 (!(valueupperLimit) // value s/b <= upperLimit fltOK=false; break; case GREATERTHAN: if (!(value>upperLimit)) // value s/b > upperLimit fltOK=false; break; case GREATERTHANEQUALS: if (value= upperLimit fltOK=false; break; } return fltOK; // report whether the value fit its limits } /******************************************************************************* // FUNCTION NAME: isDefault // // DESCRIPTION OF FUNCTION: Indicates whether this 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. Because these // are floating-point numbers, it tests to see that the value is neither less // than nor greater than the default rather than directly testing for exact // equality. // // CALLED BY: isValid, GreaterThan, LessThan, Equals, client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: If the value is neither greater than nor less than the // default value for this object, returns true. Returns false otherwise. // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ bool LimitFloat::isDefault() { int checkFlt=0.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;countercheckFlt))&&(!(valueisDefault())&&(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: Tests to determine if the both values are defaults // and if not, whether this->value is neither LessThan nor GreaterThan the // other value. // // CALLED BY: client // CALLS: isDefault, LessThan, GreaterThan // // PARAMETERS: / in / another - LimitFloat this one is being compared to // // PRECONDITIONS: none // // POSTCONDITIONS: If this LimitFloat's value is not equal to that of the one // it is being compared to, and they do not both contain default values, // returns false. Returns true otherwise. // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ bool LimitFloat::Equals(LimitFloat another) { if ((this->isDefault())&&(another.isDefault())) return true; return (!(this->LessThan(another)))||(!(this->GreaterThan(another))); } /******************************************************************************* // FUNCTION NAME: valueIs // // DESCRIPTION OF FUNCTION: Report the value currently stored in the object // // DESCRIPTION OF ALGORITHM: Returns value // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: Returns a float that contains whatever is currently stored // in the value member. // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ float LimitFloat::valueIs() { return value; } /******************************************************************************* // FUNCTION NAME: Print // // DESCRIPTION OF FUNCTION: Creates a string with the appropriate width that // contains the current value of the object // // DESCRIPTION OF ALGORITHM: Converts the value to a string and prints enough // characters to fill the width of the field as defined by this->size // // CALLED BY: Default, client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: Returns a string of width this->size which contains the // current value of the LimitFloat object // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ string LimitFloat::Print() { ostringstream floatString; // hold float as string without any formatting string outputString=" "; // hold formatted float int counter; floatString << value; // send value to float output stringstream if ((floatString.str()).length() characters of the float string outputString = ((floatString.str()).substr(0,size)); return outputString; // send the pretty string to the calling function } /******************************************************************************* // FUNCTION NAME: Default // // DESCRIPTION OF FUNCTION: Prints out the default value for this string // with the appropriate field width // // DESCRIPTION OF ALGORITHM: Creates an object of the same size with a default // value, then Prints it // // CALLED BY: client // CALLS: LimitFloat, Print // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: Returns a string of width this->size that contains the // default value for the LimitFloat object // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ string LimitFloat::Default() { LimitFloat defaultFloat(size); // Create default object return defaultFloat.Print(); // Print default object } /******************************************************************************* // FUNCTION NAME: Limits // // DESCRIPTION OF FUNCTION: Prints out string that tells what the constraints // are on this object // // DESCRIPTION OF ALGORITHM: Depending on the type of limits defined in // lowCompare and topCompare, prints out messages indicating type of limits. // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: Returns a string which indicates what limits are currently // in effect on the object. If both limits have been set, the string will // contain an embedded newline. // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ string LimitFloat::Limits() { ostringstream outputString; // To hold limit information switch (lowCompare) // Print out a message depending on the type of limit { case NONE: outputString << "No limit"; break; case LESSTHAN: outputString << "Must be less than " << lowerLimit; break; case LESSTHANEQUALS: outputString << "Must be less than or equal to " << lowerLimit; break; case GREATERTHAN: outputString << "Must be greater than " << lowerLimit; break; case GREATERTHANEQUALS: outputString << "Must be greater than or equal to " << lowerLimit; break; } switch (topCompare) { case NONE: break; case LESSTHAN: outputString << '\n' << "Must be less than " << upperLimit; break; case LESSTHANEQUALS: outputString << '\n' << "Must be less than or equal to " << upperLimit; break; case GREATERTHAN: outputString << '\n' << "Must be greater than " << upperLimit; break; case GREATERTHANEQUALS: outputString << '\n' << "Must be greater than or equal to " << upperLimit; break; } return outputString.str(); // Return string with limit message in it } /*****************************TRANSFORMERS*************************************/ /******************************************************************************* // FUNCTION NAME: setLimit // // DESCRIPTION OF FUNCTION: Sets one limit to user-specifed 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 - float magnitude of lowerLimit // // PRECONDITIONS: none // // POSTCONDITIONS: Lower limit of the LimitFloat 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 LimitFloat::setLimit(LimitType comparison, float Limit) { topCompare=DEFAULT_COMPARE; // Reset top limit to default upperLimit=DEFAULT_FLT_LIMIT; // lowCompare=comparison; // Set lower limit with user-supplied info lowerLimit=Limit; // return; } /******************************************************************************* // FUNCTION NAME: setLimit // // DESCRIPTION OF FUNCTION: Sets one limit to user-specifed 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 / compare1 - LimitType that indicates the relationship // between value and lowerLimit // / in / Limit1 - float magnitude of lowerLimit // / in / compare2 - LimitType that indicates the relationship // between value and upperLimit // / in / Limit2 - float magnitude of upperLimit // // PRECONDITIONS: none // // POSTCONDITIONS: Both limits of the LimitFloat have been set according to // the user-supplied information // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ void LimitFloat::setLimit(LimitType compare1,float Limit1,LimitType compare2, float 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 LimitFloat::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.0f; // 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 value // // CALLED BY: client // CALLS: none // // PARAMETERS: / in / newValue - float that contains the new float value to // be stored in the object // // PRECONDITIONS: none // // POSTCONDITIONS: this->value has been changed to the specified value // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ void LimitFloat::Store(float newValue) { value=newValue; return; } /******************************************************************************* // FUNCTION NAME: ResetValue // // DESCRIPTION OF FUNCTION: Resets the value stored in the object to the // default value for that object // // DESCRIPTION OF ALGORITHM: Depending on the field size of the object, creates // and stores a default value for the object. // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: value now contains an appropriate default value for this // object // // AUTHOR: Amy Langill *2B DATE: 3/20/1999 *******************************************************************************/ void LimitFloat::ResetValue() { int counter; // for controlling loop int radix=DECIMAL; // for storing default value value=0.0; // Initialize value before using it for (counter=0;counter