Скачиваний:
0
Добавлен:
08.08.2022
Размер:
15.43 Кб
Скачать
яю#include "pch.h"

#include <iostream>



const int SIZE = 3;



class Node

{

public:

	Node *pNext = nullptr;



	virtual void set() = 0;					// virtual void f() = 0 - G8AB> 28@BC0;L=0O DC=:F8O

	virtual void show() = 0;

	virtual float summ() = 0;

	virtual Node* copy() = 0;



	virtual void Output(std::ostream&) = 0;

	virtual void Input(std::istream&) = 0;



	virtual void operator --() = 0;

	friend std::ostream & operator<< (std::ostream &os, Node &node);

	friend std::istream & operator>> (std::istream &is, Node &node);

};



std::ostream & operator<<(std::ostream & os, Node & node)

{

	node.Output(os);

	return os;

}



std::istream & operator>>(std::istream & is, Node & node)

{

	node.Input(is);

	return is;

}



template<class T>

class TNode : public Node {

public:



	T a[SIZE];



	void set();

	//TNode<T> get() { return a; };

	void show();

	float summ();

	Node* copy();



	void operator --();



	void Output(std::ostream&);

	void Input(std::istream&);

};



template<class T>

void TNode<T>::set()

{

	for (int i = 0; i < SIZE; i++) {

		std::cout << "a[" << i << "] = ";

		std::cin >> a[i];

	}

}



template<class T>

void TNode<T>::show()

{

	for (int i = 0; i < SIZE; i++)

		std::cout << a[i] << "\t";

	std::cout << std::endl;

}



template<class T>

float TNode<T>::summ()

{

	float temp = 0.0;

	for (int i = 0; i < SIZE; i++)

		temp += a[i];

	return temp;

}



template<class T>

Node * TNode<T>::copy()

{

	TNode<T> *tempNode = new TNode<T>;

	for (int i = 0; i < SIZE; i++)

		tempNode->a[i] = this->a[i];



	return tempNode;

}



template<class T>

void TNode<T>::operator--()

{

	for (int i = 0; i < SIZE; i++)

		a[i]--;

}



template<class T>

void TNode<T>::Output(std::ostream &os)

{

	for (int i = 0; i < SIZE; i++)

		os << a[i] << " ";

	os << std::endl;

}



template<class T>

void TNode<T>::Input(std::istream &is)

{

	for (int i = 0; i < SIZE; i++) {

		std::cout << "a[" << i << "] = ";

		is >> a[i];

	}

}





class List

{

public:

	Node *pHead;

	Node *pTail;



	List() {

		pHead = nullptr;

		pTail = nullptr;

	}

	List(int size);

	List(const List &other);



	void show();

	void addTNode();



	List operator =(const List& other);

	friend List operator +(const List& a, const List& b);

	friend std::ostream & operator<< (std::ostream &os, const List &list);

	friend std::istream & operator>> (std::istream &is, List &list);



	~List();

};



// >102;O5< =>2K9 TNode 2 :>=5F A?8A:0

void List::addTNode()

{

	int typeNode;

	std::cout << "Enter the type of node (1 - float; 2 - int): ";

	std::cin >> typeNode;



	Node *node;



	if (typeNode == 1) {

		node = new TNode<float>;

	}

	else {

		node = new TNode<int>;

	}//if



	if (!pHead) {						// @01>B05< A "3>;>2>9". A;8 =8G53> =5B, B> A>7405<

		pHead = node;

		pHead->set();					// 22>4 7=0G. (int 0[SIZE] 8;8 float a[SIZE])

		pHead->pNext = nullptr;			// A;54 C:070B5;L C:07K205B =0 nullptr



		pTail = pHead;					// ?>A;54=89 M;5<5=B - 3>;>20

	}

	else {

		pTail->pNext = node;			// @01>B05< A ?>A;54=8< M;5<5=B><. !>7405< ?>A;54=89->pNext = new Node (node);

		pTail->pNext->set();			// 22>4 7=0G. (int 0[SIZE] 8;8 float a[SIZE])

		pTail->pNext->pNext = nullptr;

		pTail = pTail->pNext;			// 5@5E>4 

	}//if

}



// >=AB@C:B>@ A :>;-2>< M;-B>2

List::List(int size)

{

	while (size--) {

		addTNode();

	}//while

}



// >=AB@C:B>@ :>?8@>20=8O

List::List(const List &other)

{

	Node *otherList = other.pHead;



	Node *tempList = otherList->copy();

	this->pHead = tempList;

	this->pTail = this->pHead;

	otherList = otherList->pNext;



	while (otherList) {

		Node * tempList = otherList->copy();

		this->pTail->pNext = tempList;

		this->pTail = tempList;

		otherList = otherList->pNext;

	}

}//List



// K2>4 =0 M:@0=

void List::show()

{

	Node *current = pHead;	 // A>740Q< 2A?><>30B5;L=CN ?5@5<5==CN.

	while (current) {

		current->show();

		current = current->pNext;

	}

}



