프로그램 작업:MyString 클래스
이것은 첫 번째 문제인데, 직접 쓴 것이다.
#include <string.h>
#include <stdlib.h>
#include <string>
#include <iostream>
using namespace std;
class MyString
{
public:
MyString();
MyString(char*);
MyString(const MyString&);
int getlen() ;
char& operator [] (int);
char* operator () (int, int);
MyString operator += (const char*);
friend ostream& operator << (ostream& output, const MyString&);
friend MyString operator + (const MyString&, const MyString&);
friend bool operator > (MyString&, MyString&);
friend bool operator < (MyString&, MyString&);
friend bool operator == (MyString&, MyString&);
private:
char* str;
};
int CompareString( const void * e1, const void * e2)
{
MyString * s1 = (MyString * ) e1;
MyString * s2 = (MyString * ) e2;
if( * s1 < *s2 ) return -1;
else if( *s1 == *s2) return 0;
else if( *s1 > *s2 ) return 1;
}
int main()
{
MyString s1("abcd-"),s2,s3("efgh-"),s4(s1);
MyString SArray[4] = {"big","me","about","take"};
cout << "1. " << s1 << s2 << s3<< s4<< endl;
s4 = s3;
s3 = s1 + s3;
cout << "2. " << s1 << endl;
cout << "3. " << s2 << endl;
cout << "4. " << s3 << endl;
cout << "5. " << s4 << endl;
cout << "6. " << s1[2] << endl;
s2 = s1;
s1 = "ijkl-";
s1[2] = 'A' ;
cout << "7. " << s2 << endl;
cout << "8. " << s1 << endl;
s1 += "mnop";
cout << "9. " << s1 << endl;
s4 = "qrst-" + s2;
cout << "10. " << s4 << endl;
s1 = s2 + s4 + " uvw " + "xyz";
cout << "11. " << s1 << endl;
qsort(SArray,4,sizeof(MyString),CompareString);
for( int i = 0;i < 4;i ++ )
cout << SArray[i] << endl;
// s1 0 4
cout << s1(0,4) << endl;
// s1 5 10
cout << s1(5,10) << endl;
}
MyString::MyString()
{
if(str) delete str;
str = new char [1];
str[0] = 0;
}
MyString::MyString(char* x_)
{
int len = strlen(x_);
if(str) delete str;
str = new char [len + 1];
strcpy(str, x_);
}
MyString::MyString(const MyString& x_)
{
int len = strlen(x_.str);
if(str) delete str;
str = new char [len + 1];
strcpy(str, x_.str);
}
int MyString::getlen()
{
return strlen(str);
}
ostream& operator << (ostream& output, const MyString& x_)
{
output << x_.str;
return output;
}
MyString operator + (const MyString& x_, const MyString& y_)
{
int i;
MyString res;
int lena = strlen(x_.str), lenb = strlen(y_.str);
if(res.str) delete res.str;
res.str = new char [lena + lenb + 1];
for(i = 0; i != lena; ++i)
{
res.str[i] = x_.str[i];
}
for(i = 0; i != lenb; ++i)
{
res.str[lena + i] = y_.str[i];
}
res.str[lena + lenb] = 0;
return res;
}
char& MyString::operator [] (int x_)
{
return str[x_];
}
MyString MyString::operator += (const char* x_)
{
int i;
int lena = strlen(str), lenb = strlen(x_);
char *tmp = new char [lena + 1];
for(i = 0; i != lena; ++i)
{
tmp[i] = str[i];
}
if(str) delete str;
str = new char [lena + lenb + 1];
for(i = 0; i != lena; ++i)
{
str[i] = tmp[i];
}
for(i = 0; i != lenb; ++i)
{
str[lena + i] = x_[i];
}
str[lena + lenb] = 0;
return *this;
}
char* MyString::operator () (int x_, int y_)
{
int i;
char* tmp = new char [y_];
for(i = x_; i != x_ + y_; ++i)
{
tmp[i - x_] = str[i];
}
tmp[y_] = 0;
return tmp;
}
bool operator > (MyString& x_, MyString& y_)
{
int i;
int lena = x_.getlen(), lenb = y_.getlen();
int len;
if(lena < lenb) len = lena;
else len = lenb;
for(i = 0; i != len; ++i)
{
if(x_.str[i] > y_.str[i])
{
return true;
}
if(x_.str[i] < y_.str[i])
{
return false;
}
}
if(lena < lenb)
{
return true;
}
return false;
}
bool operator < (MyString& x_, MyString& y_)
{
if((x_ > y_) || (x_ == y_))
{
return false;
}
return true;
}
bool operator == (MyString& x_, MyString& y_)
{
int i;
int lena = x_.getlen(), lenb = y_.getlen();
if(lena != lenb)
{
return false;
}
for(i = 0; i != lena; ++i)
{
if(x_.str[i] != y_.str[i])
{
return false;
}
}
return true;
}
이것은 두 번째 문제입니다.string류를 계승했습니다.
#include <string.h>
#include <stdlib.h>
#include <string>
#include <iostream>
using namespace std;
class MyString : public string
{
public:
MyString() : string() { };
MyString(const char* str) : string(str) { };
MyString(string str) : string(str) { };
MyString operator () (int x_, int y_)
{
return substr(x_, y_);
}
};
int CompareString( const void * e1, const void * e2)
{
MyString * s1 = (MyString * ) e1;
MyString * s2 = (MyString * ) e2;
if( * s1 < *s2 ) return -1;
else if( *s1 == *s2) return 0;
else if( *s1 > *s2 ) return 1;
}
int main()
{
MyString s1("abcd-"),s2,s3("efgh-"),s4(s1);
MyString SArray[4] = {"big","me","about","take"};
cout << "1. " << s1 << s2 << s3<< s4<< endl;
s4 = s3;
s3 = s1 + s3;
cout << "2. " << s1 << endl;
cout << "3. " << s2 << endl;
cout << "4. " << s3 << endl;
cout << "5. " << s4 << endl;
cout << "6. " << s1[2] << endl;
s2 = s1;
s1 = "ijkl-";
s1[2] = 'A' ;
cout << "7. " << s2 << endl;
cout << "8. " << s1 << endl;
s1 += "mnop";
cout << "9. " << s1 << endl;
s4 = "qrst-" + s2;
cout << "10. " << s4 << endl;
s1 = s2 + s4 + " uvw " + "xyz";
cout << "11. " << s1 << endl;
qsort(SArray,4,sizeof(MyString),CompareString);
for( int i = 0;i < 4;i ++ )
cout << SArray[i] << endl;
// s1 0 4
cout << s1(0,4) << endl;
// s1 5 10
cout << s1(5,10) << endl;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Access Request, Session and Application in Struts2If we want to use request, Session and application in JSP, what should we do? We can obtain Map type objects such as Req...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.