C++重写String类(部分)

 课程设计的作业,不过后面因为一些事并没写完,而且有些要求太过模糊,老师也没解释个所以然...有空的时候整理吧,先丢代码了,抱歉。话说因为写的时候是个彻彻底底的弱鸡(虽然现在也还是..),所以肯定有一堆很奇怪的地方,希望大佬们不要太在意某些细节,若给出建议,万分感谢。emmmm仅供学习参考。

----------------------------------

头文件:string.h

#pragma once
#include<iostream>
using namespace std;

class String
{
public:
	 //指导书中要求的方法
	 String(const char* str = NULL);
	 String(const String & other);
	 String & operator = (const String & other);
	 String &operator+=(const String &s);
	 friend ostream & operator<<(ostream&out, String &obj);
	 friend istream & operator >> (istream&in, String&obj);
 	 String &append(const char *s);
	 int Compare(String strA, String strB);
	 int find(String s_1,String s_2,int i,int count);
	 int Compare(String strA, String strB, bool ignoreCase);
	 String &dapapt(String s);
	 int Indexof(String value);
	 int Indexof(String value, int startIndex);
	 int Indexof(String value, int startIndex, int count);
	 String replace(String oldValue, String newValue);
	 String trim(String value);
	 String trim(String value, String trimString);
	 String trimStart(String value, String trimString);
	 String trimEnd(String value, String trimString);
	 int getLength(String value);
	 int getLength(String value, String insertString);
	 int getLength(String value, String insertString, String trimString);
	 String &assign(const char*s);
	 String &assign(const char *s, int n);
	 String &assign(int n, char c);
	 ~String(void);

	 //自己添加的方法,用以完成某些功能。
	 String trimDelete(String value, String trimString);
	 String delete_1(char a);
	 String delete_2(char a);
	 int Compare_2(String strA, String strB);
	 int Compare_2(String strA, String strB, bool ignoreCase);

private:
	char* _str;
	char str[500];
};


-----------

源文件:string.cpp

#include<iostream>
using namespace std;
#include"string.h"


//默认构造函数
String::String(const char* str) {
	if (str == NULL) {
		_str = new char[1];
		*_str = '/0';
	}
	else {
		int len = strlen(str);
		_str = new char[len+1];
		strcpy_s(_str, len + 1, str);
	}
}

//析构函数
String::~String(void) {
	//delete _str;
	//delete str;
	_str = NULL;
}

//拷贝构造函数
String::String(const String & other) {
	int len = strlen(other._str);
	_str = new char[len + 1];
	strcpy_s(_str, len + 1, other._str);
}

//重载运算符 =
String & String::operator=(const String & other) {
	if (this == &other) {
		return *this;
	}
	else {
		delete[]_str;//删除原本的空间重新赋值
		int len = strlen(other._str);
		this->_str = new char[len + 1];
		strcpy_s(this->_str, len + 1, other._str);
		return *this;
	}
}

//重载运算符 += 将字符串s拼接到当前字符串尾
String & String::operator+=(const String &s) {
	int len_1 = strlen(this->_str);
	int len_2 = strlen(s._str);
	for (int i = 0;i < len_1;i++) {
		str[i] = this->_str[i];
	}
	for (int i = 0;i < len_2;i++) {
		str[len_1 + i] = s._str[i];
	}
	str[len_1 + len_2] = '\0';
	this->_str = str;
	return *this;
}

//重载运算符<<和>>
ostream & operator<<(ostream &out, String &obj) {
	out << obj._str;
	return out;
}
istream & operator >> (istream &in, String &obj) {
	in >> obj._str;
	return in;
}

//重载assign函数1.6  字符串s的赋值
String & String::assign(const char *s) {
	delete _str;
	int len = strlen(s);
	_str = new char[len + 1];
	strcpy_s(_str, len + 1, s);
	return *this;
}

//重载assign函数1.7  获得字符串中指定位置的字符
String & String::assign(const char *s, int n) {
	int len = strlen(s);
	_str = new char[len + 1];
	if (n > len) {
		cout << "输入错误";
		return *this = "错了";
	}
	else {
		this->str[0] = s[n - 1];
		this->_str = str;
		return *this;
	}
}

//重载assign函数1.8  输出n个字符c
String & String::assign(int n, char c) {
	delete[]_str;
	for (int i = 0;i < n;i++) {
		str[i] = c;
	}
	_str = str;
	return *this;
}

