TDT4102, Spring 2008

Exercise 5

Deadline: 22.02.2008


The objective of this exercise:

General requirements:

Tools

Recommended reading:


Part 1: C-strings (15 pt.)

a)      Create a C-string variable in your main-method;

b)      Write a function which prompts the user for a line of text and stores the input in the C-string variable.
void  getInput(char[]);

c)      Write a function that that counts the number of characters in the C-string and returns the number of characters in the string (not including ‘\0’).
NB! You are not allowed to use the strlen library function in this assignment.

int count(char[]);

 

d)     Create a function to reverse the characters in a C-string:
void reverse(char[]);

Example: the function should change the string “abcde” to “edcba”.

e)      Imagine you are developing a software package that requires users to enter their own password.

Write a function that asks for a password and then verifies that it meets the stated criteria. If it doesn't, the program should display a message telling the user why.
void verify(char[]);

The password has to meet the following criteria:

·         The password should be at least six characters long.

·         The password should contain at least one uppercase and at least one uppercase and at least one lowercase letter.

·         The password should have at least one digit.

Part 2: The Rational class (15 pt.)

In this part of the exercise you will create the class Rational that will be used throughout this exercise.
Rational numbers are all the numbers that can be written as a ratio of two integers. Rational numbers can be expressed as a fraction, which is written as a / b where a is the numerator and b is the denominator. Note that b should not be zero.

a)      Create the class Rational that has the private member values numerator and denominator. Use a long integer for both these values.

Hint: The long type is simply an integer type that is always at least 4 bytes big.

b)      Create a constructor for the class that takes two numbers as input, the numerator and the denominator as long integers.

c)      Create the public member functions getNumerator() and getDenominator() that will return the appropriate numbers as long integers.

d)     Create a public intValue() function, that returns the value of the rational number as an integer. Use normal integer division.

e)      Create a public doubleValue() function, that returns the value of the rational number as a double.


Part 3: First operator overloading (20 pt.)

Now that we have implemented some functionality for our class, it's time to overload some operators. For now, implement these operators as non-member, that is - operators outside the class. They should all be taking two rational numbers references as input, and simply returning the appropriate boolean value. Since you will not have access to the private fields of the Rational class, you should use the get-functions.

a)      Overload the "= =" operator for the Rational class. The operator takes as input two Rational Number References and returns a boolean.

b)      Overload the "!=" operator for the Rational class. The operator takes as input two Rational Number References and returns a boolean.

c)      Overload the "<" operator for the Rational class. The operator takes as input two Rational Number References and returns a boolean.

d)     Overload the ">" operator for the Rational class. The operator takes as input two Rational Number References and returns a boolean.

e)      Overload the "<=" operator for the Rational class. The operator takes as input two Rational Number References and returns a boolean.

f)       Overload the ">=" operator for the Rational class. The operator takes as input two Rational Number References and returns a boolean.

 

Part 4: Operator overloading, friend functions (20 pt.)

We will now look at overloading some more operators, this time the arithmetic operators. This time we will use a slightly different approach.
These operations are not quite straightforward, so wipe the dust of your old math books and read up on operations on fractions (brøkregning! ).
The problem with implementing operators as non-members of a class is that the normal rules for private data is followed, so you cannot access the private attributes of the objects.
To fix this problem, we can use Friend Functions. This will ensure that the operators will have access to our private fields. Use this approach when you override these operators. To ensure that you have access to the private fields, use the numerator and denominator fields directly rather than using the getNumerator() and getDenominator() functions.

a)      Overload the "+" operator for the Rational class. This operator should be a friend function of the Rational class and takes as input two Rational Number References and returns the resulting Rational Number.

b)      Overload the "-" operator for the Rational class. This operator should be a friend function of the Rational class and takes as input two Rational Number References and returns the resulting Rational Number.

c)      Overload the "*" operator for the Rational class. This operator should be a friend function of the Rational class and takes as input two Rational Number References and returns the resulting Rational Number.

d)     Overload the "/" operator for the Rational class. This operator should be a friend function of the Rational class and takes as input two Rational Number References and returns the resulting Rational Number.


Part 5: Operator overloading, member functions (20 pt.)

All right, it's time for some more operators, this time using yet another approach.
Now, instead of using the friend approach from the previous Part, we will implement some operators as members of the Rational class. This will ensure us access to the private fields directly.

a)      Overload the postfix "++" operator for the Rational class. When increased, the number should be increased by 1 ( the numerator should increase by the denominator ).( 1 / 3 ) ++ should result in ( 4 / 3 ) !
Hint: To distinguish between the prefix ( ++i ) and postfix ( i++ ) operators, c++ uses a dummy int value as an argument in the postfix version ( see page 345 of the textbook )

b)      Overload the postfix "--" operator for the Rational class. This is basically the opposite of the "++" operator's behavior.
Hint: The dummy int value applies to this postfix operator as well.

c)      Overload the shorthand operator +=. That is, Rational += Rational.

d)     Overload the shorthand operator -=. That is, Rational -= Rational.


Part 6: Operator overloading, insertion operator (10 pt.)

a)      We would like to be able to output rational numbers using cout << rn. Overload the "<<" operator for the Rational class to enable this. Rational numbers should be printed as fractions, like this: “1/3”.

OPTIONAL


Optional Part I: Operator overloading (optional)

We seem to have done a good job of overloading the operators, but there are always room for more. Here is a quick list of some you can try out, and you can always do the ones already overloaded in other ways, as member functions for instance. Just be sure to have a copy of your original work for the student assistant to approve.

Operators:

*=

/=

Prefix ++

Prefix --

[]

- This could be used to get the numerator and denominator as [0] and [1], for instance.

Unary +

Unary -

>>

- You should probably query the user for the numerator and denominator separately.

Optional Part II: Improving the Rational class (optional)

It's time to improve our class a little. What we would like the class to do is to simplify ( forkorte ) itself as much as possible. For instance, if we create the number ( 2 / 6 ), we would like the constructor to realize this is the same as ( 1 / 3 ), and create that number instead.



a) Add the member function gcd() that finds the Greatest Common Divisor of two numbers. The greatest common divisor of two numbers is the greatest whole number that divides both numbers. For instance, the greatest common divisor of 42 and 63 is 21.
Because the teaching assistants are feeling really generous, we have decided to give you a pseudo-code for this function.

 
Euclids algorithm:
 
 function gcd(a, b)
     while b != 0
         t = b
         b = a mod b
         a = t
     return a



b) Now improve the constructor of the class to implement the wanted functionality ( simplification ). You'll want to use the gcd function, as well as a little math..