首页 > 技术文章 > 字符串类

TQCAI 2017-09-28 15:15 原文

C++ code

#pragma once
#include "stdafx.h"
#include "stdio.h"
#include <string>

using namespace std;

class CStr
{
public:
    char* str;
    CStr()
    {
        length=0;
    }
    CStr(string obj)
    {
        length=obj.length();
        str=new char[length+1];
        for(int i=0;i<length;i++) str[i]=obj[i];
        str[length]=0;
    }
    CStr(int a)
    {//0,48
        char tmp[100];
        int len=0,i;
        while(a){
            tmp[len++]=a%10;
            a/=10;
        }
        length=len;
        str=new char[len+1];
        for(i=0;i<len;i++) str[len-i-1]=tmp[i]+48;
        str[len]=0;
    }
    CStr(double a)
    {
        int b=(int)a;
        int preserve=b;
        char tmp[100];
        int len=0,i;
        while(b>0){
            tmp[len++]=b%10;
            b/=10;
        }
        CStr s1;
        s1.length=len;
        s1.str=new char[len+1];
        for(i=0;i<len;i++) s1.str[len-i-1]=tmp[i]+48;
        s1.str[len]=0;
        printf(s1.str);
        //处理小数部分
        printf("%f\n",a);
        a-=(double)preserve;
        printf("%f\n",a);
        printf("%d\n",preserve);
        len=0;
        while(a>0.001)
        {
            a*=10.0;
            int t=int(a);
            tmp[len++]=t;
            a-=(double)t;
            printf("t=%d,a=%f\n",t,a);
        }
        CStr s2;
        s2.length=len;
        s2.str=new char[len+1];
        for(i=0;i<len;i++) s2.str[i]=tmp[i];
        s2.str[len]=0;
        printf("%d,%d",s1.length,s2.length);
        //整合
        length=s1.length+s2.length+1;
        str=new char[length+1];
        for(i=0;i<s1.length;i++) str[i]=s1.str[i];
        str[s1.length]='.';
        for(i=0;i<s2.length;i++) str[i+s1.length+1]=s2.str[i];
        str[length]=0;
    }
    CStr( CStr &obj)
    {
        length=0;
        str=new char[obj.getLength()];
        for(int i=0;i<obj.getLength();i++) str[i]=obj.str[i];
        length=obj.getLength();
        str[length]=0;
    }
    CStr(char* ch)
    {
        initOperate(ch);
    }
    void operator =(char* ch)
    {
        initOperate(ch);
    }
    int getLength()
    {
        return length;
    }
    friend  CStr operator +( CStr &s1, CStr &s2)
    {
        CStr * obj=new CStr;
        int len=s1.getLength()+s2.getLength();
        obj->length=len;
        obj->str=new char[len+1];
        int i;
        for(i=0;i<s1.getLength();i++) obj->str[i]=s1.str[i];
        for(i=0;i<s2.getLength();i++) obj->str[i+s1.getLength()]=s2.str[i];
        obj->str[len]=0;
        return *obj;
    }
    friend  CStr operator +(char *ch, CStr &s2)
    {
        CStr s1(ch);
        CStr * obj=new CStr;
        int len=s1.getLength()+s2.getLength();
        obj->length=len;
        obj->str=new char[len+1];
        int i;
        for(i=0;i<s1.getLength();i++) obj->str[i]=s1.str[i];
        for(i=0;i<s2.getLength();i++) obj->str[i+s1.getLength()]=s2.str[i];
        obj->str[len]=0;
        return *obj;
    }
    friend  CStr operator +( CStr &s1,char *ch)
    {
        CStr s2(ch);
        CStr * obj=new CStr;
        int len=s1.getLength()+s2.getLength();
        obj->length=len;
        obj->str=new char[len+1];
        int i;
        for(i=0;i<s1.getLength();i++) obj->str[i]=s1.str[i];
        for(i=0;i<s2.getLength();i++) obj->str[i+s1.getLength()]=s2.str[i];
        obj->str[len]=0;
        return *obj;
    }
    int str2int()
    {
        return str2int_procedure(str,length);
    }
    int str2int(char* ch)
    {
        return str2int_procedure(ch,getstrlen(ch));
    }
    CStr Right(int pos)
    {
        if (pos>length) return NULL;
        CStr re;
        re.length=pos;
        re.str=new char[pos+1];
        int i,j;
        for(i=length-pos,j=0;i<length;i++,j++)
        {
            re.str[j]=str[i];
        }
        re.str[pos]=0;
        return re;
    }
    CStr Left(int pos)
    {
        if (pos>length) return NULL;
        CStr re;
        re.length=pos;
        re.str=new char[pos];
        int i,j;
        for(i=0,j=0;i<pos;i++,j++)
        {
            re.str[j]=str[i];
        }
        re.str[pos]=0;
        return re;
    }
    CStr Mid(int a,int b)
    {
        if(b<a || a<0 || b>length-1) return NULL;
        CStr re;
        re.length=b-a+1;
        re.str=new char[re.length+1];
        int i,j;
        for(i=a,j=0;i<=b;i++,j++)
        {
            re.str[j]=str[i];
        }
        re.str[re.length]=0;
        return re;
    }
    void DeleteAt(int pos)
    {
        length-=1;
        int j=pos;
        do{
            str[j]=str[j+1];
            j+=1;
        }while(str[j]);
    }
    CStr* split(char ch,int& n)
    {
        int i=0;
        int pos=0;
        n=0;
        //1.删除多余空格
        i=0;
        while(str[i])
        {
            if(str[i]==ch && str[i]==str[i+1])
            {//匹配到,全部左移
                DeleteAt(i);
            }
            else
                i++;
        }
        //2.去除首尾空格
        if(str[0]==ch)
        {
            DeleteAt(0);
        }
        if(str[length-1]==ch)
        {
            DeleteAt(length-1);
        }
        //3.统计空格数从而计算n
        i=0;
        while(str[i]) if(str[i++]==ch) n++;
        n++;
        //4.新建返回对象
        CStr* re=new CStr[n];
        //5.进行分割
        if(n==1) //只有一个,直接返回
        {
            re[0]=CStr(str);
            return re;
        }
        int left=0,right=0;
        bool flag=1;
        for(i=0;i<=length;i++)
        {
            if(i==length || str[i]==ch)
            {
                right=i-1;
                re[pos++]=Mid(left,right);
                flag=1;
            }
            else
            {
                if(flag) 
                {
                    left=i;
                    flag=0;
                }
            }
        }
    //    printf(str);
        return re;
    }
protected:
private:
    int length;
    void initOperate(char* ch)
    {
        length=0;
        int i;
        for(i=0;ch[i]!=0;i++) length++;
        str=new char[length+1];
        for(i=0;i<length;i++) str[i]=ch[i];
        str[length]=0;
    //    printf("%d\n",length);
    }
    int str2int_procedure(char* ch,int len)
    {
        int re=0;
        int pow=1;
        for(int i=len-1;i>=0;i--)
        {
            if(ch[i]>=48 && i<=57){
            re+=pow*(ch[i]-48);
            pow*=10;}
        }
        return re;
    }
    int getstrlen(char * ch)
    {
        int len=0;
        int i;
        for(i=0;ch[i]!=0;i++) len++;
        return len;
    }
};

 

推荐阅读