📄 h7.cpp
字号:
#include"h7.h"
#include<string.h>
using namespace std;
//构造函数String::String()
{
s=NULL;
}
String::String(const char* sx)
{
s=new char[strlen(sx)+1];
strcpy(s,sx);
}
String::String(char sx)
{
s[0]=sx;
}
String::String(int sx)
{
s = new char[10]; sprintf(s,"%s%d",sx);
}
String::String(const String& temp)
{
s=new char[strlen(temp.s)+1];
strcpy(s,temp.s);
}
String::String(double sx)
{
s = new char[10]; sprintf(s,"%s%lf",sx);
}
String::~String()
{
delete [] s;
}
//public方法
String String::operator+(const String& temp)
{
String str;
str.s=new char[strlen(s)+strlen(temp.s)+1];
strcpy(str.s,s);
strcat(str.s,temp.s);
return str;
}
String String::operator-(const String& temp)
{
String str; char* p,*q=s,*r=temp.s; int i=0,j=0,firstlong=strlen(s),secondlong=strlen(temp.s); if(strlen(s)>strlen(temp.s)) { str.s=new char[strlen(s)+1]; p=str.s; } else { str.s=new char[strlen(temp.s)+1]; p=str.s; } while(i<firstlong&&j<secondlong) { if(*q<*r) { *p=*q; q++; r++; p++; i++; j++; } else { *p=*r; r++; q++; p++; i++; j++; } } while(i<firstlong) { strcat(p,q); break; } while(j<secondlong) { strcat(p,r); break; }
return str;
}
String& String::operator=(const String& temp)
{
if(this==&temp)
return *this;
if(s)
delete [] s;
s=new char[strlen(temp.s)+1];
strcpy(s,temp.s); return *this;
}
int String::Strcmph(String& temp)
{
return strcmp(s,temp.s);
}
int String::Strlenh()
{
return strlen(s);
}
String String::Strcath(String& temp)
{
String str; str.s=new char[strlen(s)+strlen(temp.s)+1]; strcpy(str.s,s); strcat(str.s,temp.s);
}
void String::deleteSpace()
{ for(int i=0;i<strlen(s);++i) { char* p=s; if(*s==' ')
{
while(*p)
{
*p=*(p+1);
p++;
}
}
else
return; }
}
void String::BigWrite()
{
char* p=s;
while(*p>='a'&&*p<'z')
{
*p-=32;
p++;
}
}
void String::LittleWrite()
{
char* p=s;
while(*p>='A'&&*p<'Z')
{
*p+=32;
p++;
}
}
String::operator double()
{
return double(*s);
}
String::operator int()
{
return int(*s);
}
ostream& operator<<(ostream& out,String& temp)
{
out<<"The Strings are "<<"'"<<temp.s<<"' ";
return out;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -