Скачиваний:
0
Добавлен:
08.08.2022
Размер:
19.77 Кб
Скачать
яю/*

;O :;0AA>2 87 ;@ 1, 2 @50;87>20BL:

1.	5@53@C7:C >?5@0F88 ?@8A20820=8O 4;O :;0AA0 List (:0: <5B>4 :;0AA0). !<KA; - A>740=85 ?>;=>9 :>?88 8AE>4=>3> A?8A:0 2 @57C;LB8@CNI5<.

2.	5@53@C7:0 >?5@0B>@0 + 4;O :;0AA0 List 2 2845 4@C30. !<KA;->1J548=5=85 42CE A?8A:>2 2 >48= (2B>@>9 A?8A>: ?@8A>548=O5BAO A?@020 : ?5@2><C).

3.	5@53@C78BL 2 :;0AA5 Node >?5@0F8N  - 2 2845 <5B>40 :;0AA0. !<KA;   C<5=LH8BL =0 1 2A5 M;5<5=BK <0AA820 arr.

4.	5@53@C78BL >?5@0F88 22>40 2K2>40 2 AB0=40@B=K5 ?>B>:8 std::cin std::cout 4;O :;0AA0 List.

*/



#include "pch.h"

#include <iostream>



const int SIZE = 3;



class Node

{

	float arr[SIZE];	// 0AA82 M;5<5=B>2 float

public:

	Node *pNext;		// #:070B5;L =0 A;54NCI89 Node



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

		this->pNext = nullptr;

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

			arr[i] = 0.0;

	}



	void show();		// 5B>4 Node - 2K2>4 M;-B>2 <0AA820 

	float summ();		// 5B>4 Node - cC<<0 M;-B>2 <0AA820



	float* get() {		// 5BB5@

		return arr;

	}

	void set(float arr[]) {	// !5BB5@

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

			this->arr[i] = arr[i];

	}



	void operator --(); // 5@53@C65==K9 >?5@0B>@ -- (?> 7040=8N) !<KA;   C<5=LH8BL =0 1 2A5 M;5<5=BK <0AA820 arr.

	// 5@53@C65==K5 >?5@0F88 22>40 2K2>40 2 AB0=40@B=K5 ?>B>:8 std::cin std::cout

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

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



	// 5AB@C:B>@

	~Node() {};

};



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

void Node::show()

{

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

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

	std::cout << std::endl;

}



//  0AG5B AC<<K 3E M;-B>2 <0AA820

float Node::summ()

{

	return (arr[0] + arr[1] + arr[2]);

}



//  50;870F8O ?> 7040=8N: C<5=LH8BL =0 1 2A5 M;5<5=BK <0AA820 arr.

void Node::operator--()

{

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

		this->arr[i]--;

}



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

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

{

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

		os << node.arr[i] << "\t";

	os << std::endl;



	return os;

}



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

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

{

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

		in >> node.arr[i];



	return in;

}



class List

{

public:

	Node *pHead;	// #:070B5;L =0 "3>;>2C" ;8AB0

	Node *pTail;	// #:070B5;L =0 "E2>AB" ;8AB0. (4;O 1KAB@>3> 4>102;5=8O M;-B>2 2 :>=5F A?8A:0)



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

		this->pHead = nullptr;

		this->pTail = nullptr;

	}

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

	List(float arr[][SIZE], int size) { // >=AB@C:B>@ (A>740=85 + 70?>;=5=85)

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

			this->pushBack(arr[i]);

	}//List

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



	void show(); // 5B>4 2K2>40 ;8AB0

	void pushBack(float arr[]); // > ACB8 A>7405B 2 :>=F5 List'a =>2K9 Node 8 70?>;=O5B 53> arr[]

	float avg(); // !G8B05B A@54=55



	// 5@53@C7:0 = (?> 7040=8N). !<KA; - A>740=85 ?>;=>9 :>?88 8AE>4=>3> A?8A:0 2 @57C;LB8@CNI5<.

	List operator =(const List& other); 

	// 5@53@C7:0 >?5@0B>@0+ (?> 7040=8N). 

	// !<KA; - >1J548=5=85 42CE A?8A:>2 2 >48= (2B>@>9 A?8A>: ?@8A>548=O5BAO A?@020 : ?5@2><C).

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

	// 5@53@C65==K5 >?5@0F88 22>40 2K2>40 2 AB0=40@B=K5 ?>B>:8 std::cin std::cout

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

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



	~List();

};



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

List::List(int size)

{

	// !>7405< =>2K9 Node

	pHead = new Node;

	pTail = pHead;



	// !>7405< size - 1 Node, ?>A:>;L:C ?5@2K9(head) C65 A>740=

	for (int i = 1; i < size; i++) { // i = 1 B.:. >48= M;5<5=B C65 A>740=

		pTail->pNext = new Node;

		pTail = pTail->pNext;

	}//for

	pTail->pNext = nullptr;

}



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

List::List(const List &other)

{

	this->pHead = nullptr;

	this->pTail = nullptr;

	Node * otherList = other.pHead;			// 15@5< 3>;>2C 87 List'a, A :>B>@>3> 1C45< :>?8@>20BL



	while (otherList) {						// "5305<" ?> otherList

		this->pushBack(otherList->get());	// >102;O5< <0AA82, ?>;CG5==K9 getter'><, 2 :>=5F =>2>3> List'a 

		otherList = otherList->pNext;		// 5@5E>48< : A;54. M;-BC List'a, A :>B>@>3> 1C45< :>?8@>20BL.

	}

}