//拼接方法
String &String::append(const char *s) {
	int len_1 = strlen(this->_str);
	int len_2 = strlen(s);
	for (int i = 0;i < len_1;i++) {
		str[i] = _str[i];
	}
	for (int i = 0;i < len_2;i++) {
		str[len_1 + i] = s[i];
	}
	_str = str;
	return *this;
}

//查找字符串  i 起始位置 count 查找数量
//count 为0则为全部查找
 int String::find(String s_1, String s_2,int i,int count){
	int len_1 = strlen(s_1._str);
	int len_2 = strlen(s_2._str);
	int n = -1;//用于返回-1
	int m = 0;
	int o = 0;
		 if (i != 0) {
			 i -= 1;
		 }
	for (i; i < len_1;i++) {
		m = 0;
		if (s_1._str[i] == s_2._str[0]) {
			for (int p = 0;p < len_2;p++) {
				if (s_1._str[i + p] == s_2._str[p]) {
					m++;
				}
			}
		}
		if (m == len_2) {	
			n = i+1;
			return n;
		}
		o++;
		if (count > 0) {
			if (o > count)
				break;
		}
	}
	return n;
}

 //2.2.1  比较两个字符串在目标字符串中的位置关系
 int String::Compare(String strA, String strB) {
	 int a = find(this->_str, strA._str,0,0);
	 int b = find(this->_str, strB._str,0,0);
	 if (a == -1) {
		 cout << "strA字符串未被找到";
		 return 2;
	 }
	 if (b == -1) {
		 cout << "strB字符串未被找到";
		 return 2;
	 }
	 if (a < b)return -1;
	 if (a == b)return 0;
	 if (a > b)return 1;
}

 //将大写字母切换为小写字母
 String & String::dapapt(String s) {
	 int len = strlen(s._str);
	 for (int i = 0;i < len;i++) {
		 if (s._str[i] >= 'A'&& s._str[i] < 'Z'){
			 s._str[i] += 32;
		 }
	 }
	 return *this;
 }
 //仅比较两个指定字符的ascll码值的大小
 int String::Compare_2(String strA, String strB) {
	 int len_1 = getLength(strA);
	 int len_2 = getLength(strB);
	 int a = 0;
	 int A = 0;
	 int B = 0;
	 for (int i = 0;i < len_1;i++) {
		 if (strA._str[i] == strB._str[i]) {
			 continue;
		 }
		 else {
			 A = (int)strA._str[i];
			 B = (int)strB._str[i];
			 break;
		 }
	 }
	 if (A < B)return -1;
	 if (A == B)return 0;
	 if (A > B)return 1;
 }

 int String::Compare_2(String strA, String strB, bool ignoreCase) {
	 if (ignoreCase == false) {
		 return Compare_2(strA, strB);
	 }
	 else if (ignoreCase == true) {
		 return Compare_2(dapapt(strA._str), dapapt(strB._str));
	 }
 }


 //ignoreCase 为true时,比较字符串时将忽略大小写.false则正常比较。
 int String::Compare(String strA, String strB, bool ignoreCase) {
	 if (ignoreCase == false) {
		 int a = find(this->_str, strA._str,0,0);
		 int b = find(this->_str, strB._str,0,0);
		 if (a == 0) {
			 cout << "strA字符串未被找到";
			 return 0;
		 }
		 if (b == -1) {
			 cout << "strB字符串未被找到";
			 return 0;
		 }
		 if (a < b)return -1;
		 if (a == b)return 0;
		 if (a > b)return 1;
	 }
	 if (ignoreCase == true) {
		 int a = find(dapapt(this->_str), dapapt(strA._str),0,0);
		 int b = find(dapapt(this->_str), dapapt(strB._str),0,0);
		 if (a == 0) {
			 cout << "strA字符串未被找到";
			 return 0;
		 }
		 if (b == -1) {
			 cout << "strB字符串未被找到";
			 return 0;
		 }
		 if (a < b)return -1;
		 if (a == b)return 0;
		 if (a > b)return 1;
	 }
 }

 //查找目标字符串中的第一个对应字符
 int String::Indexof(String value) {
	 return find(this->_str, value,0,0);
 }

 //从指定位置开始查找
 int String::Indexof(String value, int startIndex){
	 return find(this->_str, value, startIndex,0);
 }

 //从指定位置开始查找并只查找count个字符
 int String::Indexof(String value, int startIndex, int count) {
	 return find(this->_str, value, startIndex, count);
 }

 //将目标字符串中的指定字符串替换为另一字符串
 String String::replace(String oldValue, String newValue) {
	 int len_1 = strlen(oldValue._str);
	 int len_2 = strlen(newValue._str);
	 while (true)
	 {
		 int k = find(this->_str, oldValue, 0, 0);
		 if (k == -1) {
			 cout << "未找到oldValue或替换完成" << endl;
			 return *this;
		 }
		 for (int i = 0;i < len_1;i++) {
			 this->_str[k - 1 + i] = newValue._str[i];
		 }
	 }
	 return *this;

 }

 //删去字符串中所有指定字符
 String String::delete_1(char a) {
	 int b = 0;
	 int c = 0;
	 while (this->_str[b]!='\0')
	 {
		 if (a !=this->_str[b]) {
			 this->_str[c] = this->_str[b];
			 c++;
			 b++;
		 }
		 else {
			 b++;
		 }
	 }
	 this->_str[c] = '\0';
	 return *this;
 }

 //删去字符串首尾的指定字符
 String String::delete_2(char a) {
	 int len = strlen(this->_str);
	 int c = 0;
	 int b = 0;
	 int d = 0;
	 for (int i = len - 1;i > 0;i--) {
		 if (this->_str[i] == a) {
			 b++;
		 }
		 else {
			 break;
		 }
	 }
	 for (int i = 0;i < len;i++) {
		 if (this->_str[i] == a) {
			 c++;
		 }
		 else
		 {
			 break;
		 }
	 }
	 for (int i = c;i < len - b;i++) {
		 this->str[d] = this->_str[i];
		 d++;
	 }
	 this->str[d] = '\0';
	 this->_str = str;
	 return *this;
 }

 //删去字符串收尾的所有空白字符。
 String String::trim(String value) {
	 char a = (char)32;
	 return value.delete_1(a);
 }

 //删去指定字符
 String String::trim(String value, String trimString){
	 char a = (char)trimString._str[0];
	 return value.delete_2(a);
 }

 //删去字符串中指定字符串
 String String::trimDelete(String value, String trimString)
 {
	 int count = 0;
	 int len_1 = strlen(value._str);
	 int len_2 = strlen(trimString._str);
	 int head = find(value,trimString,0,0);
	 int end = head + len_2 - 1;
	 for (int i = 0;i < len_1 ;i++) {
		 if (i >= head - 1 && i <= end - 1) {
			 continue;
		 }
		 else {
			 this->str[count]=value._str[i];
			 count++;
		 }
	 }
	 if(count!=0){
		 this->str[count] = '\0';
		 this->_str = this->str;
	 }
	 return *this;
 }


 String String::trimStart(String value, String trimString){
	 String temp = trimDelete(value, trimString);
	 return temp;
 }

 String String::trimEnd(String value, String trimString){
	 String temp = trimDelete(value, trimString);
	 return temp;
 }

 //获得字符长度
 int String::getLength(String value){
	 int a = 0;
	 while (value._str[a] != '\0')
	 {
		 a++;
	 }
	 return a;
 }
 //接上新字符后字符长度
 int String::getLength(String value, String insertString){
	 value += insertString;
	 return value.getLength(value);
 }

 //接上新字符后再删除指定字符串后的长度
 int String::getLength(String value, String insertString, String trimString){
	 value += insertString;
	 String temp;
	 return getLength(temp.trimDelete(value, trimString));
 }


