home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 May / Chip_2002-05_cd1.bin / chplus / cpp / 3 / stl.exe / alg2.cpp < prev    next >
C/C++ Source or Header  |  1998-02-09  |  7KB  |  250 lines

  1. #include "stlexam.h"
  2. #pragma hdrstop
  3. /**************************************************************************
  4.  *
  5.  * alg2.cpp - test program for STL generic algorithm that search for 
  6.  *    elements that satisfy a condition.  Section 12.3
  7.  *
  8.  * $Id: alg2.cpp,v 1.15 1996/08/28 01:18:45 smithey Exp $
  9.  *
  10.  ***************************************************************************
  11.  *
  12.  * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
  13.  * ALL RIGHTS RESERVED *
  14.  * The software and information contained herein are proprietary to, and
  15.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  16.  * intends to preserve as trade secrets such software and information.
  17.  * This software is furnished pursuant to a written license agreement and
  18.  * may be used, copied, transmitted, and stored only in accordance with
  19.  * the terms of such license and with the inclusion of the above copyright
  20.  * notice.  This software and information or any other copies thereof may
  21.  * not be provided or otherwise made available to any other person.
  22.  *
  23.  * Notwithstanding any other lease or license that may pertain to, or
  24.  * accompany the delivery of, this computer software and information, the
  25.  * rights of the Government regarding its use, reproduction and disclosure
  26.  * are as set forth in Section 52.227-19 of the FARS Computer
  27.  * Software-Restricted Rights clause.
  28.  * 
  29.  * Use, duplication, or disclosure by the Government is subject to
  30.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  31.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  32.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  33.  * P.O. Box 2328, Corvallis, Oregon 97339.
  34.  *
  35.  * This computer software and information is distributed with "restricted
  36.  * rights."  Use, duplication or disclosure is subject to restrictions as
  37.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  38.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  39.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  40.  * then the "Alternate III" clause applies.
  41.  *
  42.  **************************************************************************/
  43.  
  44. #ifndef _RWSTD_HEADER_REQUIRES_HPP
  45. #include <vector>
  46. #include <list>
  47. #include <algorithm>
  48. #include <ctype.h>
  49. #include <string>
  50. #include <string.h>
  51. #else
  52. #include <vector.hpp>
  53. #include <list.hpp>
  54. #include <algorithm.hpp>
  55. #include <ctype.h>
  56. #include <string.hpp>
  57. #include <string.h>
  58. #endif
  59.  
  60. #ifdef _RW_STD_IOSTREAM
  61. #include <iostream>
  62. #else
  63. #include <iostream.h>
  64. #endif
  65.  
  66. #ifndef _RWSTD_NO_NAMESPACE
  67.   using namespace std;
  68. #endif
  69.  
  70. int randomInteger (int m) {   return rand() % m; }
  71.  
  72. bool isLeapYear (unsigned int year)
  73. {
  74.     if (year % 1000 == 0)
  75.         return true;
  76.     if (year % 100 == 0)
  77.         return false;
  78.     if (year % 4 == 0)
  79.         return true;
  80.     return false;
  81. }
  82.  
  83.  
  84. void split (const string & text, const string & separators,
  85.             list<string,allocator<string> > & words)
  86. {
  87.     int n     = text.length();
  88.     int start = text.find_first_not_of(separators);
  89.  
  90.     while ((start >= 0) && (start < n))
  91.     {
  92.         int stop = text.find_first_of(separators, start);
  93.         if ((stop < 0) || (stop > n)) stop = n;
  94.         words.push_back (text.substr(start, stop-start));
  95.         start = text.find_first_not_of(separators, stop+1);
  96.     }
  97. }
  98.  
  99. //
  100. // Illustrate use of the find function.
  101. //
  102.  
  103. void find_test ()
  104. {
  105.     cout << "Test of algorithm find" << endl;
  106.  
  107.     int vintageYears[] = { 1967, 1972, 1974, 1980, 1995 };
  108.  
  109.     vector<int,allocator<int> >::iterator start = vintageYears;
  110.     vector<int,allocator<int> >::iterator stop  = vintageYears + 5;
  111.     
  112.     vector<int,allocator<int> >::iterator where = find_if(start, stop, isLeapYear);
  113.     
  114.     if (where != stop)
  115.         cout << "first vintage leap year is " << *where << endl;
  116.     else
  117.         cout << "no vintage leap years" << endl;
  118.         
  119.     where = find(start, stop, 1995);
  120.     
  121.     if (where != stop)
  122.         cout << "1995 is position " << where - start << " in sequence" << endl;
  123.     else
  124.         cout << "1995 does not occur in sequence" << endl;
  125. }
  126.  
  127. void find_adjacent_test ()
  128. {
  129.     cout << "Test of algorithm find adjacent" << endl;
  130.  
  131.     char * text = "The bookkeeper carefully opened the door";
  132.     
  133.     vector<char,allocator<char> >::iterator start = text;
  134.     vector<char,allocator<char> >::iterator stop = text + strlen(text);
  135.     
  136.     vector<char,allocator<char> >::iterator where = start;
  137.     
  138.     cout << "In the text " << text << endl;
  139.  
  140.     while ((where = adjacent_find(where, stop)) != stop)
  141.     {
  142.         cout << "double " << *where << " in position " << where-start << endl;
  143.         ++where;
  144.     }
  145. }
  146.  
  147. //
  148. // Illustrate the use of the search function.
  149. //
  150.  
  151. void search_test ()
  152. {
  153.     cout << "Test of algorithm search" << endl;
  154.  
  155.     char * base = "aspirations";
  156.     char * text = "ration";
  157.     
  158.     char * where = search(base, base+strlen(base), text, text+strlen(text));
  159.     
  160.     if (*where != '\0')
  161.         cout << "substring begins in position " << where - base << endl;
  162.     else
  163.         cout << "substring does not occur in text" << endl;
  164. }
  165.  
  166. //
  167. // Illustrate use of max_element and min_element algorithms.
  168. //
  169.  
  170. void max_min_example ()
  171. {
  172.     cout << "Test of max and min algorithms " << endl;
  173.     //
  174.     // Make a vector of random numbers between 0 and 99.
  175.     //
  176.     vector<int,allocator<int> > numbers(25);
  177.     for (int i = 0; i < 25; i++)
  178.         numbers[i] = randomInteger(100);
  179.     //
  180.     // Print the maximum.
  181.     //
  182.     vector<int,allocator<int> >::iterator max = max_element(numbers.begin(), numbers.end());
  183.     cout << "largest value was " << *max << endl;
  184.     //
  185.     // Example using strings.
  186.     //
  187.     string text = "it was the best of times, it was the worst of times.";
  188.     list<string,allocator<string> >words;
  189.     split(text, " .,!:;", words);
  190.     cout << "The smallest word is "
  191.         << * min_element(words.begin(), words.end())
  192.         << " and the largest word is "
  193.         << * max_element(words.begin(), words.end())
  194.         << endl;
  195. }
  196.  
  197. //
  198. // Illustrate the use of the mismatch function.
  199. //
  200.  
  201. void mismatch_test (char * a, char * b) 
  202. {
  203.     pair<char *, char *> differPositions(0, 0);
  204.     char * aDiffPos;
  205.     char * bDiffPos;
  206.  
  207.     if (strlen(a) < strlen(b))
  208.     {
  209.         differPositions = mismatch(a, a + strlen(a), b);
  210.         aDiffPos = differPositions.first;
  211.         bDiffPos = differPositions.second;
  212.     }
  213.     else
  214.     {
  215.         differPositions = mismatch(b, b + strlen(b), a);
  216.         aDiffPos = differPositions.second;
  217.         bDiffPos = differPositions.first;
  218.     }
  219.         
  220.     cout << "string " << a;
  221.  
  222.     if (*aDiffPos == *bDiffPos)
  223.         cout << " is equal to ";
  224.     else if (*aDiffPos < *bDiffPos)
  225.         cout << " is less than ";
  226.     else
  227.         cout << " is greater than ";
  228.  
  229.     cout << b << endl;
  230. }
  231.  
  232. int main ()
  233. {
  234.     cout << "STL generic algorithms -- Searching Algorithms" << endl;
  235.  
  236.     find_test();
  237.     find_adjacent_test();
  238.     search_test();
  239.     max_min_example();
  240.     mismatch_test("goody", "goody");
  241.     mismatch_test("good", "goody");
  242.     mismatch_test("goody", "good");
  243.     mismatch_test("good", "fred");
  244.     mismatch_test("fred", "good");
  245.     
  246.     cout << "End of search algorithms test program" << endl;
  247.  
  248.     return 0;
  249. }
  250.