From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- Bachelor/Prog1/examples/ch08/Fig08_03/Fig08_03.cpp | 89 +++++++++ .../Prog1/examples/ch08/Fig08_04_06/Array1.cpp | 177 +++++++++++++++++ Bachelor/Prog1/examples/ch08/Fig08_04_06/Array1.h | 61 ++++++ .../Prog1/examples/ch08/Fig08_04_06/Fig08_06.cpp | 90 +++++++++ .../Prog1/examples/ch08/Fig08_07_09/Fig08_09.cpp | 101 ++++++++++ .../Prog1/examples/ch08/Fig08_07_09/String1.cpp | 221 +++++++++++++++++++++ Bachelor/Prog1/examples/ch08/Fig08_07_09/String1.h | 85 ++++++++ Bachelor/Prog1/examples/ch08/Fig08_10_12/Date1.cpp | 137 +++++++++++++ Bachelor/Prog1/examples/ch08/Fig08_10_12/Date1.h | 49 +++++ .../Prog1/examples/ch08/Fig08_10_12/Fig08_12.cpp | 54 +++++ Bachelor/Prog1/examples/ch08/Fig08_13/Fig08_13.cpp | 104 ++++++++++ Bachelor/Prog1/examples/ch08/Fig08_14/Fig08_14.cpp | 134 +++++++++++++ .../Prog1/examples/ch08/Fig08_15_17/Complex1.cpp | 54 +++++ .../Prog1/examples/ch08/Fig08_15_17/Complex1.h | 35 ++++ .../Prog1/examples/ch08/Fig08_15_17/Fig08_17.cpp | 57 ++++++ .../Prog1/examples/ch08/Fig08_18_20/Fig08_20.cpp | 51 +++++ .../Prog1/examples/ch08/Fig08_18_20/Hugeint1.cpp | 121 +++++++++++ .../Prog1/examples/ch08/Fig08_18_20/Hugeint1.h | 47 +++++ 18 files changed, 1667 insertions(+) create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_03/Fig08_03.cpp create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_04_06/Array1.cpp create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_04_06/Array1.h create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_04_06/Fig08_06.cpp create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_07_09/Fig08_09.cpp create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_07_09/String1.cpp create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_07_09/String1.h create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_10_12/Date1.cpp create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_10_12/Date1.h create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_10_12/Fig08_12.cpp create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_13/Fig08_13.cpp create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_14/Fig08_14.cpp create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_15_17/Complex1.cpp create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_15_17/Complex1.h create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_15_17/Fig08_17.cpp create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_18_20/Fig08_20.cpp create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_18_20/Hugeint1.cpp create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_18_20/Hugeint1.h (limited to 'Bachelor/Prog1/examples/ch08') diff --git a/Bachelor/Prog1/examples/ch08/Fig08_03/Fig08_03.cpp b/Bachelor/Prog1/examples/ch08/Fig08_03/Fig08_03.cpp new file mode 100644 index 0000000..ac5e434 --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_03/Fig08_03.cpp @@ -0,0 +1,89 @@ +// Fig. 8.3: fig08_03.cpp +// Overloading the stream-insertion and +// stream-extraction operators. +#include + +using std::cout; +using std::cin; +using std::endl; +using std::ostream; +using std::istream; + +#include + +using std::setw; + +// PhoneNumber class definition +class PhoneNumber { + friend ostream &operator<<( ostream&, const PhoneNumber & ); + friend istream &operator>>( istream&, PhoneNumber & ); + +private: + char areaCode[ 4 ]; // 3-digit area code and null + char exchange[ 4 ]; // 3-digit exchange and null + char line[ 5 ]; // 4-digit line and null + +}; // end class PhoneNumber + +// overloaded stream-insertion operator; cannot be +// a member function if we would like to invoke it with +// cout << somePhoneNumber; +ostream &operator<<( ostream &output, const PhoneNumber &num ) +{ + output << "(" << num.areaCode << ") " + << num.exchange << "-" << num.line; + + return output; // enables cout << a << b << c; + +} // end function operator<< + +// overloaded stream-extraction operator; cannot be +// a member function if we would like to invoke it with +// cin >> somePhoneNumber; +istream &operator>>( istream &input, PhoneNumber &num ) +{ + input.ignore(); // skip ( + input >> setw( 4 ) >> num.areaCode; // input area code + input.ignore( 2 ); // skip ) and space + input >> setw( 4 ) >> num.exchange; // input exchange + input.ignore(); // skip dash (-) + input >> setw( 5 ) >> num.line; // input line + + return input; // enables cin >> a >> b >> c; + +} // end function operator>> + +int main() +{ + PhoneNumber phone; // create object phone + + cout << "Enter phone number in the form (123) 456-7890:\n"; + + // cin >> phone invokes operator>> by implicitly issuing + // the non-member function call operator>>( cin, phone ) + cin >> phone; + + cout << "The phone number entered was: "; + + // cout << phone invokes operator<< by implicitly issuing + // the non-member function call operator<<( cout, phone ) + cout << phone << endl; + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch08/Fig08_04_06/Array1.cpp b/Bachelor/Prog1/examples/ch08/Fig08_04_06/Array1.cpp new file mode 100644 index 0000000..817f7ec --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_04_06/Array1.cpp @@ -0,0 +1,177 @@ +// Fig 8.5: array1.cpp +// Member function definitions for class Array +#include + +using std::cout; +using std::cin; +using std::endl; + +#include + +using std::setw; + +#include // C++ standard "new" operator + +#include // exit function prototype + +#include "array1.h" // Array class definition + +// default constructor for class Array (default size 10) +Array::Array( int arraySize ) +{ + // validate arraySize + size = ( arraySize > 0 ? arraySize : 10 ); + + ptr = new int[ size ]; // create space for array + + for ( int i = 0; i < size; i++ ) + ptr[ i ] = 0; // initialize array + +} // end Array default constructor + +// copy constructor for class Array; +// must receive a reference to prevent infinite recursion +Array::Array( const Array &arrayToCopy ) + : size( arrayToCopy.size ) +{ + ptr = new int[ size ]; // create space for array + + for ( int i = 0; i < size; i++ ) + ptr[ i ] = arrayToCopy.ptr[ i ]; // copy into object + +} // end Array copy constructor + +// destructor for class Array +Array::~Array() +{ + delete [] ptr; // reclaim array space + +} // end destructor + +// return size of array +int Array::getSize() const +{ + return size; + +} // end function getSize + +// overloaded assignment operator; +// const return avoids: ( a1 = a2 ) = a3 +const Array &Array::operator=( const Array &right ) +{ + if ( &right != this ) { // check for self-assignment + + // for arrays of different sizes, deallocate original + // left-side array, then allocate new left-side array + if ( size != right.size ) { + delete [] ptr; // reclaim space + size = right.size; // resize this object + ptr = new int[ size ]; // create space for array copy + + } // end inner if + + for ( int i = 0; i < size; i++ ) + ptr[ i ] = right.ptr[ i ]; // copy array into object + + } // end outer if + + return *this; // enables x = y = z, for example + +} // end function operator= + +// determine if two arrays are equal and +// return true, otherwise return false +bool Array::operator==( const Array &right ) const +{ + if ( size != right.size ) + return false; // arrays of different sizes + + for ( int i = 0; i < size; i++ ) + + if ( ptr[ i ] != right.ptr[ i ] ) + return false; // arrays are not equal + + return true; // arrays are equal + +} // end function operator== + +// overloaded subscript operator for non-const Arrays +// reference return creates an lvalue +int &Array::operator[]( int subscript ) +{ + // check for subscript out of range error + if ( subscript < 0 || subscript >= size ) { + cout << "\nError: Subscript " << subscript + << " out of range" << endl; + + exit( 1 ); // terminate program; subscript out of range + + } // end if + + return ptr[ subscript ]; // reference return + +} // end function operator[] + +// overloaded subscript operator for const Arrays +// const reference return creates an rvalue +const int &Array::operator[]( int subscript ) const +{ + // check for subscript out of range error + if ( subscript < 0 || subscript >= size ) { + cout << "\nError: Subscript " << subscript + << " out of range" << endl; + + exit( 1 ); // terminate program; subscript out of range + + } // end if + + return ptr[ subscript ]; // const reference return + +} // end function operator[] + +// overloaded input operator for class Array; +// inputs values for entire array +istream &operator>>( istream &input, Array &a ) +{ + for ( int i = 0; i < a.size; i++ ) + input >> a.ptr[ i ]; + + return input; // enables cin >> x >> y; + +} // end function + +// overloaded output operator for class Array +ostream &operator<<( ostream &output, const Array &a ) +{ + int i; + + // output private ptr-based array + for ( i = 0; i < a.size; i++ ) { + output << setw( 12 ) << a.ptr[ i ]; + + if ( ( i + 1 ) % 4 == 0 ) // 4 numbers per row of output + output << endl; + + } // end for + + if ( i % 4 != 0 ) // end last line of output + output << endl; + + return output; // enables cout << x << y; + +} // end function operator<< + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch08/Fig08_04_06/Array1.h b/Bachelor/Prog1/examples/ch08/Fig08_04_06/Array1.h new file mode 100644 index 0000000..d84bc81 --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_04_06/Array1.h @@ -0,0 +1,61 @@ +// Fig. 8.4: array1.h +// Array class for storing arrays of integers. +#ifndef ARRAY1_H +#define ARRAY1_H + +#include + +using std::ostream; +using std::istream; + +class Array { + friend ostream &operator<<( ostream &, const Array & ); + friend istream &operator>>( istream &, Array & ); + +public: + Array( int = 10 ); // default constructor + Array( const Array & ); // copy constructor + ~Array(); // destructor + int getSize() const; // return size + + // assignment operator + const Array &operator=( const Array & ); + + // equality operator + bool operator==( const Array & ) const; + + // inequality operator; returns opposite of == operator + bool operator!=( const Array &right ) const + { + return ! ( *this == right ); // invokes Array::operator== + + } // end function operator!= + + // subscript operator for non-const objects returns lvalue + int &operator[]( int ); + + // subscript operator for const objects returns rvalue + const int &operator[]( int ) const; + +private: + int size; // array size + int *ptr; // pointer to first element of array + +}; // end class Array + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch08/Fig08_04_06/Fig08_06.cpp b/Bachelor/Prog1/examples/ch08/Fig08_04_06/Fig08_06.cpp new file mode 100644 index 0000000..29178eb --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_04_06/Fig08_06.cpp @@ -0,0 +1,90 @@ +// Fig. 8.6: fig08_06.cpp +// Array class test program. +#include + +using std::cout; +using std::cin; +using std::endl; + +#include "array1.h" + +int main() +{ + Array integers1( 7 ); // seven-element Array + Array integers2; // 10-element Array by default + + // print integers1 size and contents + cout << "Size of array integers1 is " + << integers1.getSize() + << "\nArray after initialization:\n" << integers1; + + // print integers2 size and contents + cout << "\nSize of array integers2 is " + << integers2.getSize() + << "\nArray after initialization:\n" << integers2; + + // input and print integers1 and integers2 + cout << "\nInput 17 integers:\n"; + cin >> integers1 >> integers2; + + cout << "\nAfter input, the arrays contain:\n" + << "integers1:\n" << integers1 + << "integers2:\n" << integers2; + + // use overloaded inequality (!=) operator + cout << "\nEvaluating: integers1 != integers2\n"; + + if ( integers1 != integers2 ) + cout << "integers1 and integers2 are not equal\n"; + + // create array integers3 using integers1 as an + // initializer; print size and contents + Array integers3( integers1 ); // calls copy constructor + + cout << "\nSize of array integers3 is " + << integers3.getSize() + << "\nArray after initialization:\n" << integers3; + + // use overloaded assignment (=) operator + cout << "\nAssigning integers2 to integers1:\n"; + integers1 = integers2; // note target is smaller + + cout << "integers1:\n" << integers1 + << "integers2:\n" << integers2; + + // use overloaded equality (==) operator + cout << "\nEvaluating: integers1 == integers2\n"; + + if ( integers1 == integers2 ) + cout << "integers1 and integers2 are equal\n"; + + // use overloaded subscript operator to create rvalue + cout << "\nintegers1[5] is " << integers1[ 5 ]; + + // use overloaded subscript operator to create lvalue + cout << "\n\nAssigning 1000 to integers1[5]\n"; + integers1[ 5 ] = 1000; + cout << "integers1:\n" << integers1; + + // attempt to use out-of-range subscript + cout << "\nAttempt to assign 1000 to integers1[15]" << endl; + integers1[ 15 ] = 1000; // ERROR: out of range + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch08/Fig08_07_09/Fig08_09.cpp b/Bachelor/Prog1/examples/ch08/Fig08_07_09/Fig08_09.cpp new file mode 100644 index 0000000..9a9d516 --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_07_09/Fig08_09.cpp @@ -0,0 +1,101 @@ +// Fig. 8.9: fig08_09.cpp +// String class test program. +#include + +using std::cout; +using std::endl; + +#include "string1.h" + +int main() +{ + String s1( "happy" ); + String s2( " birthday" ); + String s3; + + // test overloaded equality and relational operators + cout << "s1 is \"" << s1 << "\"; s2 is \"" << s2 + << "\"; s3 is \"" << s3 << '\"' + << "\n\nThe results of comparing s2 and s1:" + << "\ns2 == s1 yields " + << ( s2 == s1 ? "true" : "false" ) + << "\ns2 != s1 yields " + << ( s2 != s1 ? "true" : "false" ) + << "\ns2 > s1 yields " + << ( s2 > s1 ? "true" : "false" ) + << "\ns2 < s1 yields " + << ( s2 < s1 ? "true" : "false" ) + << "\ns2 >= s1 yields " + << ( s2 >= s1 ? "true" : "false" ) + << "\ns2 <= s1 yields " + << ( s2 <= s1 ? "true" : "false" ); + + // test overloaded String empty (!) operator + cout << "\n\nTesting !s3:\n"; + + if ( !s3 ) { + cout << "s3 is empty; assigning s1 to s3;\n"; + s3 = s1; // test overloaded assignment + cout << "s3 is \"" << s3 << "\""; + } + + // test overloaded String concatenation operator + cout << "\n\ns1 += s2 yields s1 = "; + s1 += s2; // test overloaded concatenation + cout << s1; + + // test conversion constructor + cout << "\n\ns1 += \" to you\" yields\n"; + s1 += " to you"; // test conversion constructor + cout << "s1 = " << s1 << "\n\n"; + + // test overloaded function call operator () for substring + cout << "The substring of s1 starting at\n" + << "location 0 for 14 characters, s1(0, 14), is:\n" + << s1( 0, 14 ) << "\n\n"; + + // test substring "to-end-of-String" option + cout << "The substring of s1 starting at\n" + << "location 15, s1(15, 0), is: " + << s1( 15, 0 ) << "\n\n"; // 0 is "to end of string" + + // test copy constructor + String *s4Ptr = new String( s1 ); + cout << "\n*s4Ptr = " << *s4Ptr << "\n\n"; + + // test assignment (=) operator with self-assignment + cout << "assigning *s4Ptr to *s4Ptr\n"; + *s4Ptr = *s4Ptr; // test overloaded assignment + cout << "*s4Ptr = " << *s4Ptr << '\n'; + + // test destructor + delete s4Ptr; + + // test using subscript operator to create lvalue + s1[ 0 ] = 'H'; + s1[ 6 ] = 'B'; + cout << "\ns1 after s1[0] = 'H' and s1[6] = 'B' is: " + << s1 << "\n\n"; + + // test subscript out of range + cout << "Attempt to assign 'd' to s1[30] yields:" << endl; + s1[ 30 ] = 'd'; // ERROR: subscript out of range + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch08/Fig08_07_09/String1.cpp b/Bachelor/Prog1/examples/ch08/Fig08_07_09/String1.cpp new file mode 100644 index 0000000..7cca6d7 --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_07_09/String1.cpp @@ -0,0 +1,221 @@ +// Fig. 8.8: string1.cpp +// Member function definitions for class String. +#include + +using std::cout; +using std::endl; + +#include + +using std::setw; + +#include // C++ standard "new" operator + +#include // strcpy and strcat prototypes +#include // exit prototype + +#include "string1.h" // String class definition + +// conversion constructor converts char * to String +String::String( const char *s ) + : length( ( s != 0 ) ? strlen( s ) : 0 ) +{ + cout << "Conversion constructor: " << s << '\n'; + setString( s ); // call utility function + +} // end String conversion constructor + +// copy constructor +String::String( const String © ) + : length( copy.length ) +{ + cout << "Copy constructor: " << copy.sPtr << '\n'; + setString( copy.sPtr ); // call utility function + +} // end String copy constructor + +// Destructor +String::~String() +{ + cout << "Destructor: " << sPtr << '\n'; + delete [] sPtr; // reclaim string + +} // end ~String destructor + +// overloaded = operator; avoids self assignment +const String &String::operator=( const String &right ) +{ + cout << "operator= called\n"; + + if ( &right != this ) { // avoid self assignment + delete [] sPtr; // prevents memory leak + length = right.length; // new String length + setString( right.sPtr ); // call utility function + } + + else + cout << "Attempted assignment of a String to itself\n"; + + return *this; // enables cascaded assignments + +} // end function operator= + +// concatenate right operand to this object and +// store in this object. +const String &String::operator+=( const String &right ) +{ + size_t newLength = length + right.length; // new length + char *tempPtr = new char[ newLength + 1 ]; // create memory + + strcpy( tempPtr, sPtr ); // copy sPtr + strcpy( tempPtr + length, right.sPtr ); // copy right.sPtr + + delete [] sPtr; // reclaim old space + sPtr = tempPtr; // assign new array to sPtr + length = newLength; // assign new length to length + + return *this; // enables cascaded calls + +} // end function operator+= + +// is this String empty? +bool String::operator!() const +{ + return length == 0; + +} // end function operator! + +// Is this String equal to right String? +bool String::operator==( const String &right ) const +{ + return strcmp( sPtr, right.sPtr ) == 0; + +} // end function operator== + +// Is this String less than right String? +bool String::operator<( const String &right ) const +{ + return strcmp( sPtr, right.sPtr ) < 0; + +} // end function operator< + +// return reference to character in String as lvalue +char &String::operator[]( int subscript ) +{ + // test for subscript out of range + if( subscript < 0 || subscript >= length ) { + cout << "Error: Subscript " << subscript + << " out of range" << endl; + + exit( 1 ); // terminate program + } + + return sPtr[ subscript ]; // creates lvalue + +} // end function operator[] + +// return reference to character in String as rvalue +const char &String::operator[]( int subscript ) const +{ + // test for subscript out of range + if( subscript < 0 || subscript >= length ) { + cout << "Error: Subscript " << subscript + << " out of range" << endl; + + exit( 1 ); // terminate program + } + + return sPtr[ subscript ]; // creates rvalue + +} // end function operator[] + +// return a substring beginning at index and +// of length subLength +String String::operator()( int index, int subLength ) +{ + // if index is out of range or substring length < 0, + // return an empty String object + if ( index < 0 || index >= length || subLength < 0 ) + return ""; // converted to a String object automatically + + // determine length of substring + int len; + + if ( ( subLength == 0 ) || ( index + subLength > length ) ) + len = length - index; + else + len = subLength; + + // allocate temporary array for substring and + // terminating null character + char *tempPtr = new char[ len + 1 ]; + + // copy substring into char array and terminate string + strncpy( tempPtr, &sPtr[ index ], len ); + tempPtr[ len ] = '\0'; + + // create temporary String object containing the substring + String tempString( tempPtr ); + delete [] tempPtr; // delete temporary array + + return tempString; // return copy of the temporary String + +} // end function operator() + +// return string length +int String::getLength() const +{ + return length; + +} // end function getLenth + +// utility function called by constructors and operator= +void String::setString( const char *string2 ) +{ + sPtr = new char[ length + 1 ]; // allocate memory + + // if string2 is not a null pointer, copy contents + if ( string2 != 0 ) + strcpy( sPtr, string2 ); // copy literal to object + + // if string2 is a null pointer, make this an empty string + else + sPtr[ 0 ] = '\0'; // empty string + +} // end function setString + +// overloaded output operator +ostream &operator<<( ostream &output, const String &s ) +{ + output << s.sPtr; + + return output; // enables cascading + +} // end function operator<< + +// overloaded input operator +istream &operator>>( istream &input, String &s ) +{ + char temp[ 100 ]; // buffer to store input + + input >> setw( 100 ) >> temp; + s = temp; // use String class assignment operator + + return input; // enables cascading + +} // end function operator>> + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch08/Fig08_07_09/String1.h b/Bachelor/Prog1/examples/ch08/Fig08_07_09/String1.h new file mode 100644 index 0000000..81012aa --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_07_09/String1.h @@ -0,0 +1,85 @@ +// Fig. 8.7: string1.h +// String class definition. +#ifndef STRING1_H +#define STRING1_H + +#include + +using std::ostream; +using std::istream; + +class String { + friend ostream &operator<<( ostream &, const String & ); + friend istream &operator>>( istream &, String & ); + +public: + String( const char * = "" ); // conversion/default ctor + String( const String & ); // copy constructor + ~String(); // destructor + + const String &operator=( const String & ); // assignment + const String &operator+=( const String & ); // concatenation + + bool operator!() const; // is String empty? + bool operator==( const String & ) const; // test s1 == s2 + bool operator<( const String & ) const; // test s1 < s2 + + // test s1 != s2 + bool operator!=( const String & right ) const + { + return !( *this == right ); + + } // end function operator!= + + // test s1 > s2 + bool operator>( const String &right ) const + { + return right < *this; + + } // end function operator> + + // test s1 <= s2 + bool operator<=( const String &right ) const + { + return !( right < *this ); + + } // end function operator <= + + // test s1 >= s2 + bool operator>=( const String &right ) const + { + return !( *this < right ); + + } // end function operator>= + + char &operator[]( int ); // subscript operator + const char &operator[]( int ) const; // subscript operator + + String operator()( int, int ); // return a substring + + int getLength() const; // return string length + +private: + int length; // string length + char *sPtr; // pointer to start of string + + void setString( const char * ); // utility function + +}; // end class String + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch08/Fig08_10_12/Date1.cpp b/Bachelor/Prog1/examples/ch08/Fig08_10_12/Date1.cpp new file mode 100644 index 0000000..0c96a95 --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_10_12/Date1.cpp @@ -0,0 +1,137 @@ +// Fig. 8.11: date1.cpp +// Date class member function definitions. +#include +#include "date1.h" + +// initialize static member at file scope; +// one class-wide copy +const int Date::days[] = + { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; + +// Date constructor +Date::Date( int m, int d, int y ) +{ + setDate( m, d, y ); + +} // end Date constructor + +// set month, day and year +void Date::setDate( int mm, int dd, int yy ) +{ + month = ( mm >= 1 && mm <= 12 ) ? mm : 1; + year = ( yy >= 1900 && yy <= 2100 ) ? yy : 1900; + + // test for a leap year + if ( month == 2 && leapYear( year ) ) + day = ( dd >= 1 && dd <= 29 ) ? dd : 1; + else + day = ( dd >= 1 && dd <= days[ month ] ) ? dd : 1; + +} // end function setDate + +// overloaded preincrement operator +Date &Date::operator++() +{ + helpIncrement(); + + return *this; // reference return to create an lvalue + +} // end function operator++ + +// overloaded postincrement operator; note that the dummy +// integer parameter does not have a parameter name +Date Date::operator++( int ) +{ + Date temp = *this; + helpIncrement(); // hold current state of object + + // return unincremented, saved, temporary object + return temp; // value return; not a reference return + +} // end function operator++ + +// add specified number of days to date +const Date &Date::operator+=( int additionalDays ) +{ + for ( int i = 0; i < additionalDays; i++ ) + helpIncrement(); + + return *this; // enables cascading + +} // end function operator+= + +// if the year is a leap year, return true; +// otherwise, return false +bool Date::leapYear( int testYear ) const +{ + if ( testYear % 400 == 0 || + ( testYear % 100 != 0 && testYear % 4 == 0 ) ) + return true; // a leap year + else + return false; // not a leap year + +} // end function leapYear + +// determine whether the day is the last day of the month +bool Date::endOfMonth( int testDay ) const +{ + if ( month == 2 && leapYear( year ) ) + return testDay == 29; // last day of Feb. in leap year + else + return testDay == days[ month ]; + +} // end function endOfMonth + +// function to help increment the date +void Date::helpIncrement() +{ + // day is not end of month + if ( !endOfMonth( day ) ) + ++day; + + else + + // day is end of month and month < 12 + if ( month < 12 ) { + ++month; + day = 1; + } + + // last day of year + else { + ++year; + month = 1; + day = 1; + } + +} // end function helpIncrement + +// overloaded output operator +ostream &operator<<( ostream &output, const Date &d ) +{ + static char *monthName[ 13 ] = { "", "January", + "February", "March", "April", "May", "June", + "July", "August", "September", "October", + "November", "December" }; + + output << monthName[ d.month ] << ' ' + << d.day << ", " << d.year; + + return output; // enables cascading + +} // end function operator<< + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch08/Fig08_10_12/Date1.h b/Bachelor/Prog1/examples/ch08/Fig08_10_12/Date1.h new file mode 100644 index 0000000..d1d6148 --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_10_12/Date1.h @@ -0,0 +1,49 @@ +// Fig. 8.10: date1.h +// Date class definition. +#ifndef DATE1_H +#define DATE1_H +#include + +using std::ostream; + +class Date { + friend ostream &operator<<( ostream &, const Date & ); + +public: + Date( int m = 1, int d = 1, int y = 1900 ); // constructor + void setDate( int, int, int ); // set the date + + Date &operator++(); // preincrement operator + Date operator++( int ); // postincrement operator + + const Date &operator+=( int ); // add days, modify object + + bool leapYear( int ) const; // is this a leap year? + bool endOfMonth( int ) const; // is this end of month? + +private: + int month; + int day; + int year; + + static const int days[]; // array of days per month + void helpIncrement(); // utility function + +}; // end class Date + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch08/Fig08_10_12/Fig08_12.cpp b/Bachelor/Prog1/examples/ch08/Fig08_10_12/Fig08_12.cpp new file mode 100644 index 0000000..0d4c996 --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_10_12/Fig08_12.cpp @@ -0,0 +1,54 @@ +// Fig. 8.12: fig08_12.cpp +// Date class test program. +#include + +using std::cout; +using std::endl; + +#include "date1.h" // Date class definition + +int main() +{ + Date d1; // defaults to January 1, 1900 + Date d2( 12, 27, 1992 ); + Date d3( 0, 99, 8045 ); // invalid date + + cout << "d1 is " << d1 << "\nd2 is " << d2 + << "\nd3 is " << d3; + + cout << "\n\nd2 += 7 is " << ( d2 += 7 ); + + d3.setDate( 2, 28, 1992 ); + cout << "\n\n d3 is " << d3; + cout << "\n++d3 is " << ++d3; + + Date d4( 7, 13, 2002 ); + + cout << "\n\nTesting the preincrement operator:\n" + << " d4 is " << d4 << '\n'; + cout << "++d4 is " << ++d4 << '\n'; + cout << " d4 is " << d4; + + cout << "\n\nTesting the postincrement operator:\n" + << " d4 is " << d4 << '\n'; + cout << "d4++ is " << d4++ << '\n'; + cout << " d4 is " << d4 << endl; + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch08/Fig08_13/Fig08_13.cpp b/Bachelor/Prog1/examples/ch08/Fig08_13/Fig08_13.cpp new file mode 100644 index 0000000..cd74972 --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_13/Fig08_13.cpp @@ -0,0 +1,104 @@ +// Fig. 8.13: fig08_13.cpp +// Standard library string class test program. +#include + +using std::cout; +using std::endl; + +#include + +using std::string; + +int main() +{ + string s1( "happy" ); + string s2( " birthday" ); + string s3; + + // test overloaded equality and relational operators + cout << "s1 is \"" << s1 << "\"; s2 is \"" << s2 + << "\"; s3 is \"" << s3 << '\"' + << "\n\nThe results of comparing s2 and s1:" + << "\ns2 == s1 yields " + << ( s2 == s1 ? "true" : "false" ) + << "\ns2 != s1 yields " + << ( s2 != s1 ? "true" : "false" ) + << "\ns2 > s1 yields " + << ( s2 > s1 ? "true" : "false" ) + << "\ns2 < s1 yields " + << ( s2 < s1 ? "true" : "false" ) + << "\ns2 >= s1 yields " + << ( s2 >= s1 ? "true" : "false" ) + << "\ns2 <= s1 yields " + << ( s2 <= s1 ? "true" : "false" ); + + // test string member function empty + cout << "\n\nTesting s3.empty():\n"; + + if ( s3.empty() ) { + cout << "s3 is empty; assigning s1 to s3;\n"; + s3 = s1; // assign s1 to s3 + cout << "s3 is \"" << s3 << "\""; + } + + // test overloaded string concatenation operator + cout << "\n\ns1 += s2 yields s1 = "; + s1 += s2; // test overloaded concatenation + cout << s1; + + // test overloaded string concatenation operator + // with C-style string + cout << "\n\ns1 += \" to you\" yields\n"; + s1 += " to you"; + cout << "s1 = " << s1 << "\n\n"; + + // test string member function substr + cout << "The substring of s1 starting at location 0 for\n" + << "14 characters, s1.substr(0, 14), is:\n" + << s1.substr( 0, 14 ) << "\n\n"; + + // test substr "to-end-of-string" option + cout << "The substring of s1 starting at\n" + << "location 15, s1.substr(15), is:\n" + << s1.substr( 15 ) << '\n'; + + // test copy constructor + string *s4Ptr = new string( s1 ); + cout << "\n*s4Ptr = " << *s4Ptr << "\n\n"; + + // test assignment (=) operator with self-assignment + cout << "assigning *s4Ptr to *s4Ptr\n"; + *s4Ptr = *s4Ptr; + cout << "*s4Ptr = " << *s4Ptr << '\n'; + + // test destructor + delete s4Ptr; + + // test using subscript operator to create lvalue + s1[ 0 ] = 'H'; + s1[ 6 ] = 'B'; + cout << "\ns1 after s1[0] = 'H' and s1[6] = 'B' is: " + << s1 << "\n\n"; + + // test subscript out of range with string member function "at" + cout << "Attempt to assign 'd' to s1.at( 30 ) yields:" << endl; + s1.at( 30 ) = 'd'; // ERROR: subscript out of range + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch08/Fig08_14/Fig08_14.cpp b/Bachelor/Prog1/examples/ch08/Fig08_14/Fig08_14.cpp new file mode 100644 index 0000000..7fda752 --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_14/Fig08_14.cpp @@ -0,0 +1,134 @@ +// Fig. 8.14: fig08_14.cpp +// Demonstrating standard library class vector. +#include + +using std::cout; +using std::cin; +using std::endl; + +#include + +using std::setw; + +#include + +using std::vector; + +void outputVector( const vector< int > & ); +void inputVector( vector< int > & ); + +int main() +{ + vector< int > integers1( 7 ); // 7-element vector< int > + vector< int > integers2( 10 ); // 10-element vector< int > + + // print integers1 size and contents + cout << "Size of vector integers1 is " + << integers1.size() + << "\nvector after initialization:\n"; + outputVector( integers1 ); + + // print integers2 size and contents + cout << "\nSize of vector integers2 is " + << integers2.size() + << "\nvector after initialization:\n"; + outputVector( integers2 ); + + // input and print integers1 and integers2 + cout << "\nInput 17 integers:\n"; + inputVector( integers1 ); + inputVector( integers2 ); + + cout << "\nAfter input, the vectors contain:\n" + << "integers1:\n"; + outputVector( integers1 ); + cout << "integers2:\n"; + outputVector( integers2 ); + + // use overloaded inequality (!=) operator + cout << "\nEvaluating: integers1 != integers2\n"; + + if ( integers1 != integers2 ) + cout << "integers1 and integers2 are not equal\n"; + + // create vector integers3 using integers1 as an + // initializer; print size and contents + vector< int > integers3( integers1 ); // copy constructor + + cout << "\nSize of vector integers3 is " + << integers3.size() + << "\nvector after initialization:\n"; + outputVector( integers3 ); + + + // use overloaded assignment (=) operator + cout << "\nAssigning integers2 to integers1:\n"; + integers1 = integers2; + + cout << "integers1:\n"; + outputVector( integers1 ); + cout << "integers2:\n"; + outputVector( integers1 ); + + // use overloaded equality (==) operator + cout << "\nEvaluating: integers1 == integers2\n"; + + if ( integers1 == integers2 ) + cout << "integers1 and integers2 are equal\n"; + + // use overloaded subscript operator to create rvalue + cout << "\nintegers1[5] is " << integers1[ 5 ]; + + // use overloaded subscript operator to create lvalue + cout << "\n\nAssigning 1000 to integers1[5]\n"; + integers1[ 5 ] = 1000; + cout << "integers1:\n"; + outputVector( integers1 ); + + // attempt to use out of range subscript + cout << "\nAttempt to assign 1000 to integers1.at( 15 )" + << endl; + integers1.at( 15 ) = 1000; // ERROR: out of range + + return 0; + +} // end main + +// output vector contents +void outputVector( const vector< int > &array ) +{ + for ( int i = 0; i < array.size(); i++ ) { + cout << setw( 12 ) << array[ i ]; + + if ( ( i + 1 ) % 4 == 0 ) // 4 numbers per row of output + cout << endl; + + } // end for + + if ( i % 4 != 0 ) + cout << endl; + +} // end function outputVector + +// input vector contents +void inputVector( vector< int > &array ) +{ + for ( int i = 0; i < array.size(); i++ ) + cin >> array[ i ]; + +} // end function inputVector + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch08/Fig08_15_17/Complex1.cpp b/Bachelor/Prog1/examples/ch08/Fig08_15_17/Complex1.cpp new file mode 100644 index 0000000..130d39e --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_15_17/Complex1.cpp @@ -0,0 +1,54 @@ +// Fig. 8.16: complex1.cpp +// Complex class member function definitions. +#include + +using std::cout; + +#include "complex1.h" // Complex class definition + +// Constructor +Complex::Complex( double realPart, double imaginaryPart ) + : real( realPart ), + imaginary( imaginaryPart ) +{ + // empty body + +} // end Complex constructor + +// addition operator +Complex Complex::operator+( const Complex &operand2 ) const +{ + return Complex( real + operand2.real, + imaginary + operand2.imaginary ); + +} // end function operator+ + +// subtraction operator +Complex Complex::operator-( const Complex &operand2 ) const +{ + return Complex( real - operand2.real, + imaginary - operand2.imaginary ); + +} // end function operator- + +// display a Complex object in the form: (a, b) +void Complex::print() const +{ + cout << '(' << real << ", " << imaginary << ')'; + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch08/Fig08_15_17/Complex1.h b/Bachelor/Prog1/examples/ch08/Fig08_15_17/Complex1.h new file mode 100644 index 0000000..934f2f2 --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_15_17/Complex1.h @@ -0,0 +1,35 @@ +// Fig. 8.15: complex1.h +// Complex class definition. +#ifndef COMPLEX1_H +#define COMPLEX1_H + +class Complex { + +public: + Complex( double = 0.0, double = 0.0 ); // constructor + Complex operator+( const Complex & ) const; // addition + Complex operator-( const Complex & ) const; // subtraction + void print() const; // output + +private: + double real; // real part + double imaginary; // imaginary part + +}; // end class Complex + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch08/Fig08_15_17/Fig08_17.cpp b/Bachelor/Prog1/examples/ch08/Fig08_15_17/Fig08_17.cpp new file mode 100644 index 0000000..d98f9bb --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_15_17/Fig08_17.cpp @@ -0,0 +1,57 @@ +// Fig. 8.17: fig08_17.cpp +// Complex class test program. +#include + +using std::cout; +using std::endl; + +#include "complex1.h" + +int main() +{ + Complex x; + Complex y( 4.3, 8.2 ); + Complex z( 3.3, 1.1 ); + + cout << "x: "; + x.print(); + cout << "\ny: "; + y.print(); + cout << "\nz: "; + z.print(); + + x = y + z; + cout << "\n\nx = y + z:\n"; + x.print(); + cout << " = "; + y.print(); + cout << " + "; + z.print(); + + x = y - z; + cout << "\n\nx = y - z:\n"; + x.print(); + cout << " = "; + y.print(); + cout << " - "; + z.print(); + cout << endl; + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch08/Fig08_18_20/Fig08_20.cpp b/Bachelor/Prog1/examples/ch08/Fig08_18_20/Fig08_20.cpp new file mode 100644 index 0000000..f73bde7 --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_18_20/Fig08_20.cpp @@ -0,0 +1,51 @@ +// Fig. 8.20: fig08_20.cpp +// HugeInt test program. +#include + +using std::cout; +using std::endl; + +#include "hugeint1.h" + +int main() +{ + HugeInt n1( 7654321 ); + HugeInt n2( 7891234 ); + HugeInt n3( "99999999999999999999999999999" ); + HugeInt n4( "1" ); + HugeInt n5; + + cout << "n1 is " << n1 << "\nn2 is " << n2 + << "\nn3 is " << n3 << "\nn4 is " << n4 + << "\nn5 is " << n5 << "\n\n"; + + n5 = n1 + n2; + cout << n1 << " + " << n2 << " = " << n5 << "\n\n"; + + cout << n3 << " + " << n4 << "\n= " << ( n3 + n4 ) + << "\n\n"; + + n5 = n1 + 9; + cout << n1 << " + " << 9 << " = " << n5 << "\n\n"; + + n5 = n2 + "10000"; + cout << n2 << " + " << "10000" << " = " << n5 << endl; + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch08/Fig08_18_20/Hugeint1.cpp b/Bachelor/Prog1/examples/ch08/Fig08_18_20/Hugeint1.cpp new file mode 100644 index 0000000..6e1aff7 --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_18_20/Hugeint1.cpp @@ -0,0 +1,121 @@ +// Fig. 8.19: hugeint1.cpp +// HugeInt member-function and friend-function definitions. + +#include // isdigit function prototype +#include // strlen function prototype + +#include "hugeint1.h" // HugeInt class definition + +// default constructor; conversion constructor that converts +// a long integer into a HugeInt object +HugeInt::HugeInt( long value ) +{ + // initialize array to zero + for ( int i = 0; i <= 29; i++ ) + integer[ i ] = 0; + + // place digits of argument into array + for ( int j = 29; value != 0 && j >= 0; j-- ) { + integer[ j ] = value % 10; + value /= 10; + + } // end for + +} // end HugeInt default/conversion constructor + +// conversion constructor that converts a character string +// representing a large integer into a HugeInt object +HugeInt::HugeInt( const char *string ) +{ + // initialize array to zero + for ( int i = 0; i <= 29; i++ ) + integer[ i ] = 0; + + // place digits of argument into array + int length = strlen( string ); + + for ( int j = 30 - length, k = 0; j <= 29; j++, k++ ) + + if ( isdigit( string[ k ] ) ) + integer[ j ] = string[ k ] - '0'; + +} // end HugeInt conversion constructor + +// addition operator; HugeInt + HugeInt +HugeInt HugeInt::operator+( const HugeInt &op2 ) +{ + HugeInt temp; // temporary result + int carry = 0; + + for ( int i = 29; i >= 0; i-- ) { + temp.integer[ i ] = + integer[ i ] + op2.integer[ i ] + carry; + + // determine whether to carry a 1 + if ( temp.integer[ i ] > 9 ) { + temp.integer[ i ] %= 10; // reduce to 0-9 + carry = 1; + + } // end if + + // no carry + else + carry = 0; + } + + return temp; // return copy of temporary object + +} // end function operator+ + +// addition operator; HugeInt + int +HugeInt HugeInt::operator+( int op2 ) +{ + // convert op2 to a HugeInt, then invoke + // operator+ for two HugeInt objects + return *this + HugeInt( op2 ); + +} // end function operator+ + +// addition operator; +// HugeInt + string that represents large integer value +HugeInt HugeInt::operator+( const char *op2 ) +{ + // convert op2 to a HugeInt, then invoke + // operator+ for two HugeInt objects + return *this + HugeInt( op2 ); + +} // end operator+ + +// overloaded output operator +ostream& operator<<( ostream &output, const HugeInt &num ) +{ + int i; + + for ( i = 0; ( num.integer[ i ] == 0 ) && ( i <= 29 ); i++ ) + ; // skip leading zeros + + if ( i == 30 ) + output << 0; + else + + for ( ; i <= 29; i++ ) + output << num.integer[ i ]; + + return output; + +} // end function operator<< + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch08/Fig08_18_20/Hugeint1.h b/Bachelor/Prog1/examples/ch08/Fig08_18_20/Hugeint1.h new file mode 100644 index 0000000..46dd1df --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_18_20/Hugeint1.h @@ -0,0 +1,47 @@ +// Fig. 8.18: hugeint1.h +// HugeInt class definition. +#ifndef HUGEINT1_H +#define HUGEINT1_H + +#include + +using std::ostream; + +class HugeInt { + friend ostream &operator<<( ostream &, const HugeInt & ); + +public: + HugeInt( long = 0 ); // conversion/default constructor + HugeInt( const char * ); // conversion constructor + + // addition operator; HugeInt + HugeInt + HugeInt operator+( const HugeInt & ); + + // addition operator; HugeInt + int + HugeInt operator+( int ); + + // addition operator; + // HugeInt + string that represents large integer value + HugeInt operator+( const char * ); + +private: + short integer[ 30 ]; + +}; // end class HugeInt + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file -- cgit v1.2.3