-
Notifications
You must be signed in to change notification settings - Fork 0
/
syslog.cpp
165 lines (130 loc) · 3.26 KB
/
syslog.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "syslog.h"
#include "sysentry.h"
#include <iostream>
using namespace std;
#include <cstring>
void Syslog::addEntry(const char *sys, const char *who)
{
if (strcmp(sys, this->mSysName) == 0)
{
int index = 0;
if (this->mEntries[0].getPerson() == nullptr)
{
this->mEntries[0].setPerson(who);
}
index = findPerson(who);
if (index != -1)
{
this->mEntries[index].incr();
}
else
{
index = findEmptyspot();
this->mEntries[index].setPerson(who);
this->mEntries[index].incr();
}
}
}
void Syslog::printAll()
{
cout << "System: " << this->mSysName << endl;
cout << " Top: " << getTop() << endl;
cout << " Sencond: " << getSecond() << endl;
cout << " All:" << endl;
for (int i = 0; i < findEmptyspot(); ++i)
{
cout << " " << this->mEntries[i].getPerson() << " "
<< this->mEntries[i].getCount() << " ("
<< static_cast<int>(getPercent(this->mEntries[i].getCount())) << "%)" << endl;
}
}
// Default Constructor
Syslog::Syslog(const char *sysname, const int numStaff)
{
this->mSysName = new char[strlen(sysname) + 1];
strcpy(this->mSysName, sysname);
this->mEntries = new Sysentry[numStaff];
this->mStaffNum = numStaff;
this->totalEntries = 0;
}
const char *Syslog::getTop()
{
int max = 0;
for (int i = 1; i < findEmptyspot(); ++i)
{
if (this->mEntries[i].getCount() > this->mEntries[max].getCount())
{
max = i;
}
}
return this->mEntries[max].getPerson();
}
const char *Syslog::getSecond()
{
int largest = 0;
int secondlargest = -1;
// int position = (largest > secondlargest) ? 0 : 1;
for (int i = 1; i < findEmptyspot(); i++)
{
if (this->mEntries[i].getCount() > this->mEntries[largest].getCount())
{
secondlargest = largest;
largest = i;
}
else if (this->mEntries[i].getCount() < this->mEntries[largest].getCount())
{
if (secondlargest == -1 || this->mEntries[secondlargest].getCount() < this->mEntries[i].getCount())
{
secondlargest = i;
}
}
}
return this->mEntries[secondlargest].getPerson();
}
int Syslog::findEmptyspot() const
{
int i = 0;
while (i < this->mStaffNum && this->mEntries[i].getPerson() != nullptr)
{
++i;
}
return i;
}
int Syslog::findPerson(const char *name)
{
for (int i = 0; i < findEmptyspot(); i++)
{
if (strcmp(name, this->mEntries[i].getPerson()) == 0)
{
return i;
}
}
return -1;
}
double Syslog::getPercent(int number) const
{
int listTotal = 0;
for (int i = 0; i < findEmptyspot(); i++)
{
listTotal += this->mEntries[i].getCount();
}
double percent = (number / static_cast<double>(listTotal)) * 100;
return percent;
}
Syslog::~Syslog()
{
delete[] this->mSysName;
delete[] this->mEntries;
}
/*Copy Constructor test
void Syslog::copySysentry() {
//Sysentry Copy(this->mEntries[0]);
cout << mEntries[0].getPerson() << " " << mEntries[0].getCount() << endl;
cout << Copy.getPerson() << " " << Copy.getCount() << endl;
}*/
/*Overloaded assignment operator test
void Syslog::assignmentoperator() {
Sysentry newObj = mEntries[1];
cout << mEntries[1].getPerson() << " " << mEntries[1].getCount() << endl;
cout << newObj.getPerson() << " " << newObj.getCount() << endl;
}*/