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

#include <iostream>



const int SIZE = 3;



// 07>2K9 :;0AA Node.

// @>872>4=K5 >B =53> Node1 - int b; Node2 - a[SIZE]

class Node

{

public:

	Node *pNext = nullptr;



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

	virtual void set() = 0;					// A5BB5@

	virtual void show() = 0;				// <5B>4 2K2>40 =0 M:@0= (87 ?@54K4CI8E ;01. @.)

	virtual float summ() = 0;				// <5B>4 AC<<K 2A5E M-B>2 Node

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



	virtual void Output(std::ostream&) = 0;	// 8A?. 2 operator<<

	virtual void Input(std::istream&) = 0;	// 8A?. 2 operator>>



	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;

}



// @>872>4=K9 >B Node

class Node1 : public Node {



	int b;



public:

	void set();

	int get() { return b; };

	void show();

	float summ();

	Node* copy();



	void operator --() { b--; };



	void Output(std::ostream&);

	void Input(std::istream&);

};



void Node1::set()

{

	std::cout << "b = ";

	std::cin >> b;

}



void Node1::show()

{

	std::cout << "b = " << b << std::endl;

}



float Node1::summ()

{

	return (float)b;

}



// >72@0I05B :>?8N

Node * Node1::copy()

{

	Node1 *tempNode = new Node1;

	tempNode->b = this->b;



	return tempNode;

}



void Node1::Output(std::ostream & os)

{

	os << "b = " << b << std::endl;

}



void Node1::Input(std::istream & in)

{

	std::cout << "b = ";

	in >> b;

}



// @>872>4=K9 >B Node

class Node2 : public Node {



	float a[SIZE];



public:

	void set();

	float* get() { return a; };

	void show();

	float summ();

	Node* copy();



	void operator --();



	void Output(std::ostream&);

	void Input(std::istream&);

};



void Node2::set()

{

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

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

		std::cin >> a[i];

	}

}



void Node2::show()

{

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

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

	std::cout << std::endl;

}



float Node2::summ()

{

	float temp = 0.0;

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

		temp += a[i];

	return temp;

}



Node * Node2::copy()

{

	Node2 *tempNode = new Node2;

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

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



	return tempNode;

}



void Node2::operator--()

{

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

		a[i]--;

}



void Node2::Output(std::ostream &os)

{

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

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

	os << std::endl;

}



void Node2::Input(std::istream &is)

{

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

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

		is >> a[i];

	}

}





class List

{

	int countNode1;		// countNodeX - 4;O @0AG5B0 avg(). >7=8:;8 B@C4=>AB8, B.:. A?8A>: =5>4=>@>4=K9

	int countNode2;		// >MB><C 2 ?5@5<5==K5

public:

	Node *pHead;		// #:070B5;L =0 3>;>2C List'a

	Node *pTail;		// #:070B5;L =0 E2>AB List'a (4;O 1KAB@>3> 4>102;5=O M;-B>2)



	List() {			// >=AB@C:B>@ ?> C<>;G0=8N

		pHead = nullptr;

		pTail = nullptr;

		countNode1 = 0;

		countNode2 = 0;

	}

	List(int size);		// >=AB@C:B>@ A 7040=K< :>;2->< M;-B>2

	List(const List &other);	// >=AB@C:B>@ :>?8@>20=8O



	void show();		// 5B>4 2K2>40 =0 M:@=0

	float avg();		// >4AG5B A@54=53> 0@8D<5B8G5A:>3>



	void addNode1();	// >1028BL Node1

	void addNode2();	// >1028BL Node2



	List operator =(const List& other);	// 5@53@C65==K9 >?5@0B>@ =

	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 Node1 2 :>=5F A?8A:0

void List::addNode1()

{

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

		pHead = new Node1;

		pHead->set();					// 22>4 7=0G. b = 

		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 = new Node1;		// @01>B05< A ?>A;54=8< M;5<5=B><. !>7405< ?>A;54=89->pNext = new Node;

		pTail->pNext->set();			// 22>4 7=0G. b = 

		pTail->pNext->pNext = nullptr;

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

	}//if

	countNode1++;

}



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

void List::addNode2()

{

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

		pHead = new Node2;

		pHead->set();					// 22>4 7=0G. <0AA820 0

		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 = new Node2;		// @01>B05< A ?>A;54=8< M;5<5=B><. !>7405< ?>A;54=89->pNext = new Node;

		pTail->pNext->set();			// 22>4 7=0G. <0AA820 0

		pTail->pNext->pNext = nullptr;

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

	}//if

	countNode2++;

}



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

List::List(int size)

{

	int typeNode;

	while (size--) {

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

		std::cin >> typeNode;



		if (typeNode == 1) {

			addNode1();

		} else {

			addNode2();

		}//if

	}//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;

	}



	this->countNode1 = other.countNode1;

	this->countNode2 = other.countNode2;



}//List



//  0AG5B A@54=53> 0@8D<5B8G5A:>3> 2> 2A5< A?8A:5

float List::avg()

{

	Node *current = pHead;

	float avg = 0.0;



	while (current) {

		avg += current->summ();

		current = current->pNext;

	}//while



	// countNode2 * SIZE, B.:. 2 Node2 A>45@68BAO SIZE M;-B>2 (<0AA82)



	return avg / (countNode2 * SIZE + countNode1);

}



// 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;

	}



	this->countNode1 = other.countNode1;

	this->countNode2 = other.countNode2;



	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;

	}



	result.countNode1 += b.countNode1;

	result.countNode2 += b.countNode2;



	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, typeNode; // count - :>;-2> Nod'>2, :>B>@K5 E>B8< 4>1028BL 2 List; typenod - B8? Nod'0;

	Node *node;

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

	std::cin >> count;



	while (count--) {

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

		in >> typeNode;



		if (typeNode == 1) {

			node = new Node1;

			list.countNode1++;

		} else {

			node = new Node2;

			list.countNode2++;

		}//if



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

			list.pHead = node;

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

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



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

		} else {

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

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

			list.pTail->pNext->pNext = nullptr;

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

		}//if

	}//while

	return in;

}//operator>>





int main()

{

	Node *node;

	Node1 node1;

	Node2 node2;



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

	node = &node1;



	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 = &node2;" << std::endl;

	node = &node2;



	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 семестр