michael_hartman_cz

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

Mar 28th, 2013
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.08 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     ( string oName, string oAddr, string cName = "", string cAddr = "" );
  13.     rec     ();
  14.     string  oName;
  15.     string  oAddr;
  16.     string  cName;
  17.     string  cAddr;
  18.     int     cmp ( rec a );
  19. };
  20.  
  21. rec::rec () {}
  22.  
  23. rec::rec ( string oN, string oA, string cN, string cA )
  24.  {
  25.     oName = oN;
  26.     oAddr = oA;
  27.     cName = cN;
  28.     cAddr = cA;
  29.  }
  30.  
  31. int rec::cmp ( rec a )
  32.  {
  33.     int res = a . oName . compare ( oName );
  34.     if ( res != 0 ) return res;
  35.         return a . oAddr . compare ( oAddr );
  36.  }
  37.  
  38. class CCompanyIndex
  39.  {
  40.     public:
  41.         CCompanyIndex   ();
  42.         ~CCompanyIndex  ();
  43.         bool Add    ( const string & oName, const string & oAddr, const string & cName, const string & cAddr );
  44.         bool Del    ( const string & oName, const string & oAddr );
  45.         bool Search ( const string & oName, const string & oAddr, string & cName, string & cAddr ) const;
  46.     private:
  47.         static const int    m_startSize = 1000;
  48.         int             m_count;
  49.         int             m_dbSize;
  50.         rec **          db;
  51.         void            Realloc ( double q = 2 );
  52.         int             Find ( const string & oName, const string & oAddr, bool & exact ) const;
  53.  };
  54.  
  55. CCompanyIndex::~CCompanyIndex ( void )
  56.  {
  57.     for ( int i = 0; i < m_count; i++ )
  58.         delete db[i];
  59.     delete [] db;
  60.  }
  61.  
  62. CCompanyIndex::CCompanyIndex ( void )
  63.  {
  64.     m_count = 0;
  65.     m_dbSize = m_startSize;
  66.     db = new rec * [m_startSize];
  67.     for ( int i = 0; i < m_startSize; i ++ )
  68.         db[i] = NULL;      
  69.  }
  70.  
  71. void CCompanyIndex::Realloc ( double q )
  72.  {
  73.     rec ** newDb = NULL;
  74.     m_dbSize *= q;
  75.     newDb = new rec * [m_dbSize];
  76.     for ( int i = 0; i < m_count; i ++ )
  77.         newDb[i] = db[i];
  78.     delete [] db;
  79.     db = newDb;
  80.  }
  81.  
  82. int CCompanyIndex::Find ( const string & oName, const string & oAddr, bool & exact ) const
  83.  {
  84.     int mid = 0, min = 0, max = m_count - 1, c;
  85.     rec target ( oName, oAddr );
  86.     while ( min <= max )
  87.      {
  88.         mid = ( max + min ) / 2;
  89.         c = db[mid] -> cmp ( target );
  90.         if ( c < 0 )         
  91.             max = mid - 1;
  92.         else if ( c > 0 )
  93.             min = mid + 1;
  94.         else
  95.          {
  96.             exact = true;
  97.             return mid;
  98.          }
  99.      }
  100.     exact = false;
  101.     return min;
  102.  }
  103.  
  104. bool CCompanyIndex::Add ( const string & oName, const string & oAddr, const string & cName, const string & cAddr )
  105.  {
  106.     if ( m_count == m_dbSize )
  107.         CCompanyIndex::Realloc (); 
  108.     bool e;
  109.     int pos = Find ( oName, oAddr, e );
  110.     if ( e )
  111.         return false;
  112.     for ( int i = m_count; i > pos; i -- )
  113.         db[i] = db[ i - 1 ];
  114.     db[pos] = new rec ( oName, oAddr, cName, cAddr );
  115.     m_count ++;
  116.     return true;
  117.  }
  118.  
  119. bool CCompanyIndex::Del ( const string & oName, const string & oAddr )
  120.  {
  121.     bool e;
  122.     int pos = Find ( oName, oAddr, e );
  123.     if ( ! e )
  124.         return false;
  125.     delete db[pos];
  126.     for ( int i = pos; i < m_count; i ++ )
  127.         db[i] = db[i+1];       
  128.     m_count --;
  129.     return true;
  130.  
  131.  }
  132.  
  133. bool CCompanyIndex::Search ( const string & oName, const string & oAddr, string & cName, string & cAddr ) const
  134.  {
  135.     bool e;
  136.     int pos = Find ( oName, oAddr, e );
  137.     if ( ! e )
  138.         return false;  
  139.     cName = db[pos] -> cName;
  140.     cAddr = db[pos] -> cAddr;
  141.     return true;
  142.  }
  143.  
  144. #ifndef __PROGTEST__
  145. int main ( int argc, char * argv[] )
  146.  {
  147.  
  148.     bool   status;
  149.     string cName, cAddress;
  150.     CCompanyIndex  b1;
  151.    
  152.     status = b1 . Add ( "Smith", "Oak road", "ACME, Ltd.", "One ACME road" );
  153.     // status = true
  154.     cout << "status should be true and is " << boolalpha << status << endl;
  155.    
  156.     status = b1 . Add ( "Brown", "Second street", "MakroHard, Inc.", "Soft street" );
  157.     // status = true
  158.     cout << "status should be true and is " << boolalpha << status << endl;
  159.    
  160.     status = b1 . Add ( "Hacker", "5-th avenue", "Forks and Knives, Ltd.", "Cutlery avenue" );
  161.     // status = true
  162.     cout << "status should be true and is " << boolalpha << status << endl;
  163.    
  164.     status = b1 . Add ( "Hacker", "7-th avenue", "Child toys, Inc.", "Red light district" );
  165.     // status = true
  166.     cout << "status should be true and is " << boolalpha << status << endl;
  167.    
  168.     status = b1 . Search ( "Brown", "Second street", cName, cAddress );
  169.     // status = true, cName = "MakroHard, Inc.", cAddress="Soft street"
  170.     cout << "status should be true and is " << boolalpha << status << endl;
  171.     cout << "fict" << "cName = \"MakroHard, Inc.\", cAddress=\"Soft street\"" << endl;
  172.     cout << "real" << "cName = " << cName << " cAddress= " << cAddress << endl;
  173.    
  174.     status = b1 . Search ( "Hacker", "Oak road", cName, cAddress );
  175.     // status = false
  176.     cout << "status should be false and is " << boolalpha << status << endl;
  177.    
  178.     status = b1 . Search ( "Smith", "Oak road", cName, cAddress );
  179.     // status = true, cName = "ACME, Ltd.", cAddress="One ACME road"
  180.     cout << "status should be true and is " << boolalpha << status << endl;
  181.    
  182.     status = b1 . Del ( "Smith", "Oak road" );
  183.     // status = true
  184.     cout << "status should be true and is " << boolalpha << status << endl;
  185.    
  186.     status = b1 . Search ( "Smith", "Oak road", cName, cAddress );
  187.     // status = false
  188.     cout << "status should be false and is " << boolalpha << status << endl;
  189.      
  190.     return 0;
  191.  }
  192. #endif /* __PROGTEST__ */
Advertisement
Add Comment
Please, Sign In to add comment