Advertisement
Guest User

Fisher Assignment 15

a guest
Apr 23rd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. Fisher's Assignment 15
  2. *NOTES*
  3. - Make sure to add comments and change file locations
  4. - Each division represents a different file, be sure to keep types
  5.  
  6. ______________________________Set.cpp___________________________________
  7. #include <iostream>
  8. #include <sstream>
  9. #include "d:\assignment15\set.h"
  10.  
  11. using namespace std;
  12.  
  13. Set :: Set()
  14. {
  15. len = 0;
  16. }
  17.  
  18. Set :: Set(int arr[], int hm)
  19. {
  20. len = hm;
  21. for (int x = 0; x < len; x++)
  22. {
  23. if (!in(arr[x]))
  24. val[x] = arr[x];
  25. else
  26. val[x] = -99;
  27. }
  28. }
  29.  
  30. int Set :: getLength()
  31. {
  32. return len;
  33. }
  34.  
  35. int Set :: getValue(int n)
  36. {
  37. return val[n];
  38. }
  39.  
  40. bool Set :: in(int n)
  41. {
  42. for (int x = 0; x < len; x++)
  43. if (n == val[x])
  44. return true;
  45. return false;
  46. }
  47.  
  48. void Set :: fill(int arr[], int hm)
  49. {
  50. len = hm;
  51. for (int x = 0; x < hm; x++)
  52. {
  53. if (!in(arr[x]))
  54. val[x] = arr[x];
  55. else
  56. val[x] = -99;
  57. }
  58. }
  59.  
  60. void Set :: add(int n)
  61. {
  62. if (!in(n))
  63. {
  64. val[len] = n;
  65. len ++;
  66. }
  67. }
  68.  
  69. void Set :: remove(int n)
  70. {
  71. if (in(n))
  72. {
  73. int c;
  74. for (int x = 0; x < len; x++)
  75. if (val[x] == n)
  76. {
  77. val[x] = -99;
  78. c = x + 1;
  79. }
  80. for (int y = c; y < len; y++)
  81. val[y-1] = val[y];
  82. len --;
  83. }
  84. }
  85.  
  86. string Set :: toString()
  87. {
  88. stringstream s;
  89. s << "{";
  90. for (int x = 0; x < len; x++)
  91. {
  92. if (val[x] != -99)
  93. s << val[x];
  94. if (((x+1) < len) && (val[x+1] != -99))
  95. s << ", ";
  96. }
  97. s << "}";
  98. return s.str();
  99. }
  100.  
  101. Set intersect(Set a, Set b)
  102. {
  103. int val[30], len = 0;
  104. for (int x = 0; x < a.getLength(); x++)
  105. if (b.in(a.getValue(x)))
  106. {
  107. val[len] = a.getValue(x);
  108. len ++;
  109. }
  110. Set i(val, len);
  111. return i;
  112. }
  113.  
  114. Set getUnion(Set a, Set b)
  115. {
  116. Set i;
  117. for (int x = 0; x < a.getLength(); x++)
  118. i.add(a.getValue(x));
  119. for (int x = 0; x < b.getLength(); x++)
  120. i.add(b.getValue(x));
  121. return i;
  122. }
  123.  
  124. Set difference(Set a, Set b)
  125. {
  126. Set i;
  127. for (int x = 0; x < a.getLength(); x++)
  128. if (!b.in(a.getValue(x)))
  129. i.add(a.getValue(x));
  130. return i;
  131. }
  132.  
  133.  
  134.  
  135. _______________________________Set.h______________________________________
  136. #ifndef set_h
  137. #define set_h
  138.  
  139. using namespace std;
  140.  
  141. class Set
  142. {
  143. public:
  144. Set();
  145. Set(int arr[], int);
  146. int getLength();
  147. int getValue(int);
  148. bool in(int);
  149. void fill(int arr[], int);
  150. void add(int);
  151. void remove(int);
  152. string toString();
  153. private:
  154. int val[30];
  155. int len;
  156. };
  157.  
  158. #endif
  159.  
  160. Set intersect(Set, Set);
  161. Set getUnion(Set, Set);
  162. Set difference(Set, Set);
  163.  
  164.  
  165.  
  166. _______________________________________main.cpp_____________________________
  167. #include <cstdlib>
  168. #include <iostream>
  169. #include <fstream>
  170. #include "d:\assignment15\set.h"
  171. #include "d:\assignment15\set.cpp"
  172.  
  173. using namespace std;
  174.  
  175. struct Input
  176. {
  177. Set a, b, c;
  178. };
  179.  
  180. Input input();
  181. void output(Set, Set, Set);
  182.  
  183. int main( )
  184. {
  185. Input i = input();
  186.  
  187. output(i.a, i.b, i.c);
  188.  
  189. system("PAUSE");
  190. return EXIT_SUCCESS;
  191. }
  192.  
  193. Input input()
  194. {
  195. ifstream in;
  196. //in.open("s:\\computer science ii\\setData-test.txt");
  197. in.open("d:\\assignment15\\setData-test.txt");
  198. int n1, n2, n3, val1[20], val2[20], val3[20], val;
  199.  
  200. in >> n1;
  201. for (int x = 0; x < n1; x++)
  202. {
  203. in >> val;
  204. val1[x] = val;
  205. }
  206. Set a(val1, n1);
  207.  
  208. in >> n2;
  209. for (int x = 0; x < n2; x++)
  210. {
  211. in >> val;
  212. val2[x] = val;
  213. }
  214. Set b(val2, n2);
  215.  
  216. in >> n3;
  217. for (int x = 0; x < n3; x++)
  218. {
  219. in >> val;
  220. val3[x] = val;
  221. }
  222. Set c(val3, n3);
  223.  
  224. Input i;
  225. i.a = a;
  226. i.b = b;
  227. i.c = c;
  228. return i;
  229. }
  230.  
  231. void output(Set a, Set b, Set c)
  232. {
  233. cout << "Set 1: " << a.toString() << endl;
  234. cout << "Set 2: " << b.toString() << endl;
  235. cout << "Set 3: " << c.toString() << endl;
  236. cout << endl;
  237. cout << "The intersection of 1 & 2: " << intersect(a, b).toString() <<endl;
  238. cout << "The intersection of 2 & 3: " << intersect(b, c).toString() <<endl;
  239. cout << "The intersection of 1 & 3: " << intersect(a, c).toString() <<endl;
  240. cout << endl;
  241. cout << "The union of 1 & 2: " << getUnion(a, b).toString() << endl;
  242. cout << "The union of 2 & 3: " << getUnion(b, c).toString() << endl;
  243. cout << "The union of 1 & 3: " << getUnion(a, c).toString() << endl;
  244. cout << endl;
  245. cout << "The difference of 1 & 2: " << difference(a, b).toString() << endl;
  246. cout << "The difference of 2 & 1: " << difference(b, a).toString() << endl;
  247. cout << "The difference of 2 & 3: " << difference(b, c).toString() << endl;
  248. cout << "The difference of 3 & 2: " << difference(c, b).toString() << endl;
  249.  
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement