Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Zvit_Spisok_4_Zavada.doc
Скачиваний:
0
Добавлен:
01.04.2025
Размер:
445 Кб
Скачать
☆

Частина 2. Застосування атд

//list.h

class SListNode {

public:

float C;

int X;

int Y;

int Z;

SListNode* next;

SListNode ();

SListNode(float c, int x, int y, int z);

};

class Clist{

public:

typedef SListNode *Clist::list;

static void print_list(list head,char c);

static void push_after( list tmp,float c,int x, int y, int z);

static void AddItem(list&, float, int, int, int);

static bool ValidPos(Clist::list head,Clist::list currPtr, SListNode checkItem);

static bool IsLess(SListNode a, SListNode b);

static int size (list head);

static void Add_Func (list head);

};

//list.cpp

#include "list.h"

#include <iostream>

using namespace std;

SListNode::SListNode ()

{

C = 0;

X = 0;

Y = 0;

Z = 0;

next = this;

}

SListNode::SListNode (float c, int x, int y, int z)

{

this->X = x;

this->Y = y;

this->Z = z;

this->C = c;

this->next = this;

}

int Clist::size(Clist::list head) //функція знаходження розміру списку

{

Clist::list tmp = head;

int count = 0;

while (tmp)

{

tmp = tmp ->next;

count++;

}

return count;

}

void Clist::print_list(Clist::list head, char c) //винкція виводу вмісту списку

{

int f = 1;

Clist::list tmp=head;

while (tmp->next != head)

{

cout<<tmp->C;

if (tmp->X > 0) cout<<" * X^"<<tmp->X;

if (tmp->Y > 0) cout<<" * Y^"<<tmp->Y;

if (tmp->Z > 0) cout<<" * Z^"<<tmp->Z;

if (tmp->next) cout<<" "<<c<<" ";

if ( (f % 3) == 0 ) cout<<endl<<" "<<c<<" ";

f++;

tmp = tmp->next;

}

cout<<tmp->C;

if (tmp->X > 0) cout<<" * X^"<<tmp->X;

if (tmp->Y > 0) cout<<" * Y^"<<tmp->Y;

if (tmp->Z > 0) cout<<" * Z^"<<tmp->Z;

cout<<endl;

return;

}

void Clist::push_after(Clist::list tmp, float c, int x, int y , int z) //функція додавання елемента після поточного елемента списку

{

SListNode* q = new SListNode;

q -> X = x;

q -> Y = y;

q -> Z = z;

q -> C = c;

q -> next = tmp -> next;

tmp -> next = q;

}

void Clist::AddItem(list &head, float c, int x, int y, int z) //функція добалення нового елемента в список

{

SListNode* insertItem = new SListNode(c, x, y, z); //створення ланки списку із заданими даними

if (NULL == head) // якщо список пустий

{

head = insertItem; //присвоюємо результуючому списку нашу ланку

return;

}

list currentPos = head; //поточна позиція

list insertPos = NULL;

while (!ValidPos(head,currentPos, *insertItem)) //поки список проходить перевірку на коректність

{

insertPos = currentPos; //переприсвоюємо позицію для встановлення

currentPos = currentPos->next; //рухаємось далі по списку

}

if (NULL == insertPos) // якщо елемент потрібно вставити в першу позицію

{

SListNode* temp = head;

insertItem->next = temp;

while (temp->next != head) //поки настпуний елемент != початковому (цинкілчний список)

{

temp = temp -> next; //рухаємось по списку

}

head = insertItem; //присвоюємо початку списку ланку яку необхідно додати

temp -> next = head;

}

else if (IsLess(*currentPos, *insertItem)) // перевіряється умова зменшення показників степеня

{

// додавання в середину списку

push_after(insertPos, c, x, y, z); //додавання після поточної позиції

}

else

{

// додавання в кінець списку

push_after(currentPos, c, x, y, z);

currentPos -> next -> next = head; //переприсвоювання хвоста списку

}

}

bool Clist::ValidPos(Clist::list head, Clist::list currPtr, SListNode checkItem) //перевірка на коректність

{

return (currPtr->next == head // перевірка чи наступний елемент не є початком

| IsLess(*currPtr, checkItem)); // перевірка чи виконується умова зменшення показників

}

bool Clist::IsLess(SListNode a, SListNode b) // if a < b (перевірка умови зменшення показників)

{

if (a.X < b.X) //якщо показник Х першого елемента що провіряється менший за другий ел.

{

return true; //повертаємо істину

}

else if (a.X > b.X) //якщо показник Х першого елемента що провіряється більший за другий ел.

{

return false; //повертаємо хибу

}

else if (a.Y < b.Y) //якщо показник Y першого елемента що провіряється менший за

{ // другий ел.

return true; //повертаємо істину

}

else if (a.Y > b.Y) //якщо показник Y першого елемента що провіряється більший за { //другий ел.

return false; //повертаємо хибу

}

else if (a.Z <= b.Z) //якщо показник Z першого елемента що провіряється менший або рівний за другий ел.

{

return true; //повертаємо істину

}

else //якщо показник Z першого елемента що провіряється більший за другий ел.

{

return false; //повертаємо хибу

}

}

void Clist::Add_Func(list head) //функція додавання поліномів

{

if (head->X == head -> next -> X) //якщо рівні степені Х

{

if (head->Y == head -> next -> Y) // і якщо рівні степені Y

{

if (head->Z == head -> next -> Z) // i якщо рівні степені Z

{

head -> C += head ->next->C; //виконується додавання коефіцієнтів С

head->next = head;

}

else return;

}

else return ;

}

else return;

}

//main.cpp

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

#include <iomanip>

#include "list.h"

#include <iostream>

using namespace std;

int main()

{

SListNode* head = NULL; //створення списку для зберігання поліномів

SListNode* add = NULL; //створення списку для додавання поліномів

int k,x,y,z;

float c ;

cout<<"Enter count of elements - ";

cin>>k;

cout<<endl;

for (int i=0; i<k; i++) //обробка вхідної послідовності

{

cout<<"Enter "<<(i+1)<<" koef C : ";

cin>>c;

cout<<"Enter "<<(i+1)<<" exponent X :";

cin>>x;

cout<<"Enter "<<(i+1)<<" exponent Y :";

cin>>y;

cout<<"Enter "<<(i+1)<<" exponent Z :";

cin>>z;

Clist::AddItem(head, c, x, y, z); //додавання елемента до списку

cout<<"\nPolynomials in list : \n";

Clist::print_list(head,'|'); //вивід наявних елементів списку

cout<<endl;

}

cout<<"\nEnter polynomials which must be added : \n";

for (int d = 1; d <= 2; d++){ //ввід поліномів, які мають бути додані між собою

cout<<"Enter "<<(d)<<" koef C : ";

cin>>c;

cout<<"Enter "<<(d)<<" exponent X :";

cin>>x;

cout<<"Enter "<<(d)<<" exponent Y :";

cin>>y;

cout<<"Enter "<<(d)<<" exponent Z :";

cin>>z;

Clist::AddItem(add, c, x, y, z);

}

cout<<"\nPolynomials in list : \n";

Clist::print_list(add,'|'); //елементи перед додаванням

cout<<"\nAfter add : \n";

Clist::Add_Func(add);

Clist::print_list(add,'+'); //елементи після додавання

_getch();

return 0;

}

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]