Доброго времени суток!Используя связный список, пытаюсь реализовать класс множеств. B=A|C; не работает корректно. Так и не смог понять, где напортачил(надеюсь, что код читаем). Заранее спасибо!
Код:
#ifndef BASE_SET_H
#define BASE_SET_H
#include <iostream>
using namespace std;
class base_set
{
friend class set;
public:
base_set(int inf){next_ptr = 0; data = inf;}
int get_data() const{return data;}
base_set* get_next() const{return next_ptr;}
private:
int data;
base_set *next_ptr;
};
class set
{
friend ostream& operator<<(ostream &output, set &value);
friend istream& operator>>(istream &input, set &value);
public:
set(){first_ptr = last_ptr = 0;}
set(const set & right);
void clear();//очистка списка
~set(){clear();}
bool isEmpty() const{return first_ptr == 0;}
base_set *inSet(int val) const;//принадлежит ли число множеству
set &operator+(int value);//добавление элемента
set &operator-(int value);//удаление элемента
set &operator=(const set& right);
set &operator| (const set & right) const;//объединение множеств
private:
base_set *get_new_ptr(int val)
{base_set *ptr = new base_set (val); return ptr;}
base_set *first_ptr, *last_ptr;
};
ostream& operator<<(ostream &output, set &value);
istream& operator>>(istream &input, set &value);
#endif
Код:
#include "base_set.h"
void set::clear()
{
if (!isEmpty())
{
base_set *cur_ptr = first_ptr, *temp_ptr;
while (cur_ptr != 0)
{
temp_ptr = cur_ptr;
cur_ptr = cur_ptr->next_ptr;
delete temp_ptr;
}
}
}
set::set(const set & right)
{
base_set *cur_ptr = right.first_ptr;
while (cur_ptr!= 0)
{
*this + (cur_ptr->data);
cur_ptr = cur_ptr->next_ptr;
}
}
set& set::operator+( int value)
{
base_set *new_ptr =get_new_ptr(value);
if (isEmpty())
first_ptr =last_ptr = new_ptr;
else if (!inSet(value))
{
last_ptr->next_ptr = new_ptr;
last_ptr = new_ptr;
}
return *this;
}
ostream& operator<<(ostream &output, set &value)
{
output <<"{ " ;
if (!(value.isEmpty()))
{
base_set *current_ptr = value.first_ptr;
while (current_ptr != value.last_ptr)
{
output << (current_ptr->get_data()) << ", ";
current_ptr = current_ptr->get_next();
}
output << (value.last_ptr->get_data());
}
output <<" }" ;
return output;
}
istream& operator>>(istream &input, set &value)
{
int temp_var;
while (input >> temp_var)
value + temp_var;
return input;
}
base_set *set::inSet(int val) const
{
if (!isEmpty())
{
base_set *cur_ptr = first_ptr;
while (cur_ptr!=0)
{
if (cur_ptr->data== val )
return cur_ptr;
cur_ptr = cur_ptr->next_ptr;
}
}
return 0;
}
set &set::operator-(int value)
{
if (!isEmpty())
{
base_set *temp_ptr;
if (temp_ptr = inSet(value))
{
base_set *cur_ptr = first_ptr;
if (last_ptr == first_ptr)
last_ptr = first_ptr = 0;
else if (temp_ptr == first_ptr)
first_ptr = temp_ptr->next_ptr;
else
{
while (cur_ptr->next_ptr != temp_ptr)
cur_ptr = cur_ptr->next_ptr;
if (cur_ptr ->next_ptr == last_ptr)
{
last_ptr =cur_ptr;
last_ptr->next_ptr = 0;
}
else
cur_ptr->next_ptr = (cur_ptr->next_ptr)->next_ptr;
}
delete temp_ptr;
}
}
return *this;
}
set &set::operator=(const set& right)
{
if (&right != this)
{
clear(); base_set *cur_ptr=right.first_ptr;
while (cur_ptr!=0)
{
this->operator+(cur_ptr->data);
cur_ptr = cur_ptr->next_ptr;
}
}
return *this;
}
set &set::operator| (const set & right) const
//тут я как только не извращался...
{
if ((&right != this) && (!right.isEmpty()))
{
set temp =*this;
cout << temp;
base_set *cur_ptr = right.first_ptr;
while (cur_ptr!=0)
{
temp.operator+(cur_ptr->data);
cur_ptr = cur_ptr->next_ptr;
}
return temp;
}
return *this;
}
Код:
#include "base_set.h"
using namespace std;
int main()
{set A, B, C;cin >> A; (С = B = A) +7;
cout << A << B << C << endl; B = A|C;
cout <<endl <<A - 3 + 5 << endl << B ;return 0;}