Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //IntegerSet.cpp
- #include "IntegerSet.h"
- IntegerSet::IntegerSet() {
- //ctor
- for (int i = 0; i < 100; i++) {
- nums.push_back(0);
- }
- }
- IntegerSet::IntegerSet(std::vector<int> in) {
- //ctor
- if (in.size() < 100) {
- throw std::invalid_argument("Vector size must = 100");
- }
- for (auto i : in) {
- nums.push_back(i);
- }
- }
- IntegerSet::~IntegerSet()
- {
- //dtor
- }
- void IntegerSet::push (int in) {
- nums.push_back(in);
- }
- void IntegerSet::knock (int in, int loc) {
- nums[loc] = in;
- }
- void IntegerSet::deleteNum (int loc) {
- nums[loc] = 0;
- }
- void IntegerSet::printSet() {
- for (auto x : nums) {
- std::cout << x;
- }
- std::cout << std::endl;
- }
- std::string IntegerSet::toString() {
- std::ostringstream result{};
- for (int i = 0; i < nums.size(); i++) {
- if (nums[i] != 0) {
- result << i+1 << " ";
- }
- }
- return result.str();
- }
- bool IntegerSet::isEqualTo(IntegerSet in) {
- int iterations{0};
- bool result{1};
- if (nums.size() >= this->nums.size()){
- iterations = this->nums.size();
- }
- else {
- iterations = nums.size();
- }
- for (int i = 0; i < iterations; i++) {
- //std::cout << "Set 1:" << this->nums[i] << " set 2: " << nums[i] << std::endl;
- if (this->nums[i] != in.nums[i]) {
- result = 0;
- }
- }
- return result;
- }
- IntegerSet IntegerSet::unionOfSets (IntegerSet in) {
- int iterations{0};
- IntegerSet result{};
- if (this->nums.size() >= in.nums.size()){
- iterations = this->nums.size();
- }
- else {
- iterations = in.nums.size();
- }
- for (int i = 0; i < iterations; i++) {
- if (this->nums[i] == 1 || in.nums[i] == 1) {
- result.knock(1,i);
- }
- else {
- result.knock(0,i);
- }
- }
- return result;
- }
- IntegerSet IntegerSet::intersectionOfSets(IntegerSet in) {
- IntegerSet result{};
- int iterations{0};
- if (this->nums.size() >= in.nums.size()){
- iterations = this->nums.size();
- }
- else {
- iterations = in.nums.size();
- }
- for (int i = 0; i < iterations; i++) {
- if (this->nums[i] == 1 && in.nums[i] == 1) {
- result.knock(1,i);
- }
- else {
- result.knock(0,i);
- }
- }
- return result;
- }
Add Comment
Please, Sign In to add comment