-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmystring.cpp
More file actions
151 lines (147 loc) · 4.03 KB
/
Copy pathmystring.cpp
File metadata and controls
151 lines (147 loc) · 4.03 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
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
#include "mystring.h"
#include <string.h>
#include <algorithm>
using namespace std;
// 1. default constructor
String::String():size_(0),capacity_(0){
str_ = new(nothrow) char[1];
if(str_)
str_[0] = '\0';
else
cout << "Warning: Memory Allocation Failure" << endl;
}
// 2. copy constructor
String::String(const String &rhs):size_(rhs.size_),capacity_(rhs.capacity_){
str_ = new(nothrow) char[rhs.capacity_];
if(str_){
for(size_t i = 0 ; i < rhs.size_ ; ++i )
str_[i] = rhs.str_[i];
str_[rhs.size_] = '\0';
}else{
cout << "Warning: Memory Allocation Failure" << endl;
size_ = capacity_ = 0;
}
}
// 3. constructor with one parameter with type const char *
String::String(const char *s):size_(strlen(s)),capacity_(size_){
str_ = new(nothrow) char[size_ + 1];
if(str_){
for(size_t i =0; i< size_; ++i)
str_[i] = s[i];
str_[size_] = '\0';
}else{
cout << "Warning: Memory Allocation Failure" << endl;
size_ = capacity_ = 0;
}
}
String::String(char c):size_(1),capacity_(1){
str_ = new(nothrow) char[2]{c,'\0'};
if(!str_){
cout << "Warning: Memory Allocation Failure" << endl;
size_ = capacity_ = 0;
}
}
// 8. operator +=
String & String::operator += (const String &rhs){
if(size_ + rhs.size_ > capacity_){
char* tmp = new(nothrow) char[size_+rhs.size_+1];
if(tmp){
for(size_t i=0;i<size_;++i)
tmp[i] = str_[i];
for(size_t i=0;i<rhs.size_;++i)
tmp[size_+i] = rhs.str_[i];
delete [] str_;
str_ = tmp;
size_ += rhs.size_;
capacity_ = size_;
str_[size_] = '\0';
}else{
cout << "Warning: Memory Allocation Failure" << endl;
}
}else{
for(size_t i = 0; i < rhs.size_ ; ++i)
str_[i+size_] = rhs.str_[i];
size_ += rhs.size_;
str_[size_] = '\0';
}
return *this;
}
String & String::operator += (char c){
String rhs(c);
*this += rhs;
return *this;
}
// 10. operator =
String & String::operator = (String rhs){
if(this->capacity_ > rhs.size_){
this->size_ = rhs.size();
for(size_t i=0 ; i < rhs.size() ; ++i )
this->str_[i] = rhs.str_[i];
this->str_[rhs.size_] = '\0';
}else{
this->swap(rhs);
}
return *this;
}
String & String::operator = (char c){
char rhs[]{c,'\0'};
*this = rhs;
return *this;
}
// 11. swap
void String::swap(String &rhs){
using std::swap;
swap(rhs.size_, size_);
swap(rhs.capacity_,capacity_);
swap(rhs.str_,str_);
}
// A. relational operators (<, >, <=, >=, ==, !=)
bool operator < (const String &lhs, const String &rhs){
return strcmp(lhs.str_,rhs.str_)< 0 ? true : false;
}
bool operator > (const String &lhs, const String &rhs){
return strcmp(lhs.str_,rhs.str_)> 0 ? true : false;
}
bool operator <= (const String &lhs, const String &rhs){
return lhs>rhs ? false : true;
}
bool operator >= (const String &lhs, const String &rhs){
return lhs<rhs ? false : true;
}
bool operator == (const String &lhs, const String &rhs){
return strcmp(lhs.str_,rhs.str_)== 0 ? true : false;
}
bool operator != (const String &lhs, const String &rhs){
return lhs==rhs ? false : true;
}
// B. operator <<
ostream & operator << (ostream &os, const String &s){
for(size_t i =0; i < s.size() ; ++i)
os << s[i];
return os;
}
// C. operator +
const String operator + (const String &lhs, const String &rhs){
String tmp(lhs);
tmp += rhs;
return tmp;
}
const String operator + (const String &lhs, char rhs){
String tmp(lhs);
tmp += rhs;
return tmp;
}
const String operator + (char lhs, const String &rhs){
String tmp(lhs);
tmp += rhs;
return tmp;
}
// D. extra
istream & operator >> (istream & is, String & s){
char c;
while(!isspace(c=is.get()) && c !='\n')
s+=c;
if(c!='\n')
is.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
return is;
}