-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalClass.cpp
More file actions
102 lines (92 loc) · 2.07 KB
/
GlobalClass.cpp
File metadata and controls
102 lines (92 loc) · 2.07 KB
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
#include "GlobalClass.h"
#include <cstring>
std::ostream& operator<<(std::ostream &out, const DataClass &obj){
if (obj.type == DataType::INT){
int i;
memcpy(&i, &obj.data, 4);
out << i;
return out;
}
else if (obj.type == DataType::FLOAT){
double f;
memcpy(&f, &obj.data, 8);
out << f;
return out;
}
else{
char s[30];
memcpy(s, &obj.data, 30);
std::string str(s);
out << str;
return out;
}
}
//IntData::IntData() : DataType(DataType::INT){};
DataClass::DataClass(){}
DataClass::DataClass(int i){
type = DataType::INT;
data.i = i;
bytes = 4;
}
DataClass::DataClass(double f){
type = DataType::FLOAT;
data.f = f;
bytes = 8;
}
DataClass::DataClass(std::string str){
type = DataType::CHAR;
bytes = str.length();
strcpy(data.str, str.c_str());
}
bool DataClass::operator==(const DataClass &rhs)
{
switch (this->type)
{
case DataType::INT:
return this->data.i == rhs.data.i;
break;
case DataType::FLOAT:
return this->data.f == rhs.data.f;
break;
case DataType::CHAR:
if (strcmp(this->data.str, rhs.data.str) == 0)
return true;
else return false;
break;
default:
return false;
break;
}
}
bool DataClass::operator<(const DataClass &rhs)
{
switch (this->type)
{
case DataType::INT:
return this->data.i < rhs.data.i;
break;
case DataType::FLOAT:
return this->data.f < rhs.data.f;
break;
case DataType::CHAR:
if (strcmp(this->data.str, rhs.data.str) < 0)
return true;
else return false;
break;
default:
return false;
break;
}
}
bool DataClass::operator<=(const DataClass &rhs)
{
return *this < rhs || *this == rhs;
}
bool DataClass::operator>=(const DataClass &rhs)
{
return !(*this < rhs);
}
bool DataClass::operator>(const DataClass &rhs)
{
return !(*this <= rhs);
}