Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include <iostream>
- //#include <string>
- //#include "dbc.h"
- //using namespace std;
- //
- //
- //
- //class Dictionary
- //{
- //private:
- // string names[100];
- // string emails[100];
- // int size;
- //public:
- // Dictionary()
- // {
- // size =0;
- // }
- // void Add(string name,string email)
- // {
- // names[size] = name;
- // emails[size] = email;
- // size++;
- // }
- //
- // void Remove(string name)
- // {
- // // a simple example on removing array of numbers
- // // if the array is {1,5,3,4,2,6,7,8,9,10}
- // // and its size is 10 elements
- // // and i want to remove entry "2"
- // // i'll find its index first through the following code segment
- // int indextoberemoved;
- //
- // for(int i =0;i<size;i++)
- // {
- // if(names[i] == name)
- // {
- // indextoberemoved = i;
- // break;
- // }
- // }
- // // then i'll move all elements after that index backword one step
- // // and with decrementing the size, this is equivalent to removing that element
- // size--;
- // for(int i = indextoberemoved;i<size;i++)
- // {
- // names[i] = names[i+1];
- // emails[i] = emails[i+1];
- // }
- // }
- // void printentries()
- // {
- // for(int i =0;i<size;i++)
- // {
- // cout<<"Entry #"<<i+1<<":"<<endl<<names[i]<<": "<<emails[i]<<endl;
- // }
- // }
- //};
- //
- //
- //
- //void main()
- //{
- // Dictionary x;
- // x.Add("omar","omar@live.com");
- // x.Add("hassan","hassan@live.com");
- // cout<<"Before Deleting Hassan"<<endl;
- // x.printentries();
- // x.Remove("hassan");
- // cout<<"After Deleting Hassan"<<endl;
- // x.printentries();
- // system ("pause");
- //}
- #include <iostream>
- #include <string>
- #include "dbc.h"
- using namespace std;
- class Dictionary
- {
- private:
- string names[100];
- string emails[100];
- int size;
- public:
- Dictionary()
- {
- size = 0;
- }
- int Getsize()
- {
- return size;
- }
- bool CheckName(string name)
- {
- for (int i = 0;i < 100; i++)
- {
- if (names[i]==name)
- {
- return false; //law enta la2eet elaesm
- }
- }
- return true; //law enta mal2thash
- }
- bool CheckEmail(string email)
- {
- for (int i = 0;i < 100; i++)
- {
- if (emails[i] == email)
- {
- return false;
- }
- }
- return true;
- }
- void Add(string name, string email)
- {
- INVARIANT0(Getsize()>=0 && Getsize()<=99);
- REQUIRE0(CheckName(name)==true && CheckEmail(email)==true);
- names[size] = name;
- emails[size] = email;
- size++;
- ENSURE0(CheckName(name) == false && CheckEmail(email) == false);
- INVARIANT0(Getsize()>=0 && Getsize()<=99);
- }
- void Remove(string name)
- {
- INVARIANT0(Getsize() >= 0 && Getsize() <= 99);
- REQUIRE0(CheckName(name)==false);
- //body
- int indextoberemoved;
- for (int i = 0;i<size;i++)
- {
- if (names[i] == name)
- {
- indextoberemoved = i;
- break;
- }
- }
- size--;
- for (int i = indextoberemoved;i<size;i++)
- {
- names[i] = names[i + 1];
- emails[i] = emails[i + 1];
- }
- ENSURE0(CheckName(name) == true );
- INVARIANT0(Getsize() >= 0 && Getsize() <= 99);
- }
- void printentries()
- {
- for (int i = 0;i<size;i++)
- {
- cout << "Entry #" << i + 1 << ":" << endl << names[i] << ": " << emails[i] << endl;
- }
- }
- };
- void main()
- {
- Dictionary x;
- x.Add("omar", "omar@live.com");
- x.Add("hassan", "hassan@live.com");
- cout << "Before Deleting Hassan" << endl;
- x.printentries();
- try {
- x.Remove("hassan");
- }
- catch (DesignByContractException e)
- {
- cout << (string)e ;
- }
- cout << "After Deleting Hassan" << endl;
- x.printentries();
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement