-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMerchant.cpp
150 lines (148 loc) · 5.07 KB
/
Merchant.cpp
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
#include "Merchant.h"
Merchant::Merchant(void)
{
count = 0;
SellerList = nullptr;
}
addseller_data Merchant::addSeller(unsigned int pincode)
{
addseller_data output;
if (count == 0)
{
SellerList = new SellersBlock;
SellerList->next = nullptr;
SellerList->data = new Seller[10000];
SellerList->data[0].id = count + 1;
SellerList->data[0].locations = new LocationData;
SellerList->data[0].locations->next = nullptr;
SellerList->data[0].locations->pins = new unsigned long long int[10]();
SellerList->data[0].locations->pins[0] = pincode * 1000000000000;
output.pointer = SellerList->data;
}
else
{
int location_number = count % 10000;
SellersBlock *traverse = SellerList;
while (traverse->next != nullptr)
{
traverse = traverse->next;
}
if (location_number == 0)
{
traverse->next = new SellersBlock;
traverse = traverse->next;
traverse->next = nullptr;
traverse->data = new Seller[10000];
traverse->data[0].id = count + 1;
traverse->data[0].locations = new LocationData;
traverse->data[0].locations->next = nullptr;
traverse->data[0].locations->pins = new unsigned long long int[10]();
traverse->data[0].locations->pins[0] = pincode * 1000000000000;
output.pointer = traverse->data;
}
else
{
traverse->data[location_number].id = count + 1;
traverse->data[location_number].locations = new LocationData;
traverse->data[location_number].locations->next = nullptr;
traverse->data[location_number].locations->pins = new unsigned long long int[10]();
traverse->data[location_number].locations->pins[0] = pincode * 1000000000000;
output.pointer = &(traverse->data[location_number]);
}
}
count++;
output.id = count;
return output;
}
Seller *Merchant::addPincode(unsigned long int user_id, unsigned int pincode)
{
if (user_id > count || user_id<1)
{
return nullptr;
}
int block_number = (user_id - 1) / 10000;
int seller_location = (user_id - 1) % 10000;
SellersBlock *traverse = SellerList;
for (int i = 0; i < block_number; i++)
{
traverse = traverse->next;
}
traverse->data[seller_location].addPin(pincode);
return &(traverse->data[seller_location]);
}
std::vector<unsigned int> Merchant::showLocations(unsigned long int user_id) const
{
std::vector<unsigned int> output;
if(user_id>count || user_id<1){
return output;
}
user_id--;
int block_number = user_id / 10000;
int seller_location = user_id % 10000;
SellersBlock *traverse = SellerList;
for (int i = 0; i < block_number; i++)
{
traverse = traverse->next;
}
LocationData *location_bock = traverse->data[seller_location].locations;
int pinlocation;
unsigned long long int pincode_number;
unsigned long long int outpush;
while (location_bock != nullptr)
{
pinlocation = 0;
while (location_bock->pins[pinlocation] != 0 && pinlocation < 10)
{
pincode_number = location_bock->pins[pinlocation];
outpush = pincode_number / 1000000000000;
output.push_back(outpush);
pincode_number = pincode_number % 1000000000000;
if (pincode_number != 0)
{
outpush = pincode_number / 1000000;
output.push_back(outpush);
pincode_number = pincode_number % 1000000;
}
if (pincode_number != 0)
{
output.push_back(pincode_number);
pincode_number = 0;
}
pinlocation++;
}
location_bock = location_bock->next;
}
return output;
}
bool Merchant::isServing(unsigned long int user_id, int pincode) const
{
user_id--;
if (user_id < count && user_id>-1)
{
int block_number = user_id / 10000;
int seller_location = user_id % 10000;
SellersBlock *traverse = SellerList;
for (int i = 0; i < block_number; i++)
{
traverse = traverse->next;
}
LocationData *traverse_location = traverse->data[seller_location].locations;
int first, second, third;
while (traverse_location != nullptr)
{
for (int i = 0; i < 10; i++)
{
first = traverse_location->pins[i] / 1000000000000;
second = traverse_location->pins[i] % 1000000000000;
third = second % 1000000;
second = second / 1000000;
if ((first == pincode) || (second == pincode) || (third == pincode))
{
return true;
}
}
traverse_location = traverse_location->next;
}
}
return false;
}