(C++)(OOP): Xây dựng class CHAR và class STRING
Giờ mới làm hoàn chỉnh bài này =)) Lưu lại thôi
| Đề bài:
Xây dựng class CHAR (kí tự) và STRING với việc: STRING được xây dựng với ý tưởng: 1 STRING là một chuỗi của các CHAR. Test với hàm main như sau:
int main()
{
CHAR c1, c2('c');
STRING s1, s2("s2"), s3('a'), s4(c1), s5(s4);
s1.expand(c2).expand('a').expand(s2).expand("abc");//s1: "cas2abc"
s1.remove(c2).remove('d');//remove all character c2 in s1 -- s1: "as2ab"
s1.input();//nhập chuỗi mới từ bàn phím
cout << s1.getContent();
cout << c1.getContent();
return 0;
}
Mẫu class STRING và class CHAR cho sẵn như sau:
class CHAR
{
private:
char content;
};
class STRING
{
private:
CHAR *content;
int length;
};
| Source Code
main.cpp
#include "CHAR.h"
#include "STRING.h"
int main()
{
CHAR c1, c2('c');
STRING s1, s2("s2"), s3('a'), s4(c1), s5(s4);
s1.expand(c2).expand('a').expand(s2).expand("abc");//s1: "cas2abc"
s1.remove(c2).remove('d');//remove all character c2 in s1 -- s1: "as2ab"
s1.input();//nhập chuỗi mới từ bàn phím
cout << s1.getContent();
cout << c1.getContent();
return 0;
}
CHAR.h
#ifndef char_h
#define char_h
#include<iostream>
using namespace std;
#include<string>
class CHAR
{
private:
char content;
public:
CHAR();
CHAR(char);
char getContent() const;
~CHAR();
};
#endif
CHAR.cpp
#include "CHAR.h"
CHAR::CHAR()
{
this->content = '\0';
}
CHAR::CHAR(char a)
{
this->content = a;
}
char CHAR::getContent() const
{
return this->content;
}
CHAR::~CHAR()
{
this->content = '\0';
}
STRING.h
#ifndef string_h
#define string_h
#include "CHAR.h"
#include<iostream>
#include<string>
using namespace std;
class STRING
{
private:
CHAR *content;
int length;
public:
STRING();
STRING(const char*);
STRING(char); // truyền kiểu ngầm định
STRING(const CHAR&);
STRING(const STRING&);
STRING& expand(const CHAR&); //truyền kiểu ngầm định
STRING& expand(char); //truyền kiểu ngầm định
STRING& expand(const STRING&);
STRING& expand(const char*); //truyền kiểu ngầm định
STRING& remove(const CHAR&);
STRING& remove(char);
void input();
char* getContent()const;
~STRING();
};
#endif
STRING.cpp
#include "STRING.h"
#include "STRING.h"
#include <vector>
//STRING::STRING() :length(0), content(NULL){}
STRING::STRING()
{
this->length = 0;
this->content = NULL;
}
STRING::STRING(const char* a)
{
if (strlen(a) > 0) // a!=NULL
{
this->length = (int)strlen(a);
this->content = new CHAR[this->length];
for (int i = 0; i < this->length; i++)
{
this->content[i] = CHAR(a[i]);
}
}
else
{
this->length = 0;
this->content = NULL;
}
}
STRING::STRING(char a)
{
this->length = 1;
this->content = new CHAR[1];
this->content[0] = CHAR(a);
}
STRING::STRING(const CHAR& a)
{
this->length = 1;
this->content = new CHAR[1];
this->content[0] = a;
}
STRING::STRING(const STRING& a)
{
this->length = a.length;
if (length > 0)
{
this->content = new CHAR[this->length];
for (int i = 0; i < this->length; i++)
{
this->content[i] = a.content[i];
}
}
else
{
this->content = NULL;
this->length = 0;
}
}
STRING& STRING::expand(const CHAR& c)
{
this->length++;
CHAR* newContent = new CHAR[this->length];
if (this->content != NULL)
{
for (int i = 0; i < this->length - 1; i++)
{
newContent[i] = this->content[i];
}
delete [] this->content;
}
newContent[this->length - 1] = c;
this->content = newContent;
return *this;
}
STRING& STRING::expand(char c)
{
this->length++;
CHAR* newContent = new CHAR[this->length];
if (this->content != NULL)
{
for (int i = 0; i < this->length - 1; i++)
{
newContent[i] = this->content[i];
}
delete[] this->content;
}
newContent[this->length - 1] = CHAR(c);
this->content = newContent;
return *this;
}
STRING& STRING::expand(const STRING& s)
{
int newlength = this->length + s.length + 1;
CHAR* newContent = new CHAR[newlength];
for (int i = 0; i < newlength; i++)
{
if (i < this->length)
{
newContent[i] = this->content[i].getContent();
}
else
{
newContent[i] = s.content[i - this->length].getContent();
}
}
this->content = newContent;
this->length = newlength - 1;
return *this;
}
STRING& STRING::expand(const char* s)
{
int newlength = this->length + (int)strlen(s) + 1;
CHAR* newContent = new CHAR[newlength];
for (int i = 0; i < newlength; i++)
{
if (i < this->length)
{
newContent[i] = this->content[i].getContent();
}
else
{
newContent[i] = CHAR(s[i - this->length]);
}
}
this->content = newContent;
this->length = newlength - 1;
return *this;
}
STRING& STRING::remove(const CHAR& c)
{
vector<CHAR> str;
for (int i = 0; i < this->length; i++)
{
if (this->content[i].getContent() != c.getContent())
{
str.push_back(this->content[i].getContent());
}
}
this->length = str.size();
CHAR* rmContent = new CHAR[this->length];
for (int i = 0; i < this->length; i++)
{
rmContent[i] = str[i].getContent();
}
this->content = rmContent;
return *this;
}
STRING& STRING::remove(char c)
{
vector<CHAR> str;
for (int i = 0; i < this->length; i++)
{
if (this->content[i].getContent() != c)
{
str.push_back(this->content[i].getContent());
}
}
this->length = str.size();
CHAR* rmContent = new CHAR[this->length];
for (int i = 0; i < this->length; i++)
{
rmContent[i] = str[i].getContent();
}
this->content = rmContent;
return *this;
}
void STRING::input()
{
string str;
getline(cin, str);
this->length = str.length();
this->content = new CHAR[this->length];
for (int i = 0;
i < this->length; i++)
{
this->content[i]
= CHAR(str[i]);
}
}
char* STRING::getContent()const
{
char* result = new char[this->length + 1];
for (int i = 0; i < this->length; i++)
{
result[i] = this->content[i].getContent();
}
result[this->length] = '\0';
return result;
}
STRING::~STRING()
{
this->length = 0;
delete[] this->content;
this->content = NULL;
}
Nhận xét
Đăng nhận xét