This repository has been archived by the owner on Sep 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProduct.cpp
121 lines (102 loc) · 2.08 KB
/
Product.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
#include "Product.h"
Product& Product::operator =(const Product& op2)
{
name_len_filled = op2.name_len_filled;
quantity = op2.quantity;
price = op2.price;
for (int i = 0; i < name_len_filled + 1; i++)
{
name[i] = op2.name[i];
}
return *this;
}
Product::~Product()
{
delete[] name;
}
Product::Product()
{
name = new char[name_len];
name_len_filled = 0;
quantity = 0;
price = -1;
}
Product::Product(const Product& op2)
{
name_len_filled = op2.name_len_filled;
quantity = op2.quantity;
price = op2.price;
name = new char[name_len];
for (int i = 0; i < (name_len_filled + 1); i++) //Plus 1 because there's i number of meaningful symbols (name[i-1] is the last meaningful one) and '\0' at name[i].
{
name[i] = op2.name[i];
}
}
int Product::LexicographicallyCompareNames(const Product& op2)
{
int which_is_greater = -1;
for (int i = 0; (i < name_len_filled) && (i < op2.name_len_filled); i++)
{
if (name[i] > op2.name[i])
{
which_is_greater = 0;
break;
}
else if (name[i] < op2.name[i])
{
which_is_greater = 1;
break;
}
}
if (which_is_greater == -1)
{
if (name_len_filled < op2.name_len_filled)
{
which_is_greater = 1;
}
else if (name_len_filled > op2.name_len_filled)
{
which_is_greater = 0;
}
}
return which_is_greater;
}
int Product::LexicographicallyCompareNames(char* myname)
{
Product op2;
for (int i = 0; i < name_len; i++)
{
op2.name[i] = myname[i];
if (op2.name[i] == '\0')
{
op2.name_len_filled = i;
break;
}
}
int which_is_greater = -1;
for (int i = 0; (i < name_len_filled) && (i < op2.name_len_filled); i++)
{
if (name[i] > op2.name[i])
{
which_is_greater = 0;
break;
}
else if (name[i] < op2.name[i])
{
which_is_greater = 1;
break;
}
}
if (which_is_greater == -1)
{
if (name_len_filled < op2.name_len_filled)
{
which_is_greater = 1;
}
else if (name_len_filled > op2.name_len_filled)
{
which_is_greater = 0;
}
}
return which_is_greater;
}