C++ string類使用、定義

#define strmax 100

#include <iostream.h>

#include <cstring>


class string
{

char line[100];

int x,y;

public:

string(char *);

void print();

void turn();

int strlength();

void substring(unsigned,unsigned);

void insert(int,char *);

void index(char *);

void replace(char *,char *);

void strempty();
   
string operator = (string s);

string operator + (string s);

void operator <= (string s1);//判斷字符竄長度

void operator == (string s1);//字符竄相等

void operator != (string s1);//字符竄不相等

char operator [] (int pos);  

};

//===============================================================

string::string(char *n)

{

    strcpy(line,n);

}

//===============================================================

void string::print()

{

    cout<< line;

}

//===============================================================

void string::turn()

{

int i,len=strlen(line);

for(i=0;i<len;i++)
  
{
  
   if(line[i]>=97&&line[i]<=122)
   
   {

    line[i]-=32;

    cout<< line[i];

   }
  
   else
   
   {

    cout<< line[i];

   }
  
}

}

//===============================================================

int string::strlength()

{

return strlen(line);

}

//===============================================================

void string::substring(unsigned pos,unsigned len)

{
if(pos > strlen(line))
  
   cout<<"Error!";

else
  
{
  
   if((strlen(line)-pos) < len)
   
    len=strlen(line)-pos;
  
   cout<<"The substring is:";
  
   unsigned i=pos+len;
  
   for(pos;pos<i;pos++)
   
   {
   
    cout<< line[pos-1];
   
   }
  
}

}
//===============================================================

void string::insert(int pos,char *p)

{

char p1[64];

char p2[64];

strcpy(p1,p);

cout<<"The line which will be insert is:"<<p1;

int l=strlen (p1);

int m=0;

int c=0;

for(int b=pos-1;b<64;b++,c++)
  
{
  
   p2[c]=line[b];
  
}

for(int a=pos-1;a<pos+l;a++,m++)
  
{
  
   line[a]=p1[m];
  
}

strcat(line,p2);

cout<<endl<<"The line after inserting is:"<<line;

}

//===============================================================

void string::index(char *n)

{

char *p;

char n1[100];

char n2[100];

strcpy(n1,line);

strcpy(n2,n);

p=strstr(n1,n2);

int l;

l=strlen(n1);

cout<<"The words you want to find is:"<<n;

if(p)
  
{
  
   strcpy(n2,p);
  
   for(int i=0;i<l;i++)
   
   {
   
    if(n1[i]==n2[0])
    
    {
    
     cout<<endl<<"The position of the words which you wanted is "<<i+1;
    
     break;
    
    }
   
   }
  
}

else
  
{
  
   cout<<endl<<"There isn't the words which you wanted.";
  
}

}

//===============================================================

void string::replace(char *q,char *p)//q被替換串,p替換串

{

char p1[100];

char p2[100];

char p3[100];


strcpy(p1,line);

strcpy(p2,p);

strcpy(p3,q);

p=strstr(p1,p3);

if(p)

{

   int i,j;
  
   int lp=strlen(p);
  
   int lp1=strlen(p1);
  
   int lp2=strlen(p2);
  
   int lp3=strlen(p3);
  
   cout<<"The sourse string is : "<<line<<endl<<"You want to change : "<<p3;
  
   for(i=lp1-lp,j=0;j<lp2;i++,j++)
   
    line[i]=p2[j];
  
   for(j=lp3;p[j];i++,j++)
   
    line[i]=p[j];
  
   line[i]='/0';
  
   cout<<endl<<"After change : "<<line;

}

else

{
   cout<<q<<" is not the substring of "<<line;
}

}

//===============================================================
void string::strempty()

{

int i=strlen(line);

if(i=0)
  
{
  
   cout<<"The string is empty !";
  
}

else
  
{
  
   cout<<"The string is not empty !";
}
  
}

//===============================================================

string string::operator = (string s1)

{

strcpy(line,s1.line);

return *this;

}

//===============================================================

string string::operator + (string s1)

{

strcat(line,s1.line);

return *this;

}

void string::operator <= (string s1)

{

int l=strlen(s1.line);

int m=strlen(line);

if(m<=l)

{
  
   cout<<"less and equal";

}
  
else

{
  
   cout<<"not less and equal";
  
}

}
//===============================================================

void string::operator != (string s1)

{

char n1[100];

strcpy(n1,s1.line);

int l;

l=strlen(line);

int m;

m=strlen(n1);

if(l!=m)

   cout<<"judge not equal correct";

else
{
  
   for(int i=0;i<l;i++)
   
   {
   
    if(line[i]==n1[i])

    {
     if(i==l-1)

      cout<<"judge not equal error";
    }

    else

    {
     cout<<"judge not equal correct";

     break;

    }
   
   }
  
}

}

//===============================================================

void string::operator ==(string s1)

{

char n1[100];

strcpy(n1,s1.line);

int m;

m=strlen(n1);

int l;

l=strlen(this->line);

if(l!=m)

{

   cout<<"judge equal error";

}

else
  
{
  
   for(int i=0;i<l;i++)
   
   {
   
    if(line[i]==n1[i])

    {
     if(i==l-1)

      cout<<"judge equal correct";
    }

    else
    
    {
    
     cout<<"judge equal error";
    
     break;

    }

   }
  
}

}

//===============================================================

char string::operator [](int pos)

{

int i=pos;

int l=strlen(line);

if(i>-1&&i<l)
  
{
  
   cout<<"The word at "<<pos<<" is "<<line[i]<<endl;
   
    return line[i];
  
}

else

{
  
   cout<<"There is no word at "<<pos<<endl;

   return 0;

}

}

//===============================================================

void main()

{

string s("The line is a line.");

cout<<"s.print():"<<endl;

s.print();

cout<<endl<<"s.turn():"<<endl;

s.turn();

cout<<endl<<"The length of the string is "<<s.strlength();

cout<<endl<<"s.substring(4,12):"<<endl;
  
s.substring(4,12);

cout<<endl<<"s.insert(5,abcdefgh):"<<endl;

s.insert(5,"abcdefgh");

cout<<endl<<"s.index(LINE):"<<endl;

s.index("LINE");

cout<<endl<<"s.index(xyz):"<<endl;

s.index("xyz");

cout<<endl<<"s.replace(abc,DingTao)"<<endl;

s.replace("abc","DingTao");

cout<<endl<<"s.replace(req,dakfj)"<<endl;

s.replace("req","dakfj");

cout<<endl<<"s.strempty():"<<endl;

s.strempty();

string s1("abc"),s2("def"),s3("ghi"),s4("abcdefg"),s5("2008");

cout<<endl<<"s1=abc/ts2=def/ts3=ghi/ts4=abcdefg/ts5=2008"<<endl;

cout<<"after s1=s1+s2 , s1=";

s1=s1+s2;

s1.print();

cout<<endl<<"s1<=s4"<<endl;

s1<=s4;

cout<<endl<<"s2==s4"<<endl;

s2==s4;

cout<<endl<<"s2!=s3"<<endl;

s2!=s3;

cout<<endl<<"s5[3]"<<endl;

s5[3];

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章