//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //Date: 02.02.2000 //Interval Implementation //Description: A closed interval I takes the form [Is, Ie], //where s is the starting point and e is the ending point. //For example, for an interval A = [3, 6], the starting point //is 3 and the ending point is 6. Arithmetic operations on two //intervals A = [3, 6] and B = [2, 10] is defined as follows: //Addition: A + B = [As + Bs, Ae + Be] //Subtraction: A - B = [As - Be, Ae - Bs] //Multiplication: A x B = [min (As x Bs, As x Be, Ae x Bs, Ae x Be), //max (As x Bs, As x Be, Ae x Bs, Ae x Be)] //Also, an interval A can be categorized as either being SOLID, //IN AIR, or SURFACE based on the following conditions: //SOLID if Ae < 0; IN AIR if As > 0; SURFACE if As <= 0 <= Ae. //The objective is to write a C++ program using object-oriented principles //to perform interval arithmetic on the input intervals. We have to implement //interval along with constructor //methods, display methods that prints the start and end point of the interval, //methods to perform arithmetic operations , and //method to display the category of the interval. Parameter passing is be //done only by reference. //Input: The input for the program consists of an unknown number of lines of //input data. // Each line of the input data contains the arithmetic operation, the staring //point of interval A, the ending point of interval A, the starting point of //interval B, and the ending point of interval B. //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% #include class Interval //this is the class for the Interval { protected: //protected data char op_type; double as; double ae; double bs; double be; public: //public data Interval(char op_type1, double as1, double ae1, double bs1, double be1); //constructor ~Interval(); //destructor char display_op_type(); //method for displaying type of operation double display_as(); //displays interval boundaries double display_ae(); double display_bs(); double display_be(); double add1(); //performs addition double add2(); double subtract1(); //performs subtraction double subtract2(); double multiplicate1(); //performs multiplication double multiplicate2(); void display_category(double temp_as, double temp_ae); }; //****************************************************************************** Interval::Interval(char op_type1, double as1, double ae1, double bs1, double be1) //Interval constructor implementation { cout << "In constructor\n"; as = as1; ae = ae1; bs = bs1; be = be1; op_type = op_type1; } //****************************************************************************** char Interval::display_op_type() //display operation method implementation { return op_type; } //****************************************************************************** double Interval::display_as() //display interval boundaries method implementation { return as; } //****************************************************************************** double Interval::display_ae() { return ae; } //****************************************************************************** double Interval::display_bs() { return bs; } //****************************************************************************** double Interval::display_be() { return be; } //****************************************************************************** double Interval::add1() { return as+bs; } //****************************************************************************** double Interval::add2() { return ae+be; } //****************************************************************************** double Interval::subtract1() //interval subtract method multiplication { return as-be; } //****************************************************************************** double Interval::subtract2() { return ae-bs; } //****************************************************************************** double Interval::multiplicate1() //interval multiplication method implementation { double a, b, c, d, temp_min; a = as*bs; b = as*be; c = ae*bs; d = ae*be; temp_min = a; if (temp_min >= b) temp_min = b; if (temp_min >= c) temp_min = c; if (temp_min >= d) temp_min = d; return temp_min; } //****************************************************************************** double Interval::multiplicate2() { double a, b, c, d, temp_max; a = as*bs; b = as*be; c = ae*bs; d = ae*be; temp_max = a; if (temp_max <= b) temp_max = b; if (temp_max <= c) temp_max = c; if (temp_max <= d) temp_max = d; return temp_max; } void Interval::display_category(double temp_as, double temp_ae) //display category method implementation { if (temp_ae < 0) cout << "SOLID\n"; else if (temp_as > 0) cout << "IN AIR\n"; else if (temp_as <= 0 && temp_ae>= 0) cout <<"SURFACE\n"; else cout << "Invalid Interval\n"; } Interval::~Interval() //destructor implementation { cout << "Destructing..\n"; } main() //main method starts here { char a; //local variables double b,c,d,e; //local variables while (!(cin.eof())) { cin >> a >> b >> c >> d >> e; //input data Interval ob(a, b, c, d, e); //create object if (a == '+') { cout << "The operation is ADD\n"; //displays Operation type cout << "Interval A: ["; //displays Interval cout << ob.display_as(); cout << ", "; cout << ob.display_ae(); cout << "]\n"; cout << "The category of Interval A is: "; ob.display_category(b, c); cout << "Interval B: ["; cout << ob.display_bs(); cout << ", "; cout << ob.display_be(); cout << "]\n"; cout << "The category of Interval B is: "; ob.display_category(d, e); cout << "The result of the operation is: ["; cout << ob.add1(); //displays result of Addition cout << ", "; cout << ob.add2(); cout << "]\n"; } //end IF else if (a == '-') { cout << "The operation is SUBTRACT\n"; cout << "Interval A: ["; cout << ob.display_as(); cout << ", "; cout << ob.display_ae(); cout << "]\n"; cout << "The category of Interval A is: "; ob.display_category(b, c); cout << "Interval B: ["; cout << ob.display_bs(); cout << ", "; cout << ob.display_be(); cout << "]\n"; cout << "The category of Interval B is: "; ob.display_category(d, e); cout << "The result of the operation is: ["; cout << ob.subtract1(); //displays result of subtraction cout << ", "; cout << ob.subtract2(); //same here cout << "]\n"; } //end ELSE-IF else if (a == '*') { cout << "The operation is MULTIPLY\n"; cout << "Interval A: ["; cout << ob.display_as(); cout << ", "; cout << ob.display_ae(); cout << "]\n"; cout << "The category of Interval A is: "; ob.display_category(b, c); cout << "Interval B: ["; cout << ob.display_bs(); cout << ", "; cout << ob.display_be(); cout << "]\n"; cout << "The category of Interval B is: "; ob.display_category(d, e); cout << "The result of the operation is: ["; cout << ob.multiplicate1(); //displays result of multiplication cout << ", "; cout << ob.multiplicate2(); //displays result of multiplication cout << "]\n"; } //end ELSE-IF } //end WHILE return 0; } //end MAIN