fork download
  1. //
  2. //main.cpp
  3. //CollectionRefactoring_04
  4. //
  5. //
  6.  
  7. #include <iostream>
  8. #include "Collection"
  9.  
  10. typedef bool ( *Condition )(Type item) ;
  11.  
  12. //
  13. //user defined condition...
  14. //
  15. bool lessThanFive(Type item)
  16. {
  17. return item < 5 ;
  18. }
  19.  
  20. bool DivisibleByTwo(Type item)
  21. {
  22. return ( item % 2 == 0 ) ;
  23. }
  24.  
  25. //
  26. //Utility function Dump
  27. //
  28. void CollectionDump( Collection thisCollection )
  29. {
  30. Type* item = CollectionFirst( thisCollection ) ;
  31. if( item )
  32. {
  33. std::cout << "Collection ( " <<
  34. CollectionCount( thisCollection ) << " items )" << std::endl ;
  35. while ( item )
  36. {
  37. std::cout << ( *item ) << " " ;
  38. item = CollectionNext( thisCollection ) ;
  39. }
  40. std::cout << std::endl ;
  41. }else
  42. {
  43. std::cout << " Collection Empty " << std::endl ;
  44. }
  45. }
  46.  
  47. //
  48. //Select Function
  49. //
  50. void Select( Collection input, Condition condition, Collection result )
  51. {
  52. Type* item = CollectionFirst( input ) ;
  53. while ( item )
  54. {
  55. if ( condition( *item ) ) CollectionAdd( result, *item ) ;
  56. item = CollectionNext( input ) ;
  57. }
  58.  
  59. }
  60.  
  61.  
  62. int main(int argc, const char * argv[])
  63. {
  64. Type data[] = {
  65. 2,3,5,9,6,23,4,10,12,44,13,1,22
  66. } ;
  67.  
  68. int sizeOfData = sizeof(data) / sizeof(Type ) ;
  69.  
  70. Collection collection ;
  71. CollectionInitialize( collection ) ;
  72.  
  73. CollectionCreateWith( collection, data, sizeOfData ) ;
  74. CollectionDump( collection ) ;
  75.  
  76. CollectionResult ;
  77. CollectionInitialize( result ) ;
  78. CollectionDump( result ) ;
  79.  
  80. Select ( collection, LessThanFive, result ) ;
  81.  
  82. CollectionDump( result );
  83.  
  84. CollectionRelease( result ) ;
  85. select( collection, DivisibleByTwo, result ) ;
  86. CollectionDump( result ) ;
  87.  
  88. return 0;
  89. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:8:22: fatal error: Collection: No such file or directory
 #include "Collection"
                      ^
compilation terminated.
stdout
Standard output is empty