-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZString.cpp
More file actions
73 lines (63 loc) · 1.14 KB
/
ZString.cpp
File metadata and controls
73 lines (63 loc) · 1.14 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
#include "stdafx.h"
#include "ZString.h"
#include "string.h"
ZString::ZString()
{
str = NULL;
}
ZString::ZString(const ZString& y)
{
if (!y.str)
str = NULL;
else
{
str = new char[strlen(y.str) + 1];
strcpy_s(str, strlen(y.str) + 1, y.str);
}
}
ZString::~ZString()
{
if (str)
{
delete []str;
}
}
ZString* ZString::operator + (ZString& y)
{
ZString* z = new ZString();
z->str = new char[y.getLength() + getLength() + 1];
strcpy_s(z->str, y.getLength() + getLength() + 1, str);
strcat_s(z->str, y.getLength() + getLength() + 1, y.str);
return z;
}
void ZString::operator = (ZString &y)
{
if (str != NULL)
delete[] str;
str = new char[y.getLength() + 1];
strcpy_s(str, y.getLength()+ 1, y.str);
}
void ZString::operator = (ZString* y)
{
if (str != NULL)
delete[] str;
str = new char[y->getLength() + 1];
strcpy_s(str,y->getLength()+ 1, y->str);
delete y;
}
istream& operator >> (istream& in, ZString y)
{
list<char>string;
char c;
while ((c = getchar()) != '\n')
string.push_back(c);
y.str = new char[string.size() + 1];
int i = 0;
for (auto &it : string)
{
y.str[i] = (char)it;
i++;
}
y.str[i] = '\0';
return in;
}