Я хочу реализовать симметрическую разность двух множеств, заданных односвязным списком. Проблема заключается в том, что написанная мной функция изменяет один из операндов. Элемент множества являет собой кортеж из максимум 3-х чисел, количество чисел в кортеже - переменная varNum.
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
struct item
{
int data[3];
item *next;
void print(int varNum) {
cout << "(";
for(int i=0;i<varNum-1;i++)
cout << data[i] << "; ";
cout << data[varNum-1] << ")";
}
};
class List
{
private:
item *head;
public:
List();
~List();
void add(int data[3]);
void print(int varNum);
int calc(int varNum, int point[3]);
bool isLinear(int varNum);
List sum(int varNum, List a);
List mult(int varNum, List a);
};
List::List()
{
head=NULL;
}
List::~List()
{
while (head!=NULL)
{
item *temp=head->next;
delete head;
head=temp;
}
}
void List::add(int data[3])
{
item *temp=new item;
copy(data, data+3, temp->data);
temp->next=head;
head=temp;
}
void List::print(int varNum)
{
item *temp=head;
if (temp==NULL) cout << 0;
else {
while (temp!=NULL)
{
temp->print(varNum);
cout << " ";
temp=temp->next;
}
}
}
int List::calc(int varNum, int point[3])
{
item *temp=head;
int val=0, term;
while (temp!=NULL)
{
term=1;
for(int j=0;j<varNum;j++)
if (temp->data[j]==1) term*=point[j];
val=(val+term)%2;
temp=temp->next;
}
return val;
}
bool List::isLinear(int varNum)
{
item *temp=head;
int cnt;
while(temp!=NULL)
{
cnt=0;
for(int j=0;j<varNum;j++)
{
if (temp->data[j]==1) cnt++;
if (cnt>1) return false;
}
temp=temp->next;
}
return true;
}
List List::sum(int varNum, List a)
{
item *temp=head, *t;
List sum;
int cnt;
bool eq=false;
while(temp!=NULL)
{
eq=false;
t=a.head;
while(t!=NULL)
{
cnt=0;
for(int i=0;i<varNum;i++)
if (temp->data[i]==t->data[i]) cnt++;
if (cnt==varNum) {
eq=true;
break;
}
t=t->next;
}
if (!eq) sum.add(temp->data);
temp=temp->next;
}
temp=a.head;
while(temp!=NULL)
{
eq=false;
t=head;
while(t!=NULL)
{
cnt=0;
for(int i=0;i<varNum;i++)
if (temp->data[i]==t->data[i]) cnt++;
if (cnt==varNum) {
eq=true;
break;
}
t=t->next;
}
if (!eq) sum.add(temp->data);
temp=temp->next;
}
return sum;
}
List List::mult(int varNum, List a)
{
List mult;
item *temp=head, *t;
while(temp!=NULL)
{
t=a.head;
while(t!=NULL)
{
List term;
int termData[3];
for(int i=0;i<varNum;i++)
termData[i]=max(t->data[i], temp->data[i]);
term.add(termData);
mult=term.sum(varNum, mult);
t=t->next;
}
temp=temp->next;
}
return mult;
}
void getPolynoms(int &varNum, List &a, List &b)
{
ifstream in("i.txt");
if (!in.is_open()) {
cout << "WRONG NAME OF FILE";
return;
}
int n, m;
in >> varNum; // кількість змінних
in >> n;
int temp[3];
for(int i=0;i<n;i++)
{
for(int j=0;j<varNum;j++)
in >> temp[j];
a.add(temp);
}
in >> m;
for(int i=0;i<m;i++)
{
for(int j=0;j<varNum;j++)
in >> temp[j];
b.add(temp);
}
}
int main()
{
int varNum;
List a, b;
getPolynoms(varNum, a, b);
cout << "A at the beginning:\n";
a.print(varNum);
cout << "\nB at the beginning:\n";
b.print(varNum);
List m=a.sum(varNum, b);
cout << "\nA after the addition:\n";
a.print(varNum);
cout << "\nB after the addition:\n";
b.print(varNum);
cout << "\nThe sum is:\n";
m.print(varNum);
return 0;
}
Результат:
A at the beginning:
(0; 0; 0)
B at the beginning:
(1; 1; 0)
A after the addition:
(0; 0; 0)
B after the addition:
(7232248; 7209152; 0)
The sum is:
(1; 1; 0) (0; 0; 0)
Меня интересует, почему изменяется b, и как это пофиксить?