-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubj.c
230 lines (209 loc) · 5.85 KB
/
subj.c
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "subj.h"
struct Base *Create(enum ItemType type) {
struct Base *base = NULL;
switch (type) {
case Detail:
base = (struct Base*)calloc(1, sizeof(struct Detail));
break;
case FurnitureSet:
base = (struct Base *)calloc(1, sizeof(struct FurnitureSet));
break;
case None:
printf("\nWrong item type!\n\n");
break;
}
if (base) base->type = type;
return base;
}
void InputDetail(struct Detail* detail) {
printf("%d\n\n", detail->type);
do {
printf("Enter detail name:\n> ");
detail->name = (char *)malloc(sizeof(char) * 100);
scanf(" %100[^\n]", detail->name);
if (detail->name != NULL)
break;
printf("Wrong input!\n");
fflush(stdin);
} while (1);
do {
printf("Enter detail length (mm):\n> ");
if (scanf("%lf", &detail->length)) break;
else printf("Wrong input!\n");
fflush(stdin);
} while (1);
do {
printf("Enter detail width (mm):\n> ");
if (scanf("%lf", &detail->width)) break;
else printf("Wrong input!\n");
fflush(stdin);
} while (1);
do {
printf("Enter detail code:\n> ");
if (scanf("%d", &detail->code)) break;
else printf("Wrong input!\n");
fflush(stdin);
} while (1);
do {
printf("Enter detail materil:\n> ");
detail->material = (char *)malloc(sizeof(char) * 100);
scanf(" %100[^\n]", detail->material);
if (detail->material != NULL)
break;
printf("Wrong input!\n");
fflush(stdin);
} while (1);
}
void InputFurnitureSet(struct FurnitureSet* furnitureSet) {
if (!furnitureSet)
return;
do {
fflush(stdin);
printf("Enter furniture name:\n> ");
furnitureSet->name = (char *)malloc(sizeof(char) * 100);
scanf(" %100[^\n]", furnitureSet->name);
if (furnitureSet->name != NULL)
break;
printf("Wrong input!\n");
} while (1);
do {
printf("Enter code:\n> ");
if (scanf("%d", &furnitureSet->code)) break;
else printf("Wrong input!\n");
fflush(stdin);
} while (1);
do {
printf("Enter furniture quantity:\n> ");
if (scanf("%d", &furnitureSet->quantity)) break;
else printf("Wrong input!\n");
fflush(stdin);
} while (1);
}
void InputItem(struct Base* base) {
if (!base)
return;
switch (base->type) {
case Detail:
InputDetail((struct Detail*)base);
break;
case FurnitureSet:
InputFurnitureSet((struct FurnitureSet*)base);
break;
default:
printf("Unknown object type!\n");
break;
}
}
void PrintDetail(struct Detail* detail) {
printf("Name: %s\t Code: %lf\n", detail->name, detail->code);
printf("Length: %lg\t Width: %lg\n", detail->length, detail->width);
printf("Apparent magnitude: %s\n\n", detail->material);
}
void PrintFurnitureSet(struct FurnitureSet* furnitureSet) {
printf("Name: %s\t Code: %lf\n", furnitureSet->name, furnitureSet->code);
printf("quantity: %d\n\n", furnitureSet->quantity);
}
void PrintItem(struct Base *base) {
if (!base)
return;
switch (base->type) {
case Detail:
PrintDetail((struct Detail*)base);
break;
case FurnitureSet:
PrintFurnitureSet((struct FurnitureSet*)base);
break;
}
}
void PrintSpaceItems(struct List* list) {
struct Base* curr;
int i = 0;
for (curr = (struct Base*)list->head; curr != NULL; curr = (struct Base*)curr->next) {
printf("%d\t%p\t%p\t%p\n", i, curr, curr->prev, curr->next);
PrintItem(curr);
i++;
}
printf("\n");
}
struct Base *SearchByName(struct List* list, char *name) {
struct Base* curr;
for (curr = (struct Base*)list->head; curr != NULL; curr = (struct Base*)curr->next) {
if (!strcmp(name, curr->name)) {
return curr;
}
}
}
void SearchByCode(struct List* list, double start, double end) {
struct Base* curr;
int i = 0;
for (curr = (struct Base*)list->head; curr != NULL; curr = (struct Base*)curr->next) {
if (curr->type == Detail) {
int code = ((struct Detail*)curr)->code;
if (code >= start && code <= end) {
printf("\n%d.\n", i);
PrintItem(curr);
i++;
}
}
}
}
void Swap(struct List *list, int i1) {
struct Item *tmp1 = Remove(list, i1);
Insert(list, tmp1, i1 + 1);
}
int Compare(struct Base* curr) {
if ((curr->type == Detail && ((struct Base*)curr->next)->type == Detail && strcmp(curr->name, ((struct Base*)curr->next)->name) > 0) ||
(curr->type == FurnitureSet && ((struct Base*)curr->next)->type == FurnitureSet && strcmp(curr->name, ((struct Base*)curr->next)->name) > 0))
return 1;
else
return 0;
}
void SortList(struct List *list) {
struct Base *curr, *currDet, *tmp;
int swapped = 1;
for (int i = 0; i < Count(list); i++) {
if (swapped == 0)
break;
swapped = 0;
for (curr = (struct Base*)list->head; curr != NULL; curr = ((struct Base*)curr->next)) {
if (curr->next != NULL) {
if (Compare(curr)) {
Swap(list, GetIndex(list, (struct Item*)curr));
swapped = 1;
if (curr->prev != NULL)
curr = ((struct Base*)curr->prev);
}
} else {
break;
}
}
}
int count;
curr = (struct Base*)list->head;
currDet = (struct Base*)list->head;
for (int i = 0; i < Count(list); i++) {
if (currDet->type == Detail) {
count = 0;
curr = (struct Base*)currDet->next;
for (int j = i + 1; j < Count(list); j++) {
if (curr->type != FurnitureSet) {
curr = (struct Base*)curr->next;
continue;
}
if (((struct FurnitureSet*)curr)->code == currDet->code) {
tmp = (struct Base*)curr->next;
curr = (struct Base*)Remove(list, j);
Insert(list, (struct Item*)curr, i + count + 1);
curr = tmp;
count++;
} else {
curr = (struct Base*)curr->next;
}
}
}
currDet = (struct Base*)currDet->next;
}
}