收藏 分销(赏)

运算符重载string运算符.docx

上传人:pc****0 文档编号:6635330 上传时间:2024-12-18 格式:DOCX 页数:6 大小:18.95KB 下载积分:10 金币
下载 相关 举报
运算符重载string运算符.docx_第1页
第1页 / 共6页
运算符重载string运算符.docx_第2页
第2页 / 共6页


点击查看更多>>
资源描述
运算符重载 #include<iostream> #include"FunnyString.h" using std::istream; using std::ostream; using std::string; using std::cout; using std::cin; using std::endl; int main() { FunnyString string1,string2; cout<<"Please input s1: "; cin>>string1; cout<<"Please input s2: "; cin>>string2; cout<<"未进行s1-=s2运算时的s1 * s2 is "<<string1*string2; cout<<"\ns1 + s2 is "<<string1+string2; cout<<"\ns1 - s2 is "<<string1-string2; cout<<"\ns1+= s2 is "<<(string1+=string2); cout<<"\ns1 -= s2 is "<<(string1-=string2);//老师,题目中写道:s1+= s2 is abcabf s1 -= s2 is c s1 * s2 is adbacbdf cout<<"\n进行s1-=s2运算后的s1 * s2 is "<<string1*string2<<"\n"; //但是"-="后s1的只有一个c了,乘以s2后是不是应为cdabf。 return 0; } #include <iostream> using std::istream; using std::ostream; using std::string; using std::cout; using std::cin; using std::endl; #include"FunnyString.h" void FunnyString::setFunnyString( const char * a) { sPtr=new char [length+1]; if ( a!=0) strcpy(sPtr,a); else sPtr[0]='\0'; } FunnyString::FunnyString( const char *first) : length((first!=0)?strlen(first):0) { setFunnyString(first); } FunnyString::FunnyString( const FunnyString &a) { length=a.length; sPtr=new char[length+1]; for(int i=0;i<length;i++) sPtr[i]=a.sPtr[i]; } int FunnyString::getLength() const { return length; } FunnyString::~FunnyString() { delete []sPtr; } ostream &operator<<( ostream &output, const FunnyString &a ) { for (int i=0;i<a.length;i++) output<<a.sPtr[i]; return output; } istream &operator>>( istream &input, FunnyString &a) { char b[100]; input>>b; a.length=strlen(b); delete []a.sPtr; a.sPtr=new char [a.length+1]; for (int i=0;i<strlen(b);i++) { a.sPtr[i]=b[i]; } return input; } const FunnyString &FunnyString::operator= (const FunnyString &a) { if (&a != this) { delete []sPtr; length=a.length; setFunnyString(a.sPtr); return *this; } else return *this; } const FunnyString &FunnyString::operator+=( const FunnyString &a ) { FunnyString c; c=*this+a; *this=c; return *this; } const FunnyString &FunnyString::operator-=( const FunnyString &a ) { FunnyString c; c=*this-a; *this=c; return *this; } const FunnyString operator+(const FunnyString &left,const FunnyString &right) { int len=left.length+right.length; char *a=new char [len+1]; a[1]=left.sPtr[1]; for (int i=0;i<left.length;i++) { a[i]=left.sPtr[i]; } len=left.length; int j; for ( j=0;j<right.length;j++) { if(len!=0) { if (left.sPtr[len-1]==right.sPtr[j]) { len--; } else { j++; break; } } else { j++; break; } } j--; if (j<right.length-1) { for (;j<right.length;j++) { len++; a[len-1]=right.sPtr[j]; } } a[len]='\0'; FunnyString c(a); delete []a; return c; } const FunnyString operator-(const FunnyString &left,const FunnyString &right) { char *a=new char [left.length+1]; int len=left.length; for (int i=0;i<left.length;i++) { a[i]=left.sPtr[i]; } for (int j=0;j<right.length;j++) for (int p=0;p<left.length;p++) { if (a[p]==right.sPtr[j]) a[p]='0'; } for (int s=0;s<left.length;s++) { if (a[s]=='0') { int q=s; for(;q<left.length;q++) a[q]=a[q+1]; len--; s--; } } a[len]='\0'; FunnyString c(a); delete []a; return c; } const FunnyString operator*(const FunnyString &left,const FunnyString &right) { char *a=new char [left.length+right.length+1]; int min; if (left.length<right.length) min=left.length; else min=right.length; for (int i=0;i<min;i++) { a[2*i]=left.sPtr[i]; a[2*i+1]=right.sPtr[i]; } if (left.length<right.length) { for (int j=min;j<right.length;j++) { a[min+j]=right.sPtr[j]; } } else { for (int j=min;j<left.length;j++) { a[min+j]=left.sPtr[j]; } } a[left.length+right.length]='\0'; FunnyString c(a); delete []a; return c; } #ifndef FUNNYSTRING_H #define FUNNYSTRING_H #include <iostream> #include<iomanip> using std::istream; using std::ostream; using std::string; using std::cout; using std::cin; using std::endl; class FunnyString { friend ostream &operator<<( ostream &, const FunnyString &); friend istream &operator>>( istream &, FunnyString &); friend const FunnyString operator+(const FunnyString &,const FunnyString &); friend const FunnyString operator-(const FunnyString &,const FunnyString &); friend const FunnyString operator*(const FunnyString &,const FunnyString &); public: FunnyString( const char * ="" ); FunnyString( const FunnyString & ); ~FunnyString(); const FunnyString &operator+=( const FunnyString & ); const FunnyString &operator-=( const FunnyString & ); const FunnyString &operator=(const FunnyString &); int getLength() const; private: int length; char *sPtr; void setFunnyString( const char * ); }; #endif
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服