实验内容:
实现两个集合的相等判定、并、交和差运算。要求:
1)自定义数据结构
2)自先存储结构,并设计算法。在VC中实现。
集合
//Gather.h
#ifndef GATHER_H_
#define GATHER_H_
#include <iostream>
using namespace std;
template <typename T>
struct Node
{
T data;
Node<T> *next;
};
template <typename T>
class Gather
{
private:
Node<T> *head;
int m_length;
public:
Gather();
void m_Init(); //建立集合
void m_Whether(); //判断是否为集合
void m_Equal(Gather<T> &g); //判断集合相等
void m_Combine(Gather<T> &g); //集合并运算
void m_Interact(Gather<T> &g); //集合交运算
void m_Minus(Gather<T> &g); //集合差运算
void m_Show(Gather<T> &g);
void m_Destroy(); //释放内存
void m_Init_Menu(Gather<T> &g);
void m_MenuSelect(Gather<T> &g);
//Node<T>* Get_head(); //获取头指针
};
#endif
template <typename T>
Gather<T>::Gather()
{
head = new Node<T>;
head->next = NULL;
m_length = 0;
}
template <typename T>
void Gather<T>::m_Init()
{
int n = 0;
Node<T> *p, *q;
q = head;
if (head->next != NULL)
head->next = NULL;
cout << "请设置集合中元素的个数:";
cin >> n;
cout << "\n " << "请输入集合的元素:";
for (int i = 0; i < n; i++)
{
p = new Node<T>;
cin >> p->data;
q->next = p;
q = p;
q->next = NULL;
m_length++;
}
cout << "\n " << "集合建立完毕." << endl;
}
template <typename T>
void Gather<T>::m_Whether()
{
Node<T> *p, *q;
p = this->head;
if (p->next == NULL)
{
cout << "\n " << "没有集合,请确认是否已经创建.\n" << endl;
return;
}
p = p->next;
int n = 1;
while (p->next != NULL)
{
//static int n = 1;
if (n > m_length - 1)
break;
q = this->head;
for (int i = 0; i < n + 1; i++)
q = q->next;
while (q != NULL)
{
if (p->data == q->data)
{
cout << "\n " << "这不是一个集合." << endl;
cout << "\n 请重新执行.抱歉!!" << endl;
cout << "\n\n";
exit(EXIT_FAILURE);
}
q = q->next;
}
n++;
p = p->next;
}
n = 0;
cout << "\n\n";
}
template <typename T>
void Gather<T>::m_Equal(Gather<T> &g)
{
this->m_Show(g);
int t = 0;
Node<T> *p1, *p2;
p1 = this->head->next;
p2 = g.head->next;
if (this->m_length != g.m_length)
{
cout << "\n这两个集合不相等" << endl;
system("pause");
system("cls");
this->m_MenuSelect(g);
return;
}
while (p1 != NULL)
{
p2 = g.head->next;
while (p2 != NULL)
{
if (p1->data != p2->data)
p2 = p2->next;
else
{
t++;
p2 = p2->next;
}
}
p1 = p1->next;
}
if (t == m_length)
cout << "\n这两个集合相等" << endl;
else
cout << "\n这两个集合不相等" << endl;
system("pause");
system("cls");
this->m_MenuSelect(g);
}
template <typename T>
void Gather<T>::m_Combine(Gather<T> &g)
{
this->m_Show(g);
T a[100]; //记录集合,在操作后把集合还原
T b[100];
int t1_length = this->m_length;
int t2_length = g.m_length;
Node<T> *p1, *p2;
Node<T> *t;
int n = 0; //n为记录p1->data与集合二中所有元素比较的不相同个数
p1 = this->head->next;
p2 = g.head->next;
t = this->head;
int i = 0;
while (p1 != NULL)
{
a[i] = p1->data;
p1 = p1->next;
i++;
}
i = 0;
while (p2 != NULL)
{
b[i] = p2->data;
p2 = p2->next;
i++;
}
p1 = this->head->next;
p2 = g.head->next;
cout << "两个集合的并运算为" << endl;
cout << "\n{ " << p2->data;
while (p2->next != NULL)
{
cout << ", " << p2->next->data;
p2 = p2->next;
}
while (p1 != NULL)
{
p2 = g.head->next;
while (p2 != NULL)
{
if (p1->data == p2->data)
{
Node<T> *p1_t = p1;
p1 = p1->next;
t->next = p1_t->next;
delete p1_t;
this->m_length--;
break;
}
else
{
n++;
p2 = p2->next;
}
}
if (n == g.m_length)
{
cout << ", " << p1->data;
p1 = p1->next;
t = t->next;
n = 0;
}
n = 0;
}
cout << " }" << endl;
//还原被操作的集合
if (this->m_length != 0)
this->m_Destroy();
g.m_Destroy();
this->head = new Node<T>;
head->next = NULL;
p2 = head;
for (int i = 0; i < t1_length; i++)
{
p1 = new Node<T>;
p1->data = a[i];
p2->next = p1;
p1->next = NULL;
p2 = p1;
this->m_length++;
}
g.head = new Node<T>;
g.head->next = NULL;
p2 = g.head;
for (int i = 0; i < t2_length; i++)
{
p1 = new Node<T>;
p1->data = b[i];
p2->next = p1;
p1->next = NULL;
p2 = p1;
g.m_length++;
}
system("pause");
system("cls");
this->m_MenuSelect(g);
}
template <typename T>
void Gather<T>::m_Interact(Gather<T> &g)
{
this->m_Show(g);
bool t = false;
Node<T> *p1, *p2;
p1 = this->head->next;
p2 = g.head->next;
cout << "两个集合的交运算为" << endl;
cout << "\n{ ";
while (p1 != NULL)
{
p2 = g.head->next;
while (p2 != NULL)
{
if (p1->data == p2->data)
{
if (t == true)
{
cout << ", " << p1->data;
}
if (t == false)
{
cout << p1->data;
t = true;
}
break;
}
else
{
p2 = p2->next;
}
}
p1 = p1->next;
}
cout << " }" << endl;
system("pause");
system("cls");
this->m_MenuSelect(g);
}
template <typename T>
void Gather<T>::m_Minus(Gather<T> &g)
{
this->m_Show(g);
T a[100]; //定义一个数组记录集合,在操作后把集合还原
T b[100];
int t1_length = this->m_length;
int t2_length = g.m_length;
bool t = false;
Node<T> *p1, *p2;
Node<T> *o, *q;
p1 = this->head->next;
p2 = g.head->next;
q = this->head;
int i = 0;
while (p1 != NULL) //记录集合
{
a[i] = p1->data;
p1 = p1->next;
i++;
}
i = 0;
while (p2 != NULL)
{
b[i] = p2->data;
p2 = p2->next;
i++;
}
p1 = this->head->next;
cout << "两个集合的差运算为" << endl;
while (p1 != NULL)
{
t = false;
p2 = g.head->next;
while (p2 != NULL)
{
if (p1->data == p2->data)
{
o = p1;
p1 = p1->next;
q->next = o->next;
delete o;
this->m_length--;
t = true;
break;
}
p2 = p2->next;
}
if (t == false)
{
p1 = p1->next;
q = q->next;
}
}
if (this->m_length == 0)
{
cout << "\n空集." << endl;
}
else
{
cout << "\n{ ";
p1 = this->head->next;
cout << p1->data;
p1 = p1->next;
while (p1 != NULL)
{
cout << ", " << p1->data;
p1 = p1->next;
}
cout << " }" << endl;
}
//还原被操作的集合
if (this->m_length != 0)
this->m_Destroy();
g.m_Destroy();
this->head = new Node<T>;
head->next = NULL;
p2 = head;
for (int i = 0; i < t1_length; i++)
{
p1 = new Node<T>;
p1->data = a[i];
p2->next = p1;
p1->next = NULL;
p2 = p1;
this->m_length++;
}
g.head = new Node<T>;
g.head->next = NULL;
p2 = g.head;
for (int i = 0; i < t2_length; i++)
{
p1 = new Node<T>;
p1->data = b[i];
p2->next = p1;
p1->next = NULL;
p2 = p1;
g.m_length++;
}
p1 = this->head->next;
system("pause");
system("cls");
this->m_MenuSelect(g);
}
template <typename T>
void Gather<T>::m_Show(Gather<T> &g)
{
Node<T> *p;
p = this->head->next;
cout << " \n第一个集合为:" << endl;
cout << "\n{ " << p->data;
p = p->next;
while (p != NULL)
{
cout << ", " << p->data;
p = p->next;
}
cout << "}" << endl;
p = g.head->next;
cout << " \n第二个集合为:" << endl;
cout << "\n{ " << p->data;
p = p->next;
while (p != NULL)
{
cout << ", " << p->data;
p = p->next;
}
cout << "}\n" << endl;
}
template <typename T>
void Gather<T>::m_Destroy()
{
Node<T> *p, *q;
p = head;
q = p;
for (int i = 0; i < m_length; i++)
{
q = q->next;
delete p;
p = q;
}
m_length = 0;
}
template <typename T>
void Gather<T>::m_Init_Menu(Gather<T> &g)
{
cout << "┏----------------------------------------------------------------------------┓";
cout << "┃ ┃";
cout << "┠----------------------------- 集 合 运 算 ------------------原始版1.0----┨";
cout << "\n\n ";
this->m_Init();
this->m_Whether();
cout << "\n\n ";
g.m_Init();
g.m_Whether();
system("pause");
system("cls");
this->m_Show(g);
system("pause");
system("cls");
}
template <typename T>
void Gather<T>::m_MenuSelect(Gather<T> &g)
{
int ch;
cout << "┏----------------------------------------------------------------------------┓";
cout << "┃ ┃";
cout << "┠----------------------------- 集 合 运 算 ------------------原始版1.0----┨";
cout << "┃ ┃";
cout << "┃ ┃";
cout << "┃ 1. 相 等 ┃";
cout << "┃ 2. 并 运 算 ┃";
cout << "┃ 3. 交 运 算 ┃";
cout << "┃ 4. 差 运 算 ┃";
cout << "┃ 0. 退 出 ┃";
cout << "┃ ┃";
cout << "┃ ┃";
cout << "┠----------------------------------------------------------------------------┨";
cout << " ";
cout << " ";
cout << "请选择你要执行的选项:";
cin >> ch;
while (ch != 1 && ch != 2 && ch != 3 && ch != 4 && ch != 0)
{
cout << "\n没有该选项,请重新选择." << endl;
cout << "\n请选择你要执行的选项:";
cin >> ch;
}
switch (ch)
{
case 1:
system("cls");
this->m_Equal(g);
break;
case 2:
system("cls");
this->m_Combine(g);
break;
case 3:
system("cls");
this->m_Interact(g);
break;
case 4:
system("cls");
this->m_Minus(g);
break;
case 0:
cout << "\n 感谢你的使用!!" << endl;
this->m_Destroy();
g.m_Destroy();
exit(EXIT_FAILURE);
}
}
//Gather.cpp
#include "gather.h"
#include <iostream>
using namespace std;
int main()
{
Gather<int> gat1;
Gather<int> gat2;
gat1.m_Init_Menu(gat2);
gat1.m_MenuSelect(gat2);
return 0;
}
实验结果:
程序尚有不足,待补充待完善。