--------------

test:

#include"string.h"


void main() {
	
	//3.1.1  比较两个指定字符串
	//返回值小于0时,strA在排序顺序中位于strB之前。当返回值等于0时,strA在排序顺序中等于strB。当返回值大于0时,strA在排序顺序中位于strB之后
	String a_1 = "Hello World";
	String b_1 = "HELLO WORLD";
	String test;
	int ia_1 = test.Compare_2(a_1, b_1);
	cout << ia_1 << endl;							//运行结果:1.strA在ascll码表中排序顺序中位于strB之后
	//3.1.2  当bool为true时忽视大小写进行比较
	int ib_1 = test.Compare_2(a_1, b_1, false);
	cout << ib_1 << endl;							//运行结果:1.strA在ascll码表中排序顺序中位于strB之后
	int ic_1 = test.Compare_2(a_1, b_1, true);
	cout << ic_1 << endl << "---------------------------" << endl;		//运行结果:0 忽略大小写后排序相同
	
	//3.1.1  比较指定字符串在目标字符串中的位置关系
	//返回值小于0时,strA在排序顺序中位于strB之前。当返回值等于0时,strA在排序顺序中等于strB。当返回值大于0时,strA在排序顺序中位于strB之后
	String a_2 = "Hello World.C++.HELLO WORLD,Java";
	String b_2 = "C++";
	String c_2 = "Java";
	int ia_2 = a_2.Compare(a_1, b_1);
	cout << ia_2 << endl;							//运行结果:-1.strA在目标字符串中位于strB之前
	int ib_2 = a_2.Compare(c_2, b_2);
	cout << ib_2 << endl;							//运行结果:1.strA在目标字符串中位于strB之后
	//3.1.2  当bool为true时忽视大小写进行比较
	int ic_2 = a_2.Compare(a_1, b_1, true);	
	cout << ic_2 << endl << "---------------------------" << endl;			//运行结果:0.忽略大小写后,排序相同。

	//获得目标字符串长度。
	String a_3 = "China stocks extend rally on positive policy outlook";
	int len_3 = a_3.getLength(a_3);
	cout << len_3 << endl << "---------------------------" << endl;							//运行结果:52
	
	//接上指定字符串后获得字符串长度
	String a_4 = "Chinese shares remained in positive territory for the second-consecutive day on Wednesday following the government's fresh fiscal stimulus measures";
	int len_4 = test.getLength(a_3, a_4);
	cout << len_4 << endl << "---------------------------" << endl;							//运行结果:199

	//去掉目标字符串中指定字符
	char a_5 = 'o';
	String b_5 = (a_3 += a_4);

	String c_5 = b_5.delete_1(a_5);
	int len_5 = test.getLength(c_5);
	cout << len_5 << endl << "---------------------------" << endl;

	//索引目标字符串,返回其所处位置,若未找到则返回-1
	String a_6 = "China";
	String b_6 = "Chinese";
	int ia_6 = c_5.Indexof(a_6);
	cout << "China在上诉字符串中的位置为:" << ia_6 << endl;
	int ib_6 = c_5.Indexof(b_6);
	cout << "Chinese在上诉字符串中的位置为:" << ib_6 << endl;
	int ic_6 = c_5.Indexof(a_6, 40);
	cout << "China在上诉字符串中索引从40开始的结果为:" << ic_6 << endl;
	int id_6 = c_5.Indexof(b_6, 40);
	cout << "Chinese在上诉字符串中索引从40开始的结果为:" << id_6 << endl;
	String c_6 = "day";
	int ie_6 = c_5.Indexof(c_6, 35, 60);
	int if_6 = c_5.find(c_5, c_6,0,0);
	cout << "day在上诉字符串中的位置为:" << if_6 << endl;
	cout << "day在上诉字符串中索引从35到60的结果:" << ie_6 << endl << "---------------------------" << endl;
	
	//替换指定字符串
	b_5 = (a_3 += a_4);
	cout << "替换前:" << b_5 << endl;
	String a_7 = "positive";
	String b_7 = "negative";
	String c_7 = b_5.replace(a_7, b_7);
	cout << "替换后:" << c_7 << endl << "---------------------------" << endl;

	//去掉多于的字符
	String a_8 = "  CCCentral bank defends measures taken to regulate forex markettt  ";
	cout << "去除首尾空格前:" << a_8 << endl;
	String b_8 = test.trim(a_8);
	cout << "去除首尾空格后:" << b_8 << endl << "---------------------------" << endl;
	String c_8 = "CC";
	String d_8 = "tt";
	String e_8 = b_8.trimStart(b_8, c_8);
	e_8 = b_8.trimEnd(b_8, d_8);
	cout << "去除多于字符串后:" << e_8 << endl << "其实都是用的一个方法,删除字符串中的指定字符串." << endl << "---------------------------" << endl;
	cout << "谢谢检查" << endl;
	system("pause");
}

-----------------------------------------------------------------------------

结果:



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值