Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef __PROGTEST__
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <cstring>
- #include <cstdlib>
- #include <cstdio>
- using namespace std;
- #endif /* __PROGTEST__ */
- struct rec {
- rec ( string oName, string oAddr, string cName = "", string cAddr = "" );
- rec ();
- string oName;
- string oAddr;
- string cName;
- string cAddr;
- int cmp ( rec a );
- };
- rec::rec () {}
- rec::rec ( string oN, string oA, string cN, string cA )
- {
- oName = oN;
- oAddr = oA;
- cName = cN;
- cAddr = cA;
- }
- int rec::cmp ( rec a )
- {
- int res = a . oName . compare ( oName );
- if ( res != 0 ) return res;
- return a . oAddr . compare ( oAddr );
- }
- class CCompanyIndex
- {
- public:
- CCompanyIndex ();
- ~CCompanyIndex ();
- bool Add ( const string & oName, const string & oAddr, const string & cName, const string & cAddr );
- bool Del ( const string & oName, const string & oAddr );
- bool Search ( const string & oName, const string & oAddr, string & cName, string & cAddr ) const;
- private:
- static const int m_startSize = 1000;
- int m_count;
- int m_dbSize;
- rec ** db;
- void Realloc ( double q = 2 );
- int Find ( const string & oName, const string & oAddr, bool & exact ) const;
- };
- CCompanyIndex::~CCompanyIndex ( void )
- {
- for ( int i = 0; i < m_count; i++ )
- delete db[i];
- delete [] db;
- }
- CCompanyIndex::CCompanyIndex ( void )
- {
- m_count = 0;
- m_dbSize = m_startSize;
- db = new rec * [m_startSize];
- for ( int i = 0; i < m_startSize; i ++ )
- db[i] = NULL;
- }
- void CCompanyIndex::Realloc ( double q )
- {
- rec ** newDb = NULL;
- m_dbSize *= q;
- newDb = new rec * [m_dbSize];
- for ( int i = 0; i < m_count; i ++ )
- newDb[i] = db[i];
- delete [] db;
- db = newDb;
- }
- int CCompanyIndex::Find ( const string & oName, const string & oAddr, bool & exact ) const
- {
- int mid = 0, min = 0, max = m_count - 1, c;
- rec target ( oName, oAddr );
- while ( min <= max )
- {
- mid = ( max + min ) / 2;
- c = db[mid] -> cmp ( target );
- if ( c < 0 )
- max = mid - 1;
- else if ( c > 0 )
- min = mid + 1;
- else
- {
- exact = true;
- return mid;
- }
- }
- exact = false;
- return min;
- }
- bool CCompanyIndex::Add ( const string & oName, const string & oAddr, const string & cName, const string & cAddr )
- {
- if ( m_count == m_dbSize )
- CCompanyIndex::Realloc ();
- bool e;
- int pos = Find ( oName, oAddr, e );
- if ( e )
- return false;
- for ( int i = m_count; i > pos; i -- )
- db[i] = db[ i - 1 ];
- db[pos] = new rec ( oName, oAddr, cName, cAddr );
- m_count ++;
- return true;
- }
- bool CCompanyIndex::Del ( const string & oName, const string & oAddr )
- {
- bool e;
- int pos = Find ( oName, oAddr, e );
- if ( ! e )
- return false;
- delete db[pos];
- for ( int i = pos; i < m_count; i ++ )
- db[i] = db[i+1];
- m_count --;
- return true;
- }
- bool CCompanyIndex::Search ( const string & oName, const string & oAddr, string & cName, string & cAddr ) const
- {
- bool e;
- int pos = Find ( oName, oAddr, e );
- if ( ! e )
- return false;
- cName = db[pos] -> cName;
- cAddr = db[pos] -> cAddr;
- return true;
- }
- #ifndef __PROGTEST__
- int main ( int argc, char * argv[] )
- {
- bool status;
- string cName, cAddress;
- CCompanyIndex b1;
- status = b1 . Add ( "Smith", "Oak road", "ACME, Ltd.", "One ACME road" );
- // status = true
- cout << "status should be true and is " << boolalpha << status << endl;
- status = b1 . Add ( "Brown", "Second street", "MakroHard, Inc.", "Soft street" );
- // status = true
- cout << "status should be true and is " << boolalpha << status << endl;
- status = b1 . Add ( "Hacker", "5-th avenue", "Forks and Knives, Ltd.", "Cutlery avenue" );
- // status = true
- cout << "status should be true and is " << boolalpha << status << endl;
- status = b1 . Add ( "Hacker", "7-th avenue", "Child toys, Inc.", "Red light district" );
- // status = true
- cout << "status should be true and is " << boolalpha << status << endl;
- status = b1 . Search ( "Brown", "Second street", cName, cAddress );
- // status = true, cName = "MakroHard, Inc.", cAddress="Soft street"
- cout << "status should be true and is " << boolalpha << status << endl;
- cout << "fict" << "cName = \"MakroHard, Inc.\", cAddress=\"Soft street\"" << endl;
- cout << "real" << "cName = " << cName << " cAddress= " << cAddress << endl;
- status = b1 . Search ( "Hacker", "Oak road", cName, cAddress );
- // status = false
- cout << "status should be false and is " << boolalpha << status << endl;
- status = b1 . Search ( "Smith", "Oak road", cName, cAddress );
- // status = true, cName = "ACME, Ltd.", cAddress="One ACME road"
- cout << "status should be true and is " << boolalpha << status << endl;
- status = b1 . Del ( "Smith", "Oak road" );
- // status = true
- cout << "status should be true and is " << boolalpha << status << endl;
- status = b1 . Search ( "Smith", "Oak road", cName, cAddress );
- // status = false
- cout << "status should be false and is " << boolalpha << status << endl;
- return 0;
- }
- #endif /* __PROGTEST__ */
Advertisement
Add Comment
Please, Sign In to add comment