Advertisement
Guest User

Untitled

a guest
Dec 12th, 2015
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. //#include <iostream>
  2. //#include <string>
  3. //#include "dbc.h"
  4. //using namespace std;
  5. //
  6. //
  7. //
  8. //class Dictionary
  9. //{
  10. //private:
  11. // string names[100];
  12. // string emails[100];
  13. // int size;
  14. //public:
  15. // Dictionary()
  16. // {
  17. // size =0;
  18. // }
  19. // void Add(string name,string email)
  20. // {
  21. // names[size] = name;
  22. // emails[size] = email;
  23. // size++;
  24. // }
  25. //
  26. // void Remove(string name)
  27. // {
  28. // // a simple example on removing array of numbers
  29. // // if the array is {1,5,3,4,2,6,7,8,9,10}
  30. // // and its size is 10 elements
  31. // // and i want to remove entry "2"
  32. // // i'll find its index first through the following code segment
  33. // int indextoberemoved;
  34. //
  35. // for(int i =0;i<size;i++)
  36. // {
  37. // if(names[i] == name)
  38. // {
  39. // indextoberemoved = i;
  40. // break;
  41. // }
  42. // }
  43. // // then i'll move all elements after that index backword one step
  44. // // and with decrementing the size, this is equivalent to removing that element
  45. // size--;
  46. // for(int i = indextoberemoved;i<size;i++)
  47. // {
  48. // names[i] = names[i+1];
  49. // emails[i] = emails[i+1];
  50. // }
  51. // }
  52. // void printentries()
  53. // {
  54. // for(int i =0;i<size;i++)
  55. // {
  56. // cout<<"Entry #"<<i+1<<":"<<endl<<names[i]<<": "<<emails[i]<<endl;
  57. // }
  58. // }
  59. //};
  60. //
  61. //
  62. //
  63. //void main()
  64. //{
  65. // Dictionary x;
  66. // x.Add("omar","omar@live.com");
  67. // x.Add("hassan","hassan@live.com");
  68. // cout<<"Before Deleting Hassan"<<endl;
  69. // x.printentries();
  70. // x.Remove("hassan");
  71. // cout<<"After Deleting Hassan"<<endl;
  72. // x.printentries();
  73. // system ("pause");
  74. //}
  75.  
  76.  
  77. #include <iostream>
  78. #include <string>
  79. #include "dbc.h"
  80. using namespace std;
  81.  
  82. class Dictionary
  83. {
  84. private:
  85. string names[100];
  86. string emails[100];
  87. int size;
  88. public:
  89. Dictionary()
  90. {
  91. size = 0;
  92. }
  93. int Getsize()
  94. {
  95. return size;
  96. }
  97. bool CheckName(string name)
  98. {
  99. for (int i = 0;i < 100; i++)
  100. {
  101. if (names[i]==name)
  102. {
  103. return false; //law enta la2eet elaesm
  104. }
  105. }
  106. return true; //law enta mal2thash
  107. }
  108.  
  109. bool CheckEmail(string email)
  110. {
  111. for (int i = 0;i < 100; i++)
  112. {
  113. if (emails[i] == email)
  114. {
  115. return false;
  116. }
  117. }
  118. return true;
  119.  
  120. }
  121.  
  122. void Add(string name, string email)
  123. {
  124. INVARIANT0(Getsize()>=0 && Getsize()<=99);
  125. REQUIRE0(CheckName(name)==true && CheckEmail(email)==true);
  126. names[size] = name;
  127. emails[size] = email;
  128. size++;
  129. ENSURE0(CheckName(name) == false && CheckEmail(email) == false);
  130. INVARIANT0(Getsize()>=0 && Getsize()<=99);
  131.  
  132. }
  133.  
  134. void Remove(string name)
  135. {
  136. INVARIANT0(Getsize() >= 0 && Getsize() <= 99);
  137. REQUIRE0(CheckName(name)==false);
  138. //body
  139. int indextoberemoved;
  140. for (int i = 0;i<size;i++)
  141. {
  142. if (names[i] == name)
  143. {
  144. indextoberemoved = i;
  145. break;
  146. }
  147. }
  148. size--;
  149. for (int i = indextoberemoved;i<size;i++)
  150. {
  151. names[i] = names[i + 1];
  152. emails[i] = emails[i + 1];
  153. }
  154.  
  155. ENSURE0(CheckName(name) == true );
  156. INVARIANT0(Getsize() >= 0 && Getsize() <= 99);
  157. }
  158. void printentries()
  159. {
  160. for (int i = 0;i<size;i++)
  161. {
  162. cout << "Entry #" << i + 1 << ":" << endl << names[i] << ": " << emails[i] << endl;
  163. }
  164. }
  165. };
  166.  
  167.  
  168.  
  169. void main()
  170. {
  171.  
  172. Dictionary x;
  173. x.Add("omar", "omar@live.com");
  174. x.Add("hassan", "hassan@live.com");
  175. cout << "Before Deleting Hassan" << endl;
  176. x.printentries();
  177. try {
  178. x.Remove("hassan");
  179. }
  180. catch (DesignByContractException e)
  181. {
  182. cout << (string)e ;
  183. }
  184. cout << "After Deleting Hassan" << endl;
  185. x.printentries();
  186.  
  187. system("pause");
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement