Description
封装一个字符串类,用于存储字符串和处理的相关功能,支持以下操作:
1. STR::STR()构造方法:创建一个空的字符串对象。
2. STR::STR(const char *)构造方法:创建一个字符串对象,串的内容由参数给出。
3. STR::length()方法:返回字符串的长度。
4. STR::putline()方法:输出串的内容,并换行。
5. 运算符“+”和“+=”,表示两个字符串的连接运算,规则为:
c = a + b 表示串c中的字符是a和b的连接:“a+b”的结果是一个新的字符串,串a和串b的内容不变。
a += b 表示串a中的字符是a和b的连接:串b中的内容不变
-----------------------------------------------------------------------------
你设计一个字符串类STR,使得main()函数能够正确运行。
函数调用格式见append.cc。
append.cc中已给出main()函数。
-----------------------------------------------------------------------------
Invalid Word(禁用单词)错误:“string”、“vector”等被禁用。
Input
输入有若干行,每行一个字符串。
Output
每组测试数据对应输出一行,包含两部分内容,首先是一个整数,表示输入串的长度,然后是输入的字符串,两者用一个空格分开。格式见sample。
Sample Input
Sample Output
#include<bits/stdc++.h>
using namespace std;
void f(char *s1,char *s2,int length)//令s1=s2,length为s2的长度
{
int i;
for( i=0;i<length;i++)
{
s1[i]=s2[i];
}
s1[i]=0;
}
class STR
{
public:
STR()
{
l=0;
}
STR(char *p)
{
int i;
for(i=0;p[i]!=0;i++)
{
}l=i;
s=new char[l+1];
f(s,p,l);
}
~STR()
{
delete []s;
}
int length()
{
return l;
}
void putline()
{
cout<<s;
cout<<endl;
}
STR &operator+=(STR &p)//加&是因为没有写构造函数,成员有指针。
{
//思路是先构造一个q存左操作数,左操作数new完后,再将q和p复制过去。
int j=l;
STR q(s);
l=l+p.l;
delete []s;
s=new char[l+1];
f(s,q.s,q.l);
for(int i=0;i<p.l;i++)
{
s[j++]=p.s[i];
}
s[l]=0;
return *this;
}
friend STR operator+(STR &e,STR &h);
private:
int l;
char *s;
};
STR operator+(STR &e,STR &h)
{
STR temp;
int j=e.l;
temp.l=e.l+h.l;
temp.s=new char[temp.l+1];
for(int i=0;i<e.l;i++)
{
temp.s[i]=e.s[i];
}
for(int i=0;h.s[i]!=0;i++)
{
temp.s[j++]=h.s[i];
}
temp.s[temp.l]=0;
return temp;
}
int main()
{
STR e;
STR h("Hello World!");
STR he = e + h;
cout << he.length() << " ";
he.putline();
cout << e.length() << " ";
e.putline();
cout << h.length() << " ";
h.putline();
e += h;
cout << e.length() << " ";
e.putline();
cout << h.length() << " ";
h.putline();
char s1[100001], s2[100001];
while(gets(s1) != NULL && gets(s2) != NULL)
{
STR str1(s1), str2(s2);
STR str = str1 + str2;
cout << str.length() << " ";
str.putline();
cout << str1.length() << " ";
str1.putline();
cout << str2.length() << " ";
str2.putline();
str2 += str1;
cout << str2.length() << " ";
str2.putline();
cout << str1.length() << " ";
str1.putline();
}
}
840



被折叠的 条评论
为什么被折叠?



