Advertisement
Guest User

Untitled

a guest
Sep 25th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. //============================================================================
  2. // Name : live04-bags.cpp
  3. // Author :
  4. // Version :
  5. // Copyright :
  6. // Description : Playing with bags...
  7. //============================================================================
  8.  
  9. #include <iostream>
  10. #include <set> // http://www.cplusplus.com/reference/set/set/
  11. #include <string>
  12.  
  13. using namespace std;
  14.  
  15. // enumeration type for actions on the bag
  16. enum action {add , del , qry , quit, none};
  17.  
  18. // translation of strings into actions
  19. action str2action(string s){
  20. if (s=="add") return add;
  21. if (s=="quit") return quit;
  22. if (s=="del") return del;
  23. if (s=="qry") return qry;
  24. return none;
  25. }
  26.  
  27. // a place in the bag (full declaration)
  28. struct item {
  29. double val;
  30. item * next;
  31. };
  32.  
  33. //#define BAG_AS_ARRAY_SIZE 10
  34.  
  35. // We will implement a bag in three ways
  36. struct bag {
  37. multiset<double> as_set; // using sets
  38. };
  39.  
  40.  
  41.  
  42. // function to display the content of the bag
  43. void display_bag(bag b){
  44.  
  45. // cout << "The bag is : " << endl;
  46.  
  47. // Set
  48. // cout << " - (S) - : ";
  49.  
  50.  
  51. // std::multiset<unsigned int>::iterator it;
  52. // for (it = b.as_set.begin(); it != b.as_set.end(); ++it){
  53. // unsigned int e = *it;
  54. // cout << e << " " ;
  55. // }
  56. //
  57. // cout << endl;
  58. //
  59. return;
  60.  
  61. }
  62.  
  63. void insert(bag &b,double x){
  64.  
  65. // Array
  66. // b.as_array[x] = true;
  67.  
  68. // Linked list
  69. // we allocate a new place for the new value
  70. //item * i = new item;
  71. //i->val = x;
  72. //(*i).val=x;
  73.  
  74. // we make it point to the bag
  75. //i->next = b.as_linked_items;
  76. // we set the bag now to start with the new place
  77. //b.as_linked_items = i;
  78.  
  79. // Set
  80. b.as_set.insert(x);
  81.  
  82. }
  83.  
  84. void remove(bag &b, double x){
  85.  
  86. std::multiset<double>::iterator hit(b.as_set.find(x));
  87. if (hit!= b.as_set.end()) b.as_set.erase(hit);
  88.  
  89. //b.as_set.erase(x);
  90. }
  91.  
  92. bool query(bag &b, double x){
  93. bool isPresent = 0;
  94. //for(int i=0; i<BAG_AS_ARRAY_SIZE; i++){
  95.  
  96. if(b.as_set.count(x) >= 1){
  97. isPresent = 1;
  98.  
  99. }
  100. return isPresent;
  101.  
  102.  
  103.  
  104. }
  105.  
  106. // this function deals with actions on a bag
  107. void update(bag &b, action a, double x){
  108.  
  109. switch(a){
  110. case add:
  111. insert(b,x);
  112. break;
  113. case quit:
  114. break;
  115. case del:
  116. remove(b,x);
  117. break;
  118. case qry:
  119. if(query(b,x)){
  120. cout << "T";
  121. }
  122. else{
  123. cout << "F";
  124. }
  125. break;
  126. case none:
  127. break;
  128. default:
  129. break;
  130. }
  131.  
  132. return;
  133. }
  134.  
  135. int main(){
  136.  
  137. bag my_bag;
  138. string my_act_str;
  139. double x;
  140.  
  141. //initialise(my_bag);
  142.  
  143. bool go_on = true;
  144. while(go_on){
  145.  
  146. display_bag(my_bag);
  147. // cout << "What's next? (actions = add x , quit)" << endl;
  148. cin >> my_act_str;
  149. action act = str2action(my_act_str);
  150. if(act==quit){
  151. go_on=false;
  152. }
  153. else{
  154. cin >> x;
  155. update(my_bag,act,x);
  156. }
  157.  
  158. }
  159.  
  160. return 0;
  161.  
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement