for(intk=9;k>0;k--)
cout<<(node[k].next)->data;
}
第8章类和对象
/*1、自定义一个字符串处理类CString,且能实现取子串、删除子串的功能。
*/
#include<>
#defineMAX_LENGTH100
classCString
{
public:
voidcpystring(char*str);
voidgetSubstring(intbeginStr,intendStr);
voiddelSubstring(intbeginStr,intendStr);
voidprint();
private:
charcstr[MAX_LENGTH];
intlength;
};
#include""
#include""
voidCString:
:
cpystring(char*str)
{
inti=0;
while(*str!
='\0')
{
cstr[i++]=*str;
str++;
}
cstr[i]='\0';
}
voidCString:
:
getSubstring(intbeginStr,intendStr)
{
inti,j=0;
charpstr[MAX_LENGTH];
if(beginStr<0||endStr>MAX_LENGTH||beginStr>endStr)
{
cout<<"error!
"<}
for(i=beginStr;i{
pstr[j]=cstr[i];
}
pstr[j]='\0';
cpystring(pstr);
}
voidCString:
:
delSubstring(intbeginStr,intendStr)
{
inti,j=0;
charpstr[MAX_LENGTH];
if(beginStr<0||endStr>MAX_LENGTH||beginStr>endStr)
{
cout<<"error!
"<}
for(i=0;i{
pstr[j]=cstr[i];
}
for(i=endStr+1;i{
pstr[j]=cstr[i];
}
pstr[j]='\0';
cpystring(pstr);
}
voidCString:
:
print()
{
cout<}
#include<>
#include""
main()
{
CStringstr1,str2,str3;
("Justlikebefore,it'syesterdayoncemore!
");isplay();
cout<<"v2=";();
v3=v1+v2;
cout<<"v1+v2=";();
}
/*2、将第一题中的相关重载项改为用友元函数的方式重载。
*/
classVector
{
public:
Vector();
Vector(intx,inty);
friendvoidoperator++(Vector&v);
friendvoidoperator++(Vector&v,int);
friendVectoroperator+(constVector&v1,constVector&v2);
voiddisplay();
private:
intx,y;
};
#include<>
#include""
Vector:
:
Vector(){}
Vector:
:
Vector(intx,inty)
{
this->x=x;
this->y=y;
}
voidoperator++(Vector&v){++;++;}
Vectoroperator++(Vector&v,int)
{
Vectorbefore,;
++;
++;
returnbefore;
}
Vectoroperator+(constVector&v1,constVector&v2)
{
Vectorsum;
=+;
=+;
returnsum;
}
voidVector:
:
display()
{
cout<<"("<}
#include<>
#include""
voidmain()
{
Vecto