Page 273 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 273
250 Chapter 8 Programming in C and C++
and rdbot (i) reads the ith element from the bottom of the deque. Function members
rdtop( i) and rdbot (i) do not move the pointers. Use inherited data and function
members wherever possible.
22. Write a templated class Matrix that implements matrix addition and multiplication
for square matrixes (number of rows = number of columns). Overloaded operator + adds
two intervals resulting in an interval, overloaded operator * multiplies two matrixes
resulting in a matrixes, and overloaded operators = and cast with overloaded operator
[] writes or reads elements; for instance, if M is an object of class Matrix, then
M [ i ] [ j 3 = 5; will write 5 into row i , column j , of matrix M, and k =
M{ i ] [ j ]; will read row i, column j , of matrix M into k. Matrix's constructor
has an argument size that is stored as a data member size and allocates enough
memory to hold a size by size matrix of elements of the template's data width, using
a call to the procedure allocate.
23. Intervals can be used to calculate worst-case possibilities, for instance in
determining if an I/O device's setup and hold times are satisfied. An interval <a,b>, a s
b, is a range of real numbers between a and b. If <a,b> and <c,d> are intervals A and B,
then the sum of A and B is the interval <a+c, b+d>, and the negative of A is <-b, -a>,
Interval A contains interval B if every point in A is also in B. Write a templated class
Interval having public overloaded operators + for adding two intervals resulting in an
interval, - for negating an interval resulting in an interval, and an overloaded operator
> returning a char value 1 if the left interval contains the right interval, otherwise it
returns 0. If A, B, and c are of class Interval, the expression A = B + C; will add
intervals A and B and put the result in c, A = - B; will put the negative of A into
B, and the expression if (A > B) i = 0; will clear i if A contains B. The
template allows for the values such as a or b to be char, int, or long. The class
has a public variable error that is initially cleared and set if an operation cannot be
done or results in an overflow.
24. Write a templated class Interval, having the operators of Problem 25 and
additional public overloaded operators * for multiplying two intervals to get an interval,
and / for dividing two intervals to get an interval, and a procedure sqrt (Interval},
which is a friend of Interval, for taking the square root. Use the naive rule for
multiplication, where all four terms are multiplied, and the lowest and highest of these
terms are returned as the product interval, and assume there is already a procedure long
sqrt (long) that you can use for obtaining the square root (do not write this procedure).
If A, B, and C are of class Interval, the expression A = B * C; will multiply
intervals B and C and put the result in A; A = B/c will divide B by c, putting the
result in A, and A = sqrt (B); will put the square root of B into A. Note that a/b
is a * (1/b), so the multiply operator can be used to implement the divide operator,
a - b is a + (-b), so the add and negate operators can be used to implement the
subtract operator; and 4*ais a + a + a + a, so scalar multiplication can be done
by addition. Also Interval has a public data member error that can be set if we