Unformatted text preview:

MAIN Driver include iostream include myint h using namespace std MyInt Fibonnaci MyInt num int main demonstrate behavior of the two constructors and the overload MyInt x 12345 y 9876543210123456789 r1 1000 r2 14H67 r3 char answer cout Initial values nx x ny y nr1 r1 nr2 r2 nr3 r3 n n demonstrate overload cout Enter first number cin x cout Enter second number cin y cout You entered n cout x x n cout y y n demonstrate assignment cout Assigning r1 y n r1 y cout r1 r1 n demonstrate comparison overloads if x y if x y if x y if x y if x y if x y cout x y is TRUE n cout x y is TRUE n cout x y is TRUE n cout x y is TRUE n cout x y is TRUE n cout x y is TRUE n demonstrating and overloads r1 x y cout The sum x y r1 n r2 x y cout The product x y r2 n n cout The sum x 12345 x 12345 n cout The product y 98765 y 98765 n create Fibonacci numbers stored as MyInts using cout nAssuming that the Fibonnaci sequence begins 1 1 2 3 5 8 13 n nThe 10th Fibonnaci number Fibonnaci 10 n nThe 100th Fibonnaci number Fibonnaci 100 n nThe 1000th Fibonnaci number Fibonnaci 1000 n nThe 2000th Fibonnaci number Fibonnaci 2000 n n MyInt Fibonnaci MyInt num MyInt n1 1 n2 1 n3 MyInt i 2 while i num n3 n1 n2 n1 n2 n2 n3 i return n2 END MAIN H include iostream using namespace std class MyInt friend functions friend bool operator const MyInt i1 const MyInt i2 friend bool operator const MyInt i1 const MyInt i2 friend bool operator const MyInt i1 const MyInt i2 friend bool operator const MyInt i1 const MyInt i2 friend bool operator const MyInt i2 const MyInt i1 friend bool operator const MyInt i1 const MyInt i2 friend ostream operator ostream s const MyInt i friend istream operator istream s MyInt friend MyInt operator const MyInt const MyInt friend MyInt operator const MyInt const MyInt friend MyInt operator const MyInt constructors MyInt char numString MyInt int n MyInt const MyInt MyInt MyInt public private be sure to add in the second constructor and the user defined versions of destructor copy constructor and assignment operator void Grow void debug const MyInt MyInt operator const MyInt member data suggested use a dynamic array to store the digits int maxNumStringSize int currStringSize void ReverseOrder char digitCharArry int SizeOfChar char n void setCurrStringSize char n void setCharString char n END H CPP include iostream include myint h include cstring using namespace std converts character into integer returns 1 for error int C2I char c if c 0 c 9 return 1 error return c 0 success converts single digit integer into character returns 0 for error char I2C int x if x 0 x 9 return static cast char x 0 return 0 error success overloaded reverse reverses the number char ReverseOrder char arry int maxSize char tmp new char maxSize int var maxSize 1 for int i 0 i maxSize i tmp i arry var return tmp copy const MyInt MyInt const MyInt i currStringSize i currStringSize maxNumStringSize i maxNumStringSize strcpy digitCharArry i digitCharArry cout Const Constructor endl int const MyInt MyInt int c currStringSize 0 maxNumStringSize 5 int var1 var2 digitCharArry new char 5 if c 0 else strcpy digitCharArry 0 currStringSize 1 var1 c while var1 0 var2 var1 10 var1 var1 10 digitCharArry currStringSize I2C var2 if currStringSize maxNumStringSize Grow ReverseOrder cout int Constructor endl default const MyInt MyInt maxNumStringSize 1 currStringSize 1 digitCharArry new char 5 strcpy digitCharArry 0 char const MyInt MyInt char c int i 0 currStringSize 0 maxNumStringSize 5 digitCharArry new char 5 destructor MyInt MyInt delete digitCharArry if c 0 else strcpy digitCharArry 0 while currStringSize strlen c C2I c i 1 digitCharArry currStringSize c i if currStringSize maxNumStringSize Grow less than overloader bool operator const MyInt i1 const MyInt i2 if i1 currStringSize i2 currStringSize else if i1 currStringSize i2 currStringSize return true return false else for int i 0 i i1 currStringSize i if C2I i1 digitCharArry i C2I i2 digitCharArry i C2I i2 digitCharArry i else if C2I i1 digitCharArry i return true return false return false add op overload MyInt operator const MyInt i1 const MyInt i2 endl cout YO i1 currStringSize i2 currStringSize MyInt mi int i1Index i1 currStringSize 1 int i2Index i2 currStringSize 1 int i 0 j 0 char tmp int maxResultString 0 bool carry false char result if i1 currStringSize i2 currStringSize maxResultString i1 currStringSize else maxResultString i2 currStringSize maxResultString cout MaxStringSize maxResultString endl tmp new char maxResultString do if i1Index 0 i1Index 0 i C2I i1 digitCharArry i1Index C2I i2 digitCharArry i2Index if i 9 else i i 10 if carry i carry true tmp j I2C i if carry i carry false tmp j I2C i strcat tmp tmp2 else break while 1 mi digitCharArry ReverseOrder tmp maxResultString mi currStringSize j return mi less than eq op overload bool operator const MyInt i1 const MyInt i2 if i1 currStringSize i2 currStringSize else if i1 currStringSize i2 currStringSize return true return false else for int i 0 i i1 currStringSize i C2I i2 digitCharArry i if C2I i1 digitCharArry i C2I i2 digitCharArry i else if C2I i1 digitCharArry i return true return false return true greater than op overload bool operator const MyInt i1 const MyInt i2 if i1 currStringSize i2 currStringSize else if i1 currStringSize i2 currStringSize return true return false else for int i 0 i i1 currStringSize i C2I i2 digitCharArry i if C2I i1 digitCharArry i C2I i2 digitCharArry i else if C2I i1 digitCharArry i return true return false return false greater than eq op overload bool operator const MyInt i1 const MyInt i2 if i1 currStringSize i2 currStringSize else if i1 currStringSize i2 currStringSize return true return false else for int i 0 i i1 currStringSize i if C2I i1 digitCharArry i C2I i2 digitCharArry i C2I i2 digitCharArry i else if C2I i1 digitCharArry i return true return false return true comparison op overload bool operator const MyInt i1 const MyInt i2 return strcmp i1 digitCharArry i2 digitCharArry not eq op overload bool operator const MyInt i1 const MyInt i2 return strcmp i1 digitCharArry i2 digitCharArry assignment op overload MyInt MyInt operator const MyInt i if this i delete digitCharArry digitCharArry new char i currStringSize 1 currStringSize i currStringSize 1 strcpy digitCharArry i digitCharArry return this return this return this object MyInt MyInt operator const MyInt i output overloader ostream operator ostream s const MyInt i s i digitCharArry return s sets the current string size void MyInt


View Full Document

FSU COP 3330 - Notes

Download Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?