讲解CISP 400、辅导Programming Test、讲解CS/python, C/C++编程

- 首页 >> Python编程

CISP 400 – Final Exam – Programming Test

Due December 20, 2018 at 11:59 PM

NO CHANGES TO THIS DUE DATE WILL BE MADE

Please answer the following questions in a single PLAIN TEXT file (e.g. not HTML, not word

processing, etc. Windows Notepad can be used if you are unsure, as can most IDEs that you have used

for programming in this class). Questions 1 and 2 are independent of all other questions. Questions 3-

5 build upon one another, although each question can be answered without fully or correctly answering

the others if you are having difficulty with an earlier question. Each question is worth 8 points, for a

total of 40 points (50% of the exam).

As with the first part of the exam, it is open-book and open-note but should not be discussed or done

with any other person. Group work or obtaining help of any kind from another individual will be

considered cheating.

1. Write a template function. The template should accept one type parameter, type T. The function

itself should return type T, and accept THREE parameters – an array of type T named source, a single

instance of type T named initial, and an int that is the length of the array named length. The function

should have a local variable of type T, named total, which is initialized to the value provided by initial.

The function should loop over the array to use the + operator to combine all array elements into total. It

should then return total variable. In other words, if it is passed an array of type double, then it will add

up all the elements in the array. If it is fed an array of string type, it will concatenate all of the strings

in the array (that is to say, combine them, one after another). NOTE: We are looking for an ordinary

array for this question. Do not use the STL array type.

2. For this question, we will be using the ErrorData class. We aren’t writing it, just using it in our

code. All you need to know about it is that it will work with the << operator, so it can be printed out to

the user using cout. Its constructor takes no parameters, and neither the constructor nor destructor does

anything that you need to worry about (or for that matter, anything at all).

Suppose we are writing a function, attemptErrorProneOperation(). attemptErrorProneOperation()

rerturns nothing and takes no parameters. Normally the only thing attemptErrorProneOperation() does

is it calls another function, possibleError(). You do not need to know anything about possibleError()

other than it may throw an exception. In addition to calling possibleError(),

attemptErrorProneOperation() catches exceptions thrown by its call to possibleError(). Any exceptions

thrown by possibleError() will be either a variable of type int, or an object that is an instance of the

ErrorData class. Any exception data caught should be printed to the user using cout.

attemptErrorProneOperation() will then exit, whether or not an exception has been caught.

Write the attemptErrorProneOperation() function so that it makes the call to possibleError() and

handles both types of exception that possibleError() may throw. Remember that there is a specific way

that you should catch objects that we have discussed, so be sure to use that.

3. Write an abstract class. The class will be named ShapeCalc. It will define the following members:

two double type variables named a and b, both set with access so that subclasses may access

them directly but the outside world cannot.

a getter and setter for a and b, each, so you have getA(), getB(), setA() and setB(), each

returning the appropriate data types and taking the appropriate parameters.

a pure virtual function, calculation, that takes a parameter of type bool, and returns a value of

type double.

a virtual destructor that does nothing (empty body). If you wish, for debugging purposes it may

print a single message to the screen informing the user that it is being destroyed, but that is

strictly optional. The code for this function may be inline (part of the class declaration) or

outside of the declaration, but a body must be provided.

You may write the code to work in either the class declaration or outside of the class declaration, but all

code must CLEARLY be coded as a member of the class, e.g. don't make any of these non-class

functions.

A constructor is not necessary, but can be included if you wish.

4. Assume that you have written ShapeCalc correctly.

We are now writing two subclasses of ShapeCalc: HypCalc and CylCalc. Note that each should inherit

ShapeCalc for public access.

Both of these have only one member function, which overrides the calculation() virtual function.

For CylCalc, you must take the member variables and use them to calculate the volume of a cylinder,

specifically V = πr2

h. As per usual in equations, π can be assumed to equal 3.14, but you can use a

more precise constant if you would prefer. r will be a, and h will be b. So: (3.14) * (a * a* b). The

function will then look at its only parameter, which is of type bool. If it is true, then the calculated

value will be multiplied by 2 and then returned; if false, it will be returned as it was originally

calculated.

For HypCalc, we are going to calculate the hypotenuse of a right triangle, which is:

h=√(a2+b2)

with h being the hypotenuse length. Use the mathematical library functions pow() and sqrt() to

calculate this value. The function will then look at its only parameter, which is of type bool. If it is

true, then the calculated value will be multiplied by 2 and returned; if false, it will be returned as

originally calculated.

5. Assume that you wrote ShapeCalc and at least one of its subclasses (HypCalc or CylCalc) correctly.

Make a non-member function named compute(), which takes in two double arguments and returns no

value. It will need two local variables – a shared pointer of type ShapeCalc named crunch (as in,

crunching the numbers), and a double named result.

First, crunch will be set to a shared pointer to hold an object of either type HypCalc or CylCalc (your

choice, the code should be identical for both outside of the class name). This object will need to be

constructed within the function.

crunch will use the setters to provide the values 2 and 4 to the a and b variables of crunch, using the

ShapeCalc setter functions.

The calculation() member function on crunch will then be executed, and the value will be stored in

result.

The value in result will then be printed to cout, followed by an endline.

Finally the function will clean up after itself (if need be, remember, you might not need to do

anything!) and the function will return.


站长地图