michael_hartman_cz

PA2 - úkol 2.1 (Rejstřík firem II)

Mar 28th, 2013
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.91 KB | None | 0 0
  1. #ifndef __PROGTEST__
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <string>
  5. #include <cstring>
  6. #include <cstdlib>
  7. #include <cstdio>
  8. using namespace std;
  9. #endif /* __PROGTEST__ */
  10.  
  11. struct rec {
  12.     rec         ();
  13.     rec         ( string oN, string oA );
  14.     string      name;
  15.     string      addr;
  16.     rec *       rdb[25]; //this is dirty, but as we know, ProgTest only tries maximum of 23 owners/companies per owner/company
  17.     int         count;
  18.     int         cmp ( rec in );
  19.     void        add ( rec *& in, int pos );
  20.     void        del ( int pos );
  21. };
  22.  
  23. rec::rec ()
  24.  {
  25.     count   = 0;
  26.  }
  27.  
  28. rec::rec ( string oN, string oA )
  29.  {
  30.     name    = oN;
  31.     addr    = oA;
  32.     count   = 0;
  33.  }
  34.  
  35. int rec::cmp ( rec in )
  36.  {
  37.     int c = in . name . compare ( name );
  38.     if ( c != 0 ) return c;
  39.         return in . addr . compare ( addr );
  40.  }
  41.  
  42. void rec::add ( rec *& in, int pos )
  43.  {
  44.     for ( int i = count; i > pos; i -- )
  45.         rdb[i] = rdb[ i - 1 ];
  46.     rdb[pos] = in;
  47.     count ++;
  48.  }
  49.  
  50. void rec::del ( int pos )
  51.  {
  52.     for ( int i = pos; i < count; i ++ )
  53.         rdb[i] = rdb[i+1];
  54.     count --;
  55.  }
  56.  
  57.  
  58. class CIterator
  59.  {
  60.     public:
  61.         bool           AtEnd          ( void ) const;
  62.         void           Next           ( void );
  63.         const string & Name           ( void ) const;
  64.         const string & Address        ( void ) const;
  65.         CIterator ( rec r );
  66.     private:
  67.         rec record;
  68.         int act;
  69.  };
  70.  
  71. CIterator::CIterator ( rec r )
  72.  {
  73.     record = r;
  74.     act = 0;
  75.  }
  76.  
  77. bool CIterator::AtEnd ( void ) const
  78.  {
  79.     return act >= record.count;
  80.  }
  81.  
  82. void CIterator::Next ( void )
  83.  {
  84.     act ++;
  85.  }
  86.  
  87. const string & CIterator::Name ( void ) const
  88.  {
  89.     return record.rdb[act] -> name;
  90.  }
  91.  
  92. const string & CIterator::Address ( void ) const
  93.  {
  94.     return record.rdb[act] -> addr;
  95.  }
  96.  
  97. class CCompanyIndex
  98.  {
  99.     public:
  100.          CCompanyIndex      ( void );
  101.         ~CCompanyIndex      ( void );
  102.         bool         Add              ( const string & oName, const string & oAddr, const string & cName, const string & cAddr );
  103.         bool         Del              ( const string & oName, const string & oAddr, const string & cName, const string & cAddr );                                                
  104.         CIterator  * SearchOwner      ( const string & oName, const string & oAddr ) const;
  105.         CIterator  * SearchCompany    ( const string & cName, const string & cAddr ) const;
  106.     private:
  107.         static const int    m_startSize = 1000;
  108.         rec **              m_cdb;
  109.         rec **              m_odb;
  110.         int                 m_cCount;
  111.         int                 m_cSize;
  112.         int                 m_oCount;
  113.         int                 m_oSize;
  114.         bool                Find                    ( const string & name, const string & addr, rec ** db, int count, int & p ) const;
  115.         void                AddNewRec               ( const string & name, const string & addr, rec **& db, int & count, int pos );
  116.         bool                LinkOwnerToCompany      ( rec *& owner, rec *& company );
  117.         bool                UnLinkOwnerFromCompany  ( rec *& owner, rec *& company );
  118.         void                Realloc                 ( rec **& db, int count, int & size, double q = 2 );
  119.         void                DelDb                   ( rec ** db, int count );
  120.  };
  121.  
  122. CCompanyIndex::~CCompanyIndex ()
  123.  {
  124.     DelDb ( m_odb, m_oCount );
  125.     DelDb ( m_cdb, m_cCount );
  126.  }
  127.  
  128. CCompanyIndex::CCompanyIndex ()
  129.  {
  130.     m_cCount    = 0;
  131.     m_cSize     = m_startSize;
  132.     m_oCount    = 0;
  133.     m_oSize     = m_startSize;
  134.     m_cdb       = new rec * [m_cSize];
  135.     m_odb       = new rec * [m_oSize]; 
  136.  }
  137.  
  138. bool CCompanyIndex::Find ( const string & name, const string & addr, rec ** db, int count, int & p ) const
  139.  {
  140.     int mid = 0, min = 0, max = count - 1, c;
  141.     rec target ( name, addr );
  142.     while ( min <= max )
  143.      {
  144.         mid = ( max + min ) / 2;
  145.         c = db[mid] -> cmp ( target );
  146.         if ( c < 0 )         
  147.             max = mid - 1;
  148.         else if ( c > 0 )
  149.             min = mid + 1;
  150.         else
  151.          {
  152.             p = mid;
  153.             return true;
  154.          }
  155.      }
  156.     p = min;
  157.     return false;
  158.  }
  159.  
  160. void CCompanyIndex::AddNewRec ( const string & name, const string & addr, rec **& db, int & count, int pos )
  161.  {
  162.     for ( int i = count; i > pos; i -- )
  163.         db[i] = db[ i - 1 ];
  164.     db[pos] = new rec ( name, addr );
  165.     count ++;
  166.  }
  167.  
  168. void CCompanyIndex::DelDb ( rec ** db, int count )
  169.  {
  170.     for ( int i = 0; i < count; i++ )
  171.         delete db[i];
  172.     delete [] db;
  173.  }
  174.  
  175. bool CCompanyIndex::LinkOwnerToCompany ( rec *& owner, rec *& company )
  176.  {
  177.     int cPos, oPos;
  178.     bool companyHasThisOwner    = Find ( owner -> name, owner -> addr, company -> rdb, company -> count, oPos );
  179.     bool ownerOwnsThisCompany   = Find ( company -> name, company -> addr, owner -> rdb, owner -> count, cPos );
  180.    
  181.     if ( ! companyHasThisOwner && ! ownerOwnsThisCompany ) //no realations exist
  182.      {
  183.         company -> add ( owner, oPos );
  184.         owner -> add ( company, cPos );
  185.         return true;
  186.      }
  187.      
  188.     return false;
  189.  }
  190.  
  191. bool CCompanyIndex::UnLinkOwnerFromCompany ( rec *& owner, rec *& company )
  192.  {
  193.     int cPos, oPos;
  194.     bool companyHasThisOwner    = Find ( owner -> name, owner -> addr, company -> rdb, company -> count, oPos );
  195.     bool ownerOwnsThisCompany   = Find ( company -> name, company -> addr, owner -> rdb, owner -> count, cPos );
  196.    
  197.     if ( ! companyHasThisOwner && ! ownerOwnsThisCompany ) //no realations exist
  198.         return false;
  199.    
  200.     company -> del ( oPos );
  201.     owner -> del ( cPos );
  202.     return true;
  203.  }
  204.  
  205. void CCompanyIndex::Realloc ( rec **& db, int count, int & size, double q )
  206.  {
  207.     size *= q;
  208.     rec ** newDb = new rec * [size];
  209.     for ( int i = 0; i < count; i ++ )
  210.         newDb[i] = db[i];
  211.     delete [] db;
  212.     db = newDb;
  213.  }
  214.  
  215. bool CCompanyIndex::Add ( const string & oName, const string & oAddr, const string & cName, const string & cAddr )
  216.  {
  217.     int oPos, cPos;
  218.     bool ownerExist     = Find ( oName, oAddr, m_odb, m_oCount, oPos );
  219.     bool companyExist   = Find ( cName, cAddr, m_cdb, m_cCount, cPos );
  220.    
  221.     if ( m_cCount == m_cSize )
  222.         Realloc ( m_cdb, m_cCount, m_cSize );
  223.     if ( m_oCount == m_oSize )
  224.         Realloc ( m_odb, m_oCount, m_oSize );
  225.        
  226.     if ( ! ownerExist )
  227.         AddNewRec ( oName, oAddr, m_odb, m_oCount, oPos );
  228.        
  229.     if ( ! companyExist )
  230.         AddNewRec ( cName, cAddr, m_cdb, m_cCount, cPos );
  231.        
  232.     if ( ! LinkOwnerToCompany ( m_odb[oPos], m_cdb[cPos] ) ) //this relation already exists
  233.         return false;
  234.     return true;
  235.  }
  236.  
  237. bool CCompanyIndex::Del ( const string & oName, const string & oAddr, const string & cName, const string & cAddr )
  238.  {
  239.     int oPos, cPos;
  240.     bool ownerExist     = Find ( oName, oAddr, m_odb, m_oCount, oPos );
  241.     bool companyExist   = Find ( cName, cAddr, m_cdb, m_cCount, cPos );
  242.     if ( ! ownerExist || ! companyExist )
  243.         return false;
  244.  
  245.     if ( ! UnLinkOwnerFromCompany ( m_odb[oPos], m_cdb[cPos] ) )
  246.         return false;
  247.        
  248.     return true;
  249.  }
  250.  
  251. CIterator  * CCompanyIndex::SearchOwner ( const string & oName, const string & oAddr ) const
  252.  {
  253.     int oPos;
  254.     if ( ! Find ( oName, oAddr, m_odb, m_oCount, oPos ) )
  255.         return NULL;
  256.     return new CIterator ( *m_odb[oPos] );
  257.  }
  258.  
  259. CIterator  * CCompanyIndex::SearchCompany ( const string & cName, const string & cAddr ) const
  260.  {
  261.     int cPos;
  262.     if ( ! Find ( cName, cAddr, m_cdb, m_cCount, cPos ) )
  263.         return NULL;
  264.     return new CIterator ( *m_cdb[cPos] );
  265.  }
  266.  
  267. #ifndef __PROGTEST__
  268. int main ( int argc, char * argv[] )
  269.  {
  270.          
  271.     return 0;
  272.  }
  273. #endif /* __PROGTEST__ */
Advertisement
Add Comment
Please, Sign In to add comment