[数据结构]MyString

//MyString.h
#include"List.cpp"
class MyString
{
public:
	MyString();
	~MyString();
	MyString(const MyString &copy);
	MyString(const char *copy);
	MyString(List<char>&copy);
	void operator=(const MyString &copy);
	const char *c_str() const;
protected:
	char *entries;
	int length;
};
bool operator==(const MyString &first, const MyString &second);
bool operator>=(const MyString &first, const MyString &second);
bool operator<=(const MyString &first, const MyString &second);
bool operator>(const MyString &first, const MyString &second);
bool operator<(const MyString &first, const MyString &second);
bool operator!=(const MyString &first, const MyString &second);
void strcat(MyString &add_to, const MyString &add_on);
MyString read_in(istream &input, int &terminator);
void strcpy(MyString &copy, const MyString &original);
void strncpy(MyString &copy, const MyString &original, int n);
int strstr(const MyString &text, const MyString &target);
MyString operator+(MyString s1, MyString s2);
ostream &operator<<(ostream & output, MyString s1);
void write(MyString &s);

//MyString.cpp
#define _CRT_SECURE_NO_WARNINGS
#include"MyString.h"
#include<iostream>
using namespace std;

MyString::MyString()
{
	entries = new char[1];
	entries[0] = '\0';
	length = 0;
}

MyString::~MyString()
{
	delete[]entries;
}

MyString::MyString(const MyString & copy)
{
	length = copy.length;
	entries = new char[length + 1];
	for (int i = 0; i < length; i++) entries[i] = copy.entries[i];
	entries[length] = '\0';
}

MyString::MyString(const char * copy)
{
	length = strlen(copy);
	entries = new char[length + 1];
	strcpy(entries, copy);
}

MyString::MyString(List<char>& copy)
{
	length = copy.size();
	entries = new char[length + 1];
	for (int i = 0; i < length; i++)copy.retrieve(i, entries[i]);
	entries[length] = '\0';
}

void MyString::operator=(const MyString & copy)
{
	if (entries == copy.entries)return;
	delete[] entries;
	length = copy.length;
	entries = new char[length + 1];
	for (int i = 0; i < length; i++)
		entries[i] = copy.entries[i];
	entries[length] = '\0';

}

const char * MyString::c_str() const
{
	return (const char *)entries;
}

bool operator==(const MyString & first, const MyString & second)
{
	return (strcmp(first.c_str(),second.c_str())==0);
}

bool operator>=(const MyString & first, const MyString & second)
{
	return (strcmp(first.c_str(), second.c_str()) >= 0);
}

bool operator<=(const MyString & first, const MyString & second)
{
	return (strcmp(first.c_str(), second.c_str()) <= 0);
}

bool operator>(const MyString & first, const MyString & second)
{
	return (strcmp(first.c_str(), second.c_str()) > 0);
}

bool operator<(const MyString & first, const MyString & second)
{
	return (strcmp(first.c_str(), second.c_str()) < 0);
}

bool operator!=(const MyString & first, const MyString & second)
{
	return (strcmp(first.c_str(), second.c_str()) != 0);
}

void strcat(MyString & add_to, const MyString & add_on)
{
	const char *cfirst = add_to.c_str();
	const char *csecond = add_on.c_str();
	char *copy = new char[strlen(cfirst) + strlen(csecond) + 1];
	strcpy(copy, cfirst);
	strcpy(copy, csecond);
	add_to = copy;
	delete []copy;
}

MyString read_in(istream & input, int & terminator)
{
	List<char>temp;
	terminator = 0;
	char c;
	while ((c = input.peek()) != EOF && (c = input.get()) != '\n')
		temp.insert(terminator++, c);
	MyString answer(temp);
	return answer;
}

void strcpy(MyString & copy, const MyString & original)
{
	if (copy.c_str() == original.c_str())return;
	int length = strlen(original.c_str());
	char *temp = new char[length + 1];
	strcpy(temp, original.c_str());
	copy = temp;
	delete[] temp;
}

void strncpy(MyString & copy, const MyString & original, int n)
{
	char *temp = new char[n + 1];
	strncpy(temp, original.c_str(),n);
	temp[n] = '\0';
}

int strstr(const MyString & text, const MyString & target)
{
	const char *s=strstr(text.c_str(), target.c_str());
	if (s == NULL)return -1;
	else return(s - target.c_str());
}

MyString operator+(MyString s1, MyString s2)
{
	strcat(s1, s2);
	return s1;
}

ostream & operator<<(ostream & output, MyString s1)
{
	output << s1.c_str() << endl;
	return output;
}

void write(MyString & s)
{
	cout << s.c_str() << endl;
}

//main.cpp
#include<iostream>
#include"MyString.h"
using namespace std;
void main() {
	MyString s1 = "s1 MyString";
	cout << s1.c_str() << endl;
	cout << strlen(s1.c_str()) << endl;
	MyString s2("s2 MyString");
	cout << s2.c_str() << endl;
	cout << strlen(s2.c_str()) << endl;
	if (s1 >= s2) cout << "s1>=s2" << endl;
	else cout << "s1<s2" << endl;
	s2 = s1;
	if (s1 == s2) cout << "s1=s2" << endl;
	else cout << "s1!=s2" << endl;
	int t;
	MyString s3 = read_in(cin,t);
	strcat(s3, s1);
	write(s3);

}

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值