字符串

string.h

#ifdef STRING_H
#define STRING_H

#include<iostream>
using namespace std;

class String{
public:
	String();
	String(int n,char c);
	String(const char *source);
	String(const String& s);
	
	String& operator=(char* s);
	~String();
	
	char & operator[](int i){return a[i];}
	const char& operator[](int i)const {return a[i];}
	String & operator +=(const String&s);
	
	friend istream& operator >>(istream &is,String &s);
	
	friend bool operator <(const String& left,const String& right);
	friend bool operator >(const String& left,const String& right);
	
	friend bool operator==(const String& left,const String& right);
	friend bool operator !=(const String& left,const String& right);
	
	int length();
private:
	char *a;
	int size;
};
	

#endif



string.cpp

#include "String.h"
#include <cstring>
#include <cstdlib>

String::String(){
	a = new char[1];
	a[0] = '\0';
	size = 0;
}

String::String(int n,char c){
	a = new char[n+1];
	memset(a,c,n);
	a[n] = '\0';
	size = n;
}

String::String(const char*source){
	if(source == NULL){
		a = new char[1];
		a[0] = '\0';
		size = 0; 
	}else{
		size = strlen(source);
		a = new char[size+1];
		a[size] = '\0';
	}
}

//赋值构造函数中可以访问类的私有变量?
String::String(const String& s){
	size = strlen(s.a);
	a = new char[size+1];
	strcpy(a,s.a);
}

//
String &String::operator= (const String&s)
{
	if(this == &s)
		return *this;
	else{
		delete[] a;
		size = strlen(s.a);
		a = new char[size+1];
		strcpy(a,s.a);
		return *this;
	}
}

//
String::~String(){
	delete[] a;
}

String& String::operator+= (const String &s){
	int j = strlen(a);
	int size = strlen(s.a)+j;
	char *tmp = new char[size+1];
	strcpy(tmp,a);
	strcpy(tmp+j,s.a);
	delete[] a;
	a = tmp;
	
	return *this;
}

//
int String::length(){
	return strlen(a);
}


.main

#include<iostream>
#include "String.h"

using namespace std;

bool operator == (const String &left,const String& right)
{
	int a = strcmp(left.a,right.a);
	if(a == 0)
		return true;
	else{
		return false;
	}
}

//
bool operator != (const String& left,const String &right)
{
	return !(left == right);
}

ostream& operator << (ostream& os,String& s)
{
	int length = s.length();
	for(int i = 0;i<length;i++)
		os << s[i];
	return os;
}

String operator+(const String &a,const String &b){
	String tmp;
	tmp = a;
	tmp += b;
	return tmp;
}

bool operator <(const String& left,const String& right)
{
	int j = 0;
	while((left[j]!=0)&&(right[j]!=0)){
		if(left[j]<right[j])
			return true;
		else{
			if(left[j] == right[j]){
				j++
				continue;
			}else{
				return false;
			}
		}
	}
	if(left[j] == '\0'&&right[j]!='\0')
		return true;
	else
		return false;
		
}


bool operator >(const String& left ,const String& right){
	int a = strcmp(left.a,right.a);
	if(a >0)
		return ture;
	else
		return flase;
}

istream& operator>>(isteam& is,String& s){
	delete[] s.a;
	s.a = new char[20];
	int m =20;
	char c;
	int i = 0;
	while(is.get(c)&&isspace(c));
	is(is){
		do{
			s.a[i] = c;
			i++;
		}
		if(i == m-1){
			s.a[i] == '\0';
			char* b = new char[m];
			strcpy(b,s.a);
			m = m*2;
			s.a = new char[m];
			strcpy(s.a,b);
			delete[] b;
			}
		}
		
		while(is.get(c)&&!isspace(c));
		if(is)
			is.unget();
	}
	s.size = i;
	s.a[i]='\0';
	return is;
}
Screen & display(std::ostream &os){return *this;}
const Screen& display const (std::ostream&os){return *this;}

int main(){
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值