-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoperations.cpp
123 lines (116 loc) · 3.14 KB
/
operations.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
#include "string.h"
TString TString::operator+(const char* s)
{
if (s == 0) return *this;
TString A;
int len = length + Str_Len(s);
A.string = new char[len + 1];
A.length = len;
for (int i = 0; i < length - 1; i++) A.string[i] = string[i];
for (int j = length - 1; j < len; j++) A.string[j] = s[j - length + 1];
return A;
}
TString TString::operator+(const TString& p)
{
if (p.string == 0) return *this;
TString A;
int len = length + p.length - 1;
A.string = new char[len + 1];
A.length = len;
for (int i = 0; i < length - 1; i++) A.string[i] = string[i];
for (int j = length - 1; j < len; j++) A.string[j] = p.string[j - length + 1];
return A;
}
TString& TString::operator=(const TString& p)
{
if (this == &p) return *this;
if (string != 0) delete[] string;
length = p.length;
string = new char[length];
for (int i = 0; i < length; i++) string[i] = p.string[i];
return *this;
}
TString& TString::operator=(const char* s)
{
if (string != 0) delete[] string;
length = Str_Len(s) + 1;
string = new char[length];
for (int i = 0; i < length; i++) string[i] = s[i];
return *this;
}
TString& TString::operator+=(const char* s)
{
if (Str_Len(s) == 0) return *this;
int len = length + Str_Len(s);
char* c = new char[len + 1];
for (int i = 0; i < length - 1; i++) c[i] = string[i];
for (int j = length; j < len; j++) c[j] = s[j - length + 1];
if (string != 0) delete[] string;
string = c;
length = len;
return *this;
}
TString& TString::operator+=(const TString& p)
{
if (Str_Len(p.string) == 0) return *this;
int len = length + p.length - 1;
char* c = new char[len + 1];
for (int i = 0; i < length - 1; i++) c[i] = string[i];
for (int j = length - 1; j < len; j++) c[j] = p.string[j - length + 1];
if (string != 0) delete[] string;
string = c;
length = len;
return *this;
}
bool TString::operator==(TString& p)
{
if (length != p.length) return false;
for (int i = 0; i < length; i++)
{
if (string[i] != p.string[i]) return false;
}
return true;
}
bool TString::operator<(TString& p)
{
int len = length > p.length ? p.length : length;
for (int i = 0; i < len; i++)
{
if (string[i] < p.string[i]) return true;
else if (string[i] > p.string[i]) return false;
}
if (length >= p.length) return false;
else return true;
}
bool TString::operator>(TString& p)
{
int len = length > p.length ? p.length : length;
for (int i = 0; i < len; i++)
{
if (string[i] < p.string[i]) return false;
else if (string[i] > p.string[i]) return true;
}
if (length >= p.length) return true;
else return false;
}
std::ostream& operator<<(std::ostream& B, TString& A)
{
for (int i = 0; i < A.length; i++) B << A.string[i];
return B;
}
std::istream& operator>>(std::istream& B, TString& A)
{
B >> A.length;
A.length++;
if (A.string != 0) delete[] A.string;
A.string = new char[A.length];
for (int i = 0; i < A.length - 1; i++) B >> A.string[i];
A.string[A.length - 1] = '\0';
return B;
}
char& TString::operator[](int n)
{
if (string == 0) throw "String is empty";
if (n > length - 1 || n < 0) throw "Out of range";
return string[n];
}