📄 chararraycomparison.cpp
字号:
// Exercise 22.16: CharacterArrayComparison.cpp
// Overload comparison operators for the user-defined CharArray class
#include <iostream> // required to perform C++ stream I/O
#include <string> // required to access string functions
#include "CharArray.h" // CharArray class definition
using namespace std; // for accessing C++ Standard Library members
// overloaded stream insertion operator
ostream &operator<<( ostream &, CharArray & );
// overloaded stream extraction operator
istream &operator>>( istream &, CharArray & );
int main()
{
// define variables
CharArray text1; // first character array that the user inputs
CharArray text2; // second character array that the user inputs
// prompt user for and input first character array
cout << "\nEnter a character array: ";
cin >> text1;
// prompt user for and input second character array
cout << "Enter a second character array: ";
cin >> text2;
// compare character arrays
if ( text1 == text2 )
{
cout << "\nThe two character arrays are equal." << endl;
} // end if
else if ( text1 > text2 )
{
cout << "\n" << text1 << " is greater than " << text2 << endl;
} // end else if
else
{
cout << "\n" << text1 << " is less than " << text2 << endl;
} // end else
cout << endl;
return 0; // indicate that program ended successfully
} // end main
// overloaded stream-insertion operator
ostream &operator<<( ostream &output, CharArray &myArray )
{
for ( int i = 0; i < myArray.getLength(); i++ )
{
output << myArray.getChar( i );
} // end for
return output; // enables cout << a << b << c;
} // end function operator<<
// overloaded stream-extraction operator
istream &operator>>( istream &input, CharArray &myArray )
{
string buffer; // temporarily store input
// get input from input stream
getline( input, buffer );
myArray.setLength( static_cast< int > ( buffer.length() ) );
// place input in myArray's array
for ( int i = 0; ( i < buffer.length() ); i++ )
{
myArray.setChar( buffer[ i ], i );
} // end for
return input; // enables cin >> a >> b >> c;
} // end function operator>>
/**************************************************************************
* (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. 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. *
**************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -