-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinventory_management3.c
263 lines (221 loc) · 8.31 KB
/
inventory_management3.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Structure for offline store stock
typedef struct {
char name[20];
int stock;
} OfflineStore;
// Structure for product data
typedef struct {
int item_number;
char item_name[20];
int price;
int online_stock_s;
int online_stock_m;
int online_stock_l;
OfflineStore offline_stores[3];
} Product;
// Structure for AVL tree node
typedef struct Node {
Product data;
struct Node* left;
struct Node* right;
int height;
} Node;
// Function to get the height of a node
int getHeight(Node* node) {
if (node == NULL)
return 0;
return node->height;
}
// Function to calculate the maximum of two integers
int max(int a, int b) {
return (a > b) ? a : b;
}
// Function to create a new node with the given data
Node* createNode(Product data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
newNode->height = 1;
return newNode;
}
// Function to right rotate a subtree rooted with node
Node* rightRotate(Node* node) {
Node* leftChild = node->left;
Node* rightGrandchild = leftChild->right;
// Perform rotation
leftChild->right = node;
node->left = rightGrandchild;
// Update heights
node->height = 1 + max(getHeight(node->left), getHeight(node->right));
leftChild->height = 1 + max(getHeight(leftChild->left), getHeight(leftChild->right));
// Return new root
return leftChild;
}
// Function to left rotate a subtree rooted with node
Node* leftRotate(Node* node) {
Node* rightChild = node->right;
Node* leftGrandchild = rightChild->left;
// Perform rotation
rightChild->left = node;
node->right = leftGrandchild;
// Update heights
node->height = 1 + max(getHeight(node->left), getHeight(node->right));
rightChild->height = 1 + max(getHeight(rightChild->left), getHeight(rightChild->right));
// Return new root
return rightChild;
}
// Function to get the balance factor of a node
int getBalance(Node* node) {
if (node == NULL)
return 0;
return getHeight(node->left) - getHeight(node->right);
}
// Function to insert a new node into the AVL tree
Node* insert(Node* node, Product data) {
if (node == NULL)
return createNode(data);
if (data.item_number < node->data.item_number)
node->left = insert(node->left, data);
else if (data.item_number > node->data.item_number)
node->right = insert(node->right, data);
else
return node; // Duplicate item numbers are not allowed
// Update the height of the current node
node->height = 1 + max(getHeight(node->left), getHeight(node->right));
// Check the balance factor and perform rotations if necessary
// Left Left case
if (getBalance(node) > 1 && data.item_number < node->left->data.item_number)
return rightRotate(node);
// Left Right case
if (getBalance(node) > 1 && data.item_number > node->left->data.item_number) {
node->left = leftRotate(node->left);
return rightRotate(node);
}
// Right Right case
if (getBalance(node) < -1 && data.item_number > node->right->data.item_number)
return leftRotate(node);
// Right Left case
if (getBalance(node) < -1 && data.item_number < node->right->data.item_number) {
node->right = rightRotate(node->right);
return leftRotate(node);
}
return node;
}
// Function to search for a specific item and size in the AVL tree
void search(Node* node, int item_number, char size) {
if (node == NULL) {
printf("Item not found.\n");
return;
}
if (item_number < node->data.item_number)
search(node->left, item_number, size);
else if (item_number > node->data.item_number)
search(node->right, item_number, size);
else {
printf("Item Number: %d\n", node->data.item_number);
printf("Item Name: %s\n", node->data.item_name);
printf("Price: %d\n", node->data.price);
if (size == 'S')
printf("Online Stock (Size S): %d\n", node->data.online_stock_s);
else if (size == 'M')
printf("Online Stock (Size M): %d\n", node->data.online_stock_m);
else if (size == 'L')
printf("Online Stock (Size L): %d\n", node->data.online_stock_l);
int offlineStockFound = 0;
for (int i = 0; i < 3; i++) {
if (node->data.offline_stores[i].stock > 0 && size == 'S') {
printf("Offline Stock (Store: %s, Size: S): %d\n", node->data.offline_stores[i].name,
node->data.offline_stores[i].stock);
offlineStockFound = 1;
} else if (node->data.offline_stores[i].stock > 0 && size == 'M') {
printf("Offline Stock (Store: %s, Size: M): %d\n", node->data.offline_stores[i].name,
node->data.offline_stores[i].stock);
offlineStockFound = 1;
} else if (node->data.offline_stores[i].stock > 0 && size == 'L') {
printf("Offline Stock (Store: %s, Size: L): %d\n", node->data.offline_stores[i].name,
node->data.offline_stores[i].stock);
offlineStockFound = 1;
}
}
if (!offlineStockFound)
printf("No offline stock available for size %c.\n", size);
}
}
// Function to traverse the AVL tree in inorder
void inorderTraversal(Node* node) {
if (node != NULL) {
inorderTraversal(node->left);
printf("Item Number: %d\n", node->data.item_number);
printf("Item Name: %s\n", node->data.item_name);
printf("Price: %d\n", node->data.price);
printf("Online Stock (Size S): %d\n", node->data.online_stock_s);
printf("Online Stock (Size M): %d\n", node->data.online_stock_m);
printf("Online Stock (Size L): %d\n", node->data.online_stock_l);
for (int i = 0; i < 3; i++) {
printf("Offline Stock (Store: %s, Size: S): %d\n", node->data.offline_stores[i].name,
node->data.offline_stores[i].stock);
printf("Offline Stock (Store: %s, Size: M): %d\n", node->data.offline_stores[i].name,
node->data.offline_stores[i].stock);
printf("Offline Stock (Store: %s, Size: L): %d\n", node->data.offline_stores[i].name,
node->data.offline_stores[i].stock);
}
printf("\n");
inorderTraversal(node->right);
}
}
int main(int argc, char* argv[]) {
Node* root = NULL;
if (argc < 2) {
printf("Insufficient arguments.\n");
return 1;
}
if (strcmp(argv[1], "add") == 0) {
if (argc != 26) {
printf("Invalid number of arguments.\n");
return 1;
}
int item_number = atoi(argv[2]);
char* item_name = argv[3];
int price = atoi(argv[4]);
int online_stock_s = atoi(argv[5]);
int online_stock_m = atoi(argv[7]);
int online_stock_l = atoi(argv[9]);
char* store_name1 = argv[11];
int offline_stock_s = atoi(argv[13]);
char* store_name2 = argv[15];
int offline_stock_m = atoi(argv[17]);
char* store_name3 = argv[19];
int offline_stock_l = atoi(argv[21]);
Product newProduct;
newProduct.item_number = item_number;
strcpy(newProduct.item_name, item_name);
newProduct.price = price;
newProduct.online_stock_s = online_stock_s;
newProduct.online_stock_m = online_stock_m;
newProduct.online_stock_l = online_stock_l;
strcpy(newProduct.offline_stores[0].name, store_name1);
newProduct.offline_stores[0].stock = offline_stock_s;
strcpy(newProduct.offline_stores[1].name, store_name2);
newProduct.offline_stores[1].stock = offline_stock_m;
strcpy(newProduct.offline_stores[2].name, store_name3);
newProduct.offline_stores[2].stock = offline_stock_l;
root = insert(root, newProduct);
printf("Item added successfully.\n");
} else if (strcmp(argv[1], "search") == 0) {
if (argc != 4) {
printf("Invalid number of arguments.\n");
return 1;
}
int item_number = atoi(argv[2]);
char size = argv[3][0];
search(root, item_number, size);
} else {
printf("Invalid command.\n");
return 1;
}
return 0;
}