Interval
- Objective:
Implement closed interval in C++.
- 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] are 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 the interval along with constructor methods, display methods that print 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. For example:
- + 3 10 –1 11
* -1 11 –2 -3
- Processing: After reading in each line of input, the program creates the interval objects and performs the appropriate arithmetic operation.
- Output: After processing each input line the following is being printed out:
The operation to be performed, the interval A and the category it belongs to, the interval B and the category it belongs to, the result of the operation, and output of the destructor.
- This program is coded in MicrosoftVisual C++, but it also compiles in the UNIX G++ C++ compiler.
- Implementation:
To view this program in MS Word format Click here