// 5@53@C7:0 >?5@0B>@0 ?@8A20820=8O 4;O :;0AA0 List

List List::operator=(const List & other)

{

	// G8AB:0 AB0@KE Nod'>2 (2K7K20N 45AB@C:B>@)	

	this->~List();



	Node *otherList = other.pHead;



	// >M;5<5=B=>5 4>102;5=85 Nod'>2 A otherList

	Node *tempList = otherList->copy(); // copy - 2>72@0I05B :>?8N

	this->pHead = tempList;

	this->pTail = this->pHead;

	otherList = otherList->pNext;



	while (otherList) {

		Node * tempList = otherList->copy();

		this->pTail->pNext = tempList;

		this->pTail = tempList;

		otherList = otherList->pNext;

	}



	return *this;

}//operator=



// 5AB@C:B>@

List::~List()

{

	Node *temp;



	while (pHead) {

		temp = pHead;

		pHead = pHead->pNext;

		delete temp;

	}//while

	//std::cout << "~List();" << std::endl;

}



// 5@53@C7:0 >?5@0B>@0 A;>65=8O 4;O :;0AA0 List

List operator+(const List & a, const List & b)

{

	List result = a;					// result - @57C;LB8@CNI0O ?5@5<5==0O, 2 :>B>@>9 1C45B =0E8>48BLAO A?8A>: 0 + b (: :>=FC A?8A:0 0 4>102;O5< A?8A>: b),

										// 8A?. :>=AB@C:B>@ :>?8@>20=8O (B.5. 2 result C65 ;568B A?8A>: 0).

	Node *temp = b.pHead;				// temp - 2@5<5==0O ?5@5<5==0O 4;O "GB5=8O" A?8A:0 b; 

	while (temp) {						//  result C =0A C65 5ABL A?8A>: 0. >102;O5< : =5<C 2 :>=5F A?8A>: b ?>M;5<5=B=>.

		Node *newNode = temp->copy();	// !>7405< :>?8N <5B>4>< copy 8 ?><5I05< 5Q 2 newNode;

		result.pTail->pNext = newNode;

		result.pTail = newNode;

		temp = temp->pNext;

	}



	return result;

}//operator+



// 5@53@C7:0 >?5@0B>@0 2K2>40 4;O :;0AA0 List

std::ostream & operator<<(std::ostream & out, const List & list)

{

	Node *current = list.pHead;				// A>740Q< 2A?><>30B5;L=CN ?5@5<5==CN.

	while (current) {

		out << *current;					// K2>48< Nod'K (8A?. 28@BC0;L=CN DC=:F8N)

		current = current->pNext;

	}



	return out;

}//operator<<



// 5@53@C7:0 >?5@0B>@0 22>40 4;O :;0AA0 List

// =8<0=85! !B0@K5 Nod'K (C7;K) =5 1C4CB C40;5=K. K ?@>AB> 4>102;O5< =>2K5 C7;K : C65 ACI5AB2CNI8< C7;0<!

std::istream & operator>>(std::istream & in, List & list)

{

	int count; // count - :>;-2> Nod'>2, :>B>@K5 E>B8< 4>1028BL 2 List; 

	Node *node;

	std::cout << "Enter the number of nodes (0 - exit): ";

	in >> count;



	while (count--) {

		list.addTNode();

	}//while

	return in;

}//operator>>





int main()

{

	Node *node;

	TNode<float> nodeFloat;

	TNode<int> nodeInt;



	std::cout << "node = &nodeFloat;" << std::endl;

	node = &nodeFloat;



	std::cout << "Operator >>: ";

	std::cin >> *node;

	std::cout << "Operator <<: " << *node << std::endl;



	--(*node);

	std::cout << "Operator --" << std::endl;

	std::cout << "Operator <<: " << *node;





	std::cout << "\n\nnode = &nodeInt;" << std::endl;

	node = &nodeInt;



	std::cout << "Operator >>: ";

	std::cin >> *node;

	std::cout << "Operator <<: " << *node << std::endl;



	--(*node);

	std::cout << "Operator --" << std::endl;

	std::cout << "Operator <<: " << *node << "\n\n";



	///////////////////////////////////////////////////////////////



	List list, list1, list2;

	std::cout << "Operator >> for list:" << std::endl;

	std::cin >> list;

	std::cout << "Operator << for list:\n" << list << "end" << std::endl;



	std::cout << "\nOperator >> for list1:" << std::endl;

	std::cin >> list1;

	std::cout << "Operator << for list1:\n" << list1 << "end" << std::endl;



	std::cout << "\nOperator >> for list2:" << std::endl;

	std::cin >> list2;

	std::cout << "Operator << for list2:\n" << list2 << "end" << std::endl;



	list = list1 + list2;

	std::cout << "\n\nOperator << for list = list1 + list2:\n" << list << "end" << std::endl;



	List list4 = list1;

	std::cout << "\n\nOperator << for list4 = list1:\n" << list4 << "end" << std::endl;

}



Соседние файлы в папке УТС 5 семестр