1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
| #include "queue.h"
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <time.h>
using namespace std;
//деструктор класу "queue"
queue::~queue()
{
clear();
}
//конструктор класу "queue"
queue::queue(): count(0),head(NULL){}
//-------------------------------------------------------------------
//функція додавання елемента у стек
void queue::push(virib* data)
{
pointer*ptr=new pointer();
ptr->per=data;
ptr->next=head;
head=ptr;
count++;
}
//-------------------------------------------------------------------
//функція виведення елементів стеку
void queue::Show()
{
if(head!=NULL)
{
pointer*ptr=head;
for (int i = 0; i <count; i++)
{
ptr->per->show();
ptr=ptr->next;
}
}
}
//-------------------------------------------------------------------
//функція видалення одного елемента стеку
void queue::pop()
{
if(count==0)
return;
else
{
pointer *ptr=head;
head=head->next;
delete ptr;
count--;
}
}
//-------------------------------------------------------------------
//функція видалення всіх елементів зі стеку
void queue::clear()
{
while (head!=0)
{
pop();
}
}
//-------------------------------------------------------------------
//функція пошуку елементу у стеку
string queue::found(string compname)
{
pointer* temphead=head;
for(int i=0;i<count;i++)
{
if(typeid(detal)==typeid(*temphead->per))
if(compname==temphead->per->get_name())
return temphead->per->get_material();
else
return "a";
temphead=temphead->next;
}//end of for(int i=0;i<count;i++)
return "a";
}
//--------------------------------------------------------------------
//функція збереження елементів стеку у файл
void queue::save(string filename)
{
ofstream fs;
fs.open(filename);
pointer* p=head;
while(p)
{
p->per->save(&fs);
p=p->next;
}
fs.close();
}
//--------------------------------------------------------------------
//функція завантаження елементів стеку з файлу
void queue::load(string filename)
{
int kilkist, kost,massa;
string name, material,colour;
ifstream fs;
fs.open(filename);
string p;
bool check;
while(!fs.eof())
{
getline(fs,p,'/');
if(p=="m")
check=true;
else
check=false;
getline(fs,p,'/');
name=p;
getline(fs,p,'/');
material=p;
getline(fs,p,'/');
kost=atof(p.c_str());
if(check==true)
{
getline(fs,p,'/');
kilkist=atof(p.c_str());
push(new usel(name,material,kost,kilkist));
}
else
{
getline(fs,p,'/');
massa=atof(p.c_str());
getline(fs,p,'/');
colour=p;
push(new detal(name, material, kost, massa, colour));
}
}
fs.close();
}
//-------------------------------------------------------------------
//функція швидкого сортування стеку
template<class T>
void quickSortR(T* a, int N)
{
// На вході - массив a[N]
int i = 0, j = N; // поставити вказівники на вихідні місця
T temp, p;
p = a[ N>>1 ]; // центральний елемент
// процедура розділення
do
{
while ( a[i] < p )
i++;
while ( a[j] > p )
j--;
if (i <= j)
{
temp = a[i]; a[i] = a[j]; a[j] = temp;
i++; j--;
}
}
while ( i<=j );
// рекурсивні виклики, якщо є що сортувати
if ( j > 0 )
quickSortR(a, j);
if ( N > i )
quickSortR(a+i, N-i);
}
void queue::queue_sorting()
{
virib**arr=new virib*[count];
for(int i=0; i<count; i++)
{
arr[i]=head->per;
pop();
}
quickSortR(arr,count);
} |