// >102;O5< =>2K9 Node 2 :>=5F A?8A:0 A 8=8F80;870F59

void List::pushBack(float arr[])

{

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

		pHead = new Node;

		pHead->set(arr);			// 70?>;=5=85 <0AA820 40==K<8, :>B>@K5 225; ?>;L7>20B5;L

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



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

		return;

	}//if



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

	pTail->pNext->set(arr);			// 70?>;=5=85 <0AA820 40==K<8

	pTail->pNext->pNext = nullptr;

	pTail = pTail->pNext;			// 5@5E>4 =0 A>740==K9 M;5<5=B

}



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

float List::avg()

{

	Node *current = pHead;	// B5:CI89 2A?><>30B5;L=K9 Node = 3>;>25

	float avg = 0.0;

	int count = 0;



	while (current) {				// "568<" 4> :>=F0 ;8AB0

		avg += current->summ();		// K7K205< <5B>4 summ 2 :06>< Node 8 AC<<8@C5< MB>

		count++;					// >;-2> >1>945==KE Nod'>2 +1

		current = current->pNext;	// 5@5E>4 : A;54. Nod'C

	}//while



	return avg / (count * SIZE);	// 5 701K205< count * SIZE, ?>B><C GB> 2 :064>< Node 5ABL SIZE M-B>2 <0AA820

}



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



	// >A;5 B>3>, :0: >G8AB8;8 ?0<OBL, 4>102;O5< M;-BK

	Node * otherList = other.pHead;			// 15@5< 3>;>2C 87 List'a, A :>B>@>3> 1C45< :>?8@>20BL

	while (otherList) {

		this->pushBack(otherList->get());	// >102;O5< <0AA82, ?>;CG5==K9 getter'><, 2 :>=5F =>2>3> List'a 

		otherList = otherList->pNext;		// 5@5E>48< : A;54. M;-BC List'a, A :>B>@>3> 1C45< :>?8@>20BL.

	}//while



	return *this;

}



// 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=>.

		result.pushBack(temp->get());	// >102;O5< <0AA82, ?>;CG5==K9 getter'>< 87 temp'a, 2 :>=5F =>2>3> result

		temp = temp->pNext;				// 5@5E>48< : A;54. M;-BC temp'a, A :>B>@>3> 1C45< :>?8@>20BL.

	}

	

	return result;

}



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

		current = current->pNext;

	}



    return out;

}



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

// ?5@0B>@ 22>40 CAB@>5= B0:8< >1@07><, GB> 2 A;CG05 5A;8 :0:85-;81> 40==K5 C65 5ABL, B> >=8 =5 1C4CB C40;OBLAO.

// >2K5 Nod'K 1C4CB 4>102;5=K 2 :>=5F.  main MB> ?@>45<>=AB@8@>20==>.

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

{

	int size; // :>;-2> Nod'>2, :>B>@K5 E>G5B 225AB8 ?>;L7>20B5;L (0 - 2KE>4).

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

	std::cin >> size;



	for (int i = 0; i < size; i++){				// >102;O5< AB>;L:> Nod'>2 (C7;>2), A:>;L:> ?>;L7>20B5;L C:070; @0=55

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

			list.pHead = new Node;

			in >> *list.pHead;					// 70?>;=5=85 <0AA820 40==K<8 A :;0280BC@K. A?. ?5@53@C6. >?5@0B>@ 87 :;0AA0 Node

			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

			continue;

		}//if



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

		in >> *list.pTail->pNext;				// 70?>;=5=85 <0AA820 40==K<8 A :;0280BC@K. A?>;L7C5< ?5@53@C6. >?5@0B>@ 87 :;0AA0 Node

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

		list.pTail = list.pTail->pNext;			// 5@5E>4 =0 A>740==K9 M;5<5=B

	}//for



	return in;

}





int main()

{

	// AE>4=K5 40==K5, GB>1K 4>;3> =5 22>48BL A :;0280BC@K

	float arr[5][3] = { { -10, 20, 30  }, { 1, 2, 3 }, { 555, 666, 777}, { 11, 22, 33}, { 3, 2 , 1} };



	List x;			

	x.pushBack(arr[0]);	// >102;O5< 2 :>=5F List'a =>2K9 Node 8 8=8F80;878@5< 53> arr[0] = { -10, 20, 30  }

	x.pushBack(arr[1]);

	

	std::cout << "x (before operations):" << std::endl << x;



	List z;

	List a, b(2);

	a.pushBack(arr[2]);

	a.pushBack(arr[3]);



	x = (a + b);



	std::cout << "a:" << std::endl << a;

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

	std::cout << "\nx (after x = a + b):" << std::endl << x;



	--(*x.pHead);

	std::cout << "\nx (after operation x.pHead->operator--()):" << std::endl << x;



	std::cout << "\nOperation cin >> x:\n";

	std::cin >> x;

	std::cout << "\nx (after cin >> x):" << std::endl << x;

	std::cout << "Attention! Old nods won't be deleted. We just add new nodes to already existing nodes!" << std::endl;

	// =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<!

}



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