石三笑 发表于 2003-4-10 01:30:00

字符串类(没带软盘)

#include "stdafx.h"
#include <iostream.h>
#include <string.h>
class cstring
{
private:
    char *m_a;
public:
    void fuzhi(char *p);
    void fuzhi(int i,char p);
    void display();
    void display(int i);
    int getlen();
    int getlen(char *p);
    void jiaru_q(char *p);
    void jiaru_h(char *p);
    int tonngji(char *p);
    void tihuan_word(char *p1,char *p2);
        cstring();
        void operator=(char *p);
};
cstring::cstring()
{
        m_a="bl world cc world";
}
void cstring::fuzhi(char *p)
{
       
                if(m_a!=0)
                delete []m_a;
                m_a=new char;
                strcpy(m_a,p);

}
void cstring::fuzhi(int i,char p)
{
    if(i>=0&&i<getlen(m_a))
      m_a=p;
    else
      cout<<"不能成功复制\n";
}
void cstring::display()
{
    cout<<m_a<<endl;
}
void cstring::display(int i)
{
    if(i>=0&&i<getlen(m_a))
      cout<<m_a<<endl;
}
int cstring::getlen()
{
    for(int i=0;m_a!='\0';i++)
    {}
    return i;
}
int cstring::getlen(char *p)
{
    for(int i=0;p!='\0';i++)
    {}
    return i;
}
void cstring::jiaru_q(char *p)
{
        int n1=getlen(p);
        int n2=getlen();
        char *temp=new char;
    strcpy(temp,p);
        strcat(temp,m_a);
        delete []m_a;
        m_a=temp;
}
void cstring::jiaru_h(char *p)
{
        int n1=getlen(p);
        int n2=getlen();
        char *temp=new char;
    strcpy(temp,m_a);
        strcat(temp,p);
        delete []m_a;
        m_a=temp;
   
}

int cstring::tonngji(char *p)
{
    int n2,m=0;
    int n1=getlen(p);
    for(int i=0;m_a!='\0';i++)
    {   n2=0;
      if(p==m_a)
      {
            n2=1;
            for(int j=1;p!='\0';j++)
                if(p==m_a)
                {
                  n2++;
      
                }
      }
      if(n1==n2)
            m++;
    }
    return m;
}

void cstring::tihuan_word(char *p1,char *p2)
{
    int n3,j;
    int m=0;
    int n1=getlen(p1);
    int n2=getlen(p2);
    if(n1==n2)
    {
      for(int i=0;m_a!='\0';i++)
      {
            n3=0;
            if(p1==m_a)
            {
                n3=1;
                for(j=1;p1!='\0';j++)
                  if(p1==m_a)
                        n3++;
            }      
            if(n1==n3)
            {
                for(j=0;p2!='\0';j++)
                {
                  m_a=p2;
                }
            }
      }
    }
    else
      cout<<"不能成功复制\n";
}

void cstring::operator =(char *p)
{
        int n=getlen(p);
        char *temp=new char;
        m_a=temp;
        strcpy(m_a,p);
}

int main(int argc, char* argv[])
{
    cstring a;
        a.display();
        a="snk--king of fight";
        cout<<"现在字符串:\n";
        a.display();
    int serve,i;
    char b,c;
    do
    {
      cout<<"你要进行什么操作:\n"
            <<"1.设置串值\n"
            <<"2.设定串中某一位置的值\n"
            <<"3.取得串值\n"
            <<"4.取得串中某一位置的值\n"
            <<"5.取得串长\n"
            <<"6.在字符串之前加入新子串\n"
            <<"7.在字符串之后加入新子串\n"
            <<"8.统计串中出现的某个字符或单词的个数\n"
            <<"9.对串中出现的某个字符或单词替换\n"
            <<"10.退出\n";
      cin>>serve;
      if(serve==1)
      {
            cout<<"请输入字符串\n";
            cin>>b;
            a.fuzhi(b);
      }
      if(serve==2)
      {
            cout<<"请输入在什么位置赋上什么字符\n";
            cin>>i;
            cout<<"什么字符\n";
                        cin>>b;
            a.fuzhi(i,b);
      }
      if(serve==3)
      {
            a.display();
                        cout<<endl;
      }
      if(serve==4)
      {
            cout<<"请输入你要取得哪个位置字符的";
            cin>>i;
            a.display(i);
      }
      if(serve==5)
      {
            cout<<a.getlen();
      }
      if(serve==6)
      {
            cout<<"你要在字符串之前加入什么:\n";
            cin>>b;
            a.jiaru_q(b);
      }
      if(serve==7)
      {
            cout<<"你要在字符串之后加入什么:\n";
            cin>>b;
            a.jiaru_h(b);
      }
      
      if(serve==8)
      {
            cout<<"你要对哪个单词进行统计:\n";
            cin>>b;
            cout<<a.tonngji(b);
      }
      
      if(serve==9)
      {
            cout<<"你要对哪字符或单词进行替换:\n";
            cin>>b;
            cout<<"替换成什么:\n";
            cin>>c;
            a.tihuan_word(b,c);
      }
    }while(serve!=10);
    return 0;
}
页: [1]
查看完整版本: 字符串类(没带软盘)