Dana Vrajitoru
C201/I211 Computer Programming II

Homework 9

Due Date: Wednesday, November 8, 2006.

Ex. 1 Download the following files in a new folder.
number.h
number.cpp
main.cpp

Create a project just like for the previous homework and include these three files. If you compile this on Linux, the compilation command could be:

g++ number.cpp main.cpp -o num
where the executable would be "num".

The files number.h and number.cpp implement a class representing large numbers and the operations done with them. In the main we test the arithmetic operations and then we compute the factorial of a number using objects of this class.

Implement all of the class methods and the friend operator << in the class Number based on the explanations provided as comments before each function.

You can add a test to the main for the comparison operators or modify the factorial as indicated to test other operators.

Ex. 2 (optional, 3 extra credit points) Implement the division operator more efficiently than a repeated subtraction. One idea would be to figure out the power of 10 that can be multiplied by the number data to obtain a number still less than the target object. This can be done based on the difference in size. Then you can start from the difference between the target and data multiplied by that power of 10 (use the shift). Any other method to improve this function is acceptable.

Note. A possible implementation of the function strcmp for strings:

int strcmp(const char *str1, const char str2)
{
  int size1 = strlen(str1), size2 = strlen(str2);
  int i=0;
  while (i < size1 && i < size2 && str1[i] == str2[i])
    i++;
  if (str1[i] < str2[i])
    return -1;
  else if (str1[i] == str2[i])
    return 0;
  else
    return 1;

  // Alternative that returns a positive number if str1>str2, 0 if
  // they are equal, and a negative number if str1 < str2:
  // return str1[i] - str2[i];
}

Turn in: the modified file number.cpp (the others don't need any changes).