Задание:
Описать шаблон класса «множество», позволяющий выполнять основные операции – добавление и удаление элемента, пересечение, объединение и разность множеств.
Написать программу, демонстрирующую работу с этим шаблоном для различных типов данных.
Текст программы:
Файл «ENSEMBLE.CPP»:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
#include <memory.h> #include <iostream.h> template <class T> class ENSEMBLE { class NODE{ public: T Data; NODE *Next; NODE *Previous; }; private: NODE *first; public: ENSEMBLE(); int Insert(T a); int Remove(T a); ~ENSEMBLE(); void Out(); const ENSEMBLE<T> Crossing(ENSEMBLE<T>&a); const ENSEMBLE<T> Merging(ENSEMBLE<T> &a); const ENSEMBLE<T>& operator = (ENSEMBLE<T> &a) { NODE *cur=first; if (cur) { if ( cur->Next) cur=cur->Next; while (cur->Next) { delete cur->Previous; cur=cur->Next; } delete cur ; } first=NULL; cur=a.first; while (cur) { Insert(cur->Data); cur=cur->Next; } } } ; template <class T> void ENSEMBLE<T>::ENSEMBLE() { first=NULL; } template <class T> void ENSEMBLE<T>::~ENSEMBLE() { NODE *cur=first; if (cur) { if ( cur->Next) cur=cur->Next; while (cur->Next) { delete cur->Previous; cur=cur->Next; } delete cur ; } first=NULL; } template <class T> int ENSEMBLE<T>::Insert(T a) { NODE *cur=first; if (!cur) { NODE * Node=new NODE; if (!Node) return 0; NODE *lfirst=first; first=Node; Node->Data=a; Node->Previous=NULL; Node->Next=NULL; } else { while (cur->Next) { if (cur->Data==a) return 0; cur=cur->Next; } NODE * Node=new NODE; if (!Node) return 0; cur->Next=Node; Node->Data=a; Node->Previous=cur; Node->Next=NULL; } return 1; } template <class T> int ENSEMBLE<T>::Remove(T a) { NODE *cur=first; if (cur==NULL) return 0; while (cur->Next) { if (cur->Data==a) { if (cur->Previous) cur->Previous->Next=cur->Next; if (cur->Next) cur->Next->Previous=cur->Previous; delete cur; break; } cur=cur->Next; } return 1; } template <class T> void ENSEMBLE<T>::Out() { NODE *cur=first; while (cur) { cout<< cur->Data<<" "; cur=cur->Next; } cout<<endl; } template <class T> ENSEMBLE<T> ENSEMBLE<T>::Crossing(ENSEMBLE<T> & a) { ENSEMBLE en; if ((!first)||(!a.first)) return ENSEMBLE<T>(en); NODE *cur1=first; while (cur1) { NODE *cur2=a.first; while (cur2) { if (cur2->Data==cur1->Data) en.Insert(cur1->Data); cur2=cur2->Next; } cur1=cur1->Next; } return ENSEMBLE<T>(en); } template <class T> ENSEMBLE<T> ENSEMBLE<T>::Merging(ENSEMBLE<T> &a) { ENSEMBLE en; NODE *cur; cur=first; while (cur) { en.Insert(cur->Data); cur=cur->Next; } cur=a.first; while (cur) { en.Insert(cur->Data); cur=cur->Next; } cur=en.first; while (cur) cur=cur->Next; return ENSEMBLE<T>(en); } |
Файл «MAIN.CPP»:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include "ENSEMBLE.CPP" void main() { ENSEMBLE<int> en,en2; en.Insert(3); en.Insert(4); en.Insert(5); en2.Insert(7); en2.Insert(4); en2.Insert(3); en.Out(); en2.Out(); ENSEMBLE<int> en3=(en.Crossing(en2)); cout<<"РЕЗУЛЬТАТ: "; en3.Out(); } |
Вывод:
Были изучены шаблоны в С++.