Usage of the faulty number type

I have the following problem that is quite complex and I don't have a clue how to solve it:

---

Let's assume that we have got a class that defines the new type called FaultyUnsignedLongLong, which is similar to standard unsigned long long, but in which some arithmetic operations on numbers in this type are returning false values or are causing errors.

The problem is to create a function FaultyUnsignedLongLong cprod(const int &fromInt, const int &toInt) that is calculating the product (fromInt)*(fromInt+1)*...*(toInt-1)*(toInt), where fromInt and toInt are integers such that 0 <= fromInt <= toInt <= 600000.

The type FaultyUnsignedLongLong is implementing some arithmetic and logical operators. Unfortunately there is always a risk that these operators will returne false results. Arithmetic operations can cause the following exceptions:

- class ErrorCalculation {}; - operation caused an error and will not return the result,
- class ErrorPrevCalculation {}; - previous arithmetic operation was calculated in the incorrect way, so the result that was returned by this operations has been incorrect,
- class ErrorPrevCalculaionsIncreasedByOne : public ErrorPrevCalculation {}; - means that the previous arithmetic operation returned a result that was larger by one than it's true solution,
- class SystemError {}; - means that operation has cause an overall reset (values of all variables that were defined as FaultyUnsignedLongLong are change to 0). Such reset could be also cause on purpose - you have to use a static method reset().

All of the exceptions can occur in a random manner.

The file in which we are implementing the function FaultyUnsignedLongLong cprod(const int &fromInt, const int &toInt) must have the following properties:

-it should include only the file FaultyUnsignedLongLong.h,
-using #define inside the file is forbidden,
-inside the file one can't use or declare any variables of the type other then FaultyUnsignedLongLong (particullary one can't useconst_cast, type_of, class, struct or any other keywords like bool, char, long, float etc.; the int keyword can be only used twice in the definition of the function FaultyUnsignedLongLong cprod(const int &fromInt, const int &toInt),
-there should be only a definition of one function: FaultyUnsignedLongLong cprod(const int &fromInt, const int &toInt) that is calculating the product (fromInt)*(fromInt+1)*...*(toInt-1)*(toInt),
-each of the variables fromInt, toInt could be used only 4 times in the file.

A declaration of the class FaultyUnsignedLongLong looks like that:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class FaultyUnsignedLongLong {
 private:
  unsigned long long int value;
 public:
  FaultyUnsignedLongLong(const unsigned long long int &i = 0);
  FaultyUnsignedLongLong(const FaultyUnsignedLongLong &i);
  ~FaultyUnsignedLongLong();

  static void reset();

  friend FaultyUnsignedLongLong operator+(const FaultyUnsignedLongLong &i, const FaultyUnsignedLongLong &j) 
		throw(ErrorCalculation, ErrorPrevCalculation, SystemError);
  friend FaultyUnsignedLongLong operator-(const FaultyUnsignedLongLong &i, const FaultyUnsignedLongLong &j) 
		throw(ErrorCalculation, ErrorPrevCalculation, SystemError);
  friend FaultyUnsignedLongLong operator*(const FaultyUnsignedLongLong &i, const FaultyUnsignedLongLong &j) 
		throw(ErrorCalculation, ErrorPrevCalculation, SystemError);
  friend FaultyUnsignedLongLong operator/(const FaultyUnsignedLongLong &i, const FaultyUnsignedLongLong &j) 
		throw(ErrorCalculation, ErrorPrevCalculation, SystemError);
  friend FaultyUnsignedLongLong operator%(const FaultyUnsignedLongLong &i, const FaultyUnsignedLongLong &j) 
		throw(ErrorCalculation, ErrorPrevCalculation, SystemError);

  friend bool operator==(const FaultyUnsignedLongLong &i, const FaultyUnsignedLongLong &j);
  friend bool operator!=(const FaultyUnsignedLongLong &i, const FaultyUnsignedLongLong &j);
  friend bool operator<=(const FaultyUnsignedLongLong &i, const FaultyUnsignedLongLong &j);
  friend bool operator>=(const FaultyUnsignedLongLong &i, const FaultyUnsignedLongLong &j);
};


and it's source code has the following includes:

1
2
3
4
5
6
#include <map>
#include <set>
#include <iostream>
#include <cstdlib>
#include <climits>
#include <iosfwd> 


---

I will be very glad if any of you could give me some hints or possible solutions of this problem.
can we say, this is
(fromInt)*(fromInt+1)*...*(toInt-1)*(toInt)

equivalent to??
n!/(r-1)! where n = toInt and r = fromInt, (r != 0)

anyway this doesnt matter..

see if you see the whole problem at once you wont be able to start..breaking a big problem in small chunks helps..
i suggest you start implementing one function at a time and implement the whole class..dont try to fulfill each condition give to you. because optimizing or whatever should not go during development.. when you successfully implement your full class then try to fulfill each condition given in the problem. That will make your task easy.

second thing, if you need to implement exception handling, then you should first write your exception classes and then your main class. implementing exception class is not that difficult, just derive from standard exception class.
start the task and take help from the forum.. :)



Topic archived. No new replies allowed.