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

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

//list.h

class SListNode {

public:

float C;

int n;

SListNode* next;

SListNode ();

};

class Clist{

public:

typedef SListNode *Clist::list;

static list F(list haed, int x);

static void print_list(list head);

static void push_after(list tmp, int n, float c);

static void push_before(list tmp, int n, float c);

static list Funktion (list head);

};

//list.cpp

#include "list.h"

#include <iostream>

using namespace std;

SListNode::SListNode ()

{

C = 1;

n = 1;

next = NULL;

}

Clist::list Clist::F(Clist::list head, int x) // функція сортування

{

Clist::list tmp1 = head,tmp = head;

while(tmp1)

{

if(tmp1->n > x)

tmp=tmp1;

tmp1=tmp1->next;

}

return tmp;

}

void Clist::print_list(Clist::list head) //вивід списку

{

Clist::list tmp=head;

while (tmp)

{

cout<<tmp->C;

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

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

tmp = tmp->next;

}

cout<<endl;

return;

}

Clist::list Clist::Funktion (Clist::list head) //функція диференціювання

{

SListNode* tmp = new SListNode();

SListNode* tmp1 = new SListNode();

float c = (head -> C)*(tmp -> n);

tmp -> C = c;

tmp -> n = head -> n-1;

while (head)

{

push_after(tmp,(head ->n-1),(head ->C * head->n));

head = head -> next;

}

tmp1 -> C = tmp -> C;

tmp1 -> n = tmp -> n;

tmp = tmp -> next;

while (tmp)

{

push_after(tmp1,(tmp ->n),tmp ->C);

tmp = tmp -> next;

}

return tmp1->next;

}

void Clist::push_after(Clist::list tmp, int n,float c) //дадавання у список після заданого елемента

{

SListNode* q = new SListNode;

q -> n = n;

q -> C = c;

q -> next = tmp -> next;

tmp -> next = q;

}

void Clist::push_before(Clist::list tmp, int n, float c) //додавання перед заданим елементом

{

SListNode* q = new SListNode;

q->n = tmp->n;

q->C = tmp->C;

q->next=tmp->next;

tmp->n = n;

tmp->C = c;

tmp->next = q;

}

//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 = new SListNode; // створення нового елемента типу Список

int k,n;

bool empty = true;

float c;

cout<<"Enter count of elements - ";

cin>>k;

cout<<endl;

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

{

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

cin>>c;

cout<<"\nEnter "<<(i+1)<<" pokaznuk n :";

cin>>n;

if(!empty)

{

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

if(head->n < n)

//то ел. що розгл. додається на початок списку Clist::push_before(head,n,c);

else //якщо це не так

{

head = Clist::F(head,n); //кинонується сортування списку

Clist::push_after(head,n,c);

//елемент додається у відповідну позицію

}

}

if(empty)//якшо список порожній то многочлен просто додається у список

{

head->n = n;

head->C = c;

empty = false;

}

cout<<"Mnogochlen : \n";

Clist::print_list(head);

}

cout<<"After dufrentiuvania : \n";

head = Clist::Funktion(head);

Clist::print_list(head);

_getch();

return 0;

}

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