/*std*/string类的实现,

  1 //class mstring 
  2 //function:
  3 //     friend function :
  4 //            +,<<,>>,
  5 //    member function:
  6 //            get_length,<,>,==,printf,
  7 
  8 #include<cstring>
  9 #include<iostream>
 10 #include<istream>
 11 #include<ostream>
 12 class mstring{
 13     friend const mstring operator+(const mstring&str1,const mstring&str2);//0514修改,添加 const,防止出现a+b=c的情况;
 14     friend const mstring operator+(const char*str1,const mstring&str2);
 15     friend const mstring operator+(const mstring&str1,const char*str2);
 16     friend std::ostream&operator<<(std::ostream&os,const mstring&str);
 17     friend std::istream&operator>>(std::istream&is,mstring&str);
 18     public:
 19         mstring();
 20         mstring(char*str);
 21         mstring(const mstring& str);
 22         mstring(int n);
 23         ~mstring(){
 24             delete[]m_data;
 25         }
 26         mstring& operator=(char*str);
 27         mstring& operator=(const mstring&str);
 28         bool operator<(const mstring&str)const;
 29         bool operator>(const mstring&str)const;
 30         bool operator==(const mstring&str)const;
 31         void printf()const;
 32         int get_length()const{
 33             return strlen(m_data);
 34         }
 35         mstring substring(int i,int j)const;
 36     private:
 37         char*m_data;
 38             
 39 };
 40 
 41 mstring::mstring(){
 42     m_data=new char;
 43     *m_data='\0';
 44 }
 45 mstring::mstring(char*str){
 46     m_data=new char[strlen(str)+1];
 47     strcpy(m_data,str);    
 48 }
 49 mstring::mstring(const mstring&str){
 50     m_data=new char[strlen(str.m_data)+1];
 51     strcpy(m_data,str.m_data);
 52 }
 53 mstring::mstring(int n){
 54     m_data=new char[n+1];
 55     int i=0;
 56     for(i=0;i<n;i++){
 57         m_data[i]='0';
 58     }
 59     m_data[n]='\0';
 60 }
 61 mstring& mstring::operator=(char*str){
 62     delete [] m_data;
 63     m_data=NULL;
 64     m_data=new char[strlen(str)+1];
 65     strcpy(m_data,str);    
 66     return *this;
 67 }
 68 mstring& mstring::operator=(const mstring&str){
 69     if(this==&str) return *this;
 70     delete []m_data;
 71     m_data=NULL;
 72     m_data=new char[strlen(str.m_data)+1];
 73     strcpy(m_data,str.m_data);
 74     return *this;
 75 }
 76 bool mstring::operator<(const mstring&str)const{
 77     if(strcmp(this->m_data,str.m_data)<0) return true;
 78     else return false;
 79 }
 80 bool mstring::operator>(const mstring&str)const{
 81     if(strcmp(this->m_data,str.m_data)>0) return true;
 82     else return false;
 83 }
 84 bool mstring::operator==(const mstring&str)const{
 85 if(strcmp(this->m_data,str.m_data)==0) return true;
 86     else return false;
 87 }
 88 
 89 const mstring operator+(const mstring&str1,const mstring&str2){
 90     int l1=str1.get_length();
 91     int l2=str2.get_length();
 92     mstring tmp(l1+l2);
 93     for(int i=0;i<l1;i++){
 94         tmp.m_data[i]=str1.m_data[i];
 95     }
 96     for(int i=0;i<l2;i++){
 97         tmp.m_data[l1+i]=str2.m_data[i];
 98     }
 99     return tmp;
100     
101 }
102 
103 const mstring operator+(const char*str1,const mstring&str2){
104     int l1=strlen(str1);
105     int l2=str2.get_length();
106     mstring tmp(l1+l2);
107     for(int i=0;i<l1;i++){
108         tmp.m_data[i]=str1[i];
109     }
110     for(int i=0;i<l2;i++){
111         tmp.m_data[l1+i]=str2.m_data[i];
112     }
113     return tmp;
114 }
115 
116 const mstring operator+(const mstring&str1,const char*str2){
117     int l1=str1.get_length();
118     int l2=strlen(str2);
119     mstring tmp(l1+l2);
120     for(int i=0;i<l1;i++){
121         tmp.m_data[i]=str1.m_data[i];
122     }
123     for(int i=0;i<l2;i++){
124         tmp.m_data[l1+i]=str2[i];
125     }
126     return tmp;
127 }
128 
129 std::ostream&operator<<(std::ostream&os,const mstring&str){
130     os<<str.m_data;
131     return os;
132 }
133 
134 std::istream&operator>>(std::istream&is,mstring&str){
135     char buf[1024];
136     is>>buf;
137     str=buf;
138     return is;
139 }
140 
141 void mstring::printf()const{
142     for(int i=0;m_data[i]!=0;i++){
143                 std::cout<<m_data[i];
144     }
145     std::cout<<std::endl;
146 }
147 
148 mstring mstring::substring(int i,int j)const{
149     int strlength=strlen(this->m_data);
150     if(i>j||i>strlength-1||j<0){
151         mstring mtemp;
152         return mtemp;
153     }
154     if(i<0) i=0;
155     if(j>strlength-1) j=strlength-1;
156     mstring mtemp(j-i+1); 
157     int index=0,k=0;
158     for(index=0;index<=strlength-1;index++){
159         if(index>=i&&index<=j)
160         {
161             mtemp.m_data[k++]=this->m_data[index];
162         }
163     }
164     return mtemp;
165 }
166 
167 int main(){
168     mstring ms,ms2,ms3;
169     ms="adbc";
170     ms2=ms;
171     ms.printf();
172     ms2.printf();
173     std::cout<<ms.get_length();
174     std::cout<<std::endl;
175     ms3=ms2.substring(1,5);
176     ms3.printf();
177     mstring ms4,ms5;
178     std::cin>>ms4;
179     std::cin>>ms5;
180     std::cout<<ms4<<std::endl;
181     std::cout<<ms5<<std::endl;
182     ms5=ms4+ms5;
183     std::cout<<ms5<<std::endl;
184     return 0;
185 }
186 
187                 

 

posted on 2015-05-11 19:18  喜悦的凉白开  阅读(183)  评论(0编辑  收藏  举报

导航