课堂作业1

来源:互联网 发布:苏州园区网络托管 编辑:程序博客网 时间:2024/04/30 07:16
#include <cstring>  
#include<windows.h>  
class String  
{  
public:  
String(){p=NULL;}  
String(const String &);  
String(char *str);  
    ~String();  
String & operator=(const String &);  
String operator+(const String &);  
friend bool operator>(String &string1,String &string2);  
friend bool operator< (String &string1, String &string2);  
friend bool operator== (String &string1, String &string2);  
friend bool operator!= (String &string1, String &string2);  
friend ostream& operator << (ostream&,const String&);    
    friend istream& operator >> (istream&,String&);     
    void display();  
private:  
char *p;  
};  
String::String(const String &s)  
{  
if(s.p)  
   p=new char[strlen(s.p)+1];   
else p=0;    
    strcpy(p,s.p);    
}    
  
String::String(char *str)  
{  
 p=str;  
}  
String::~String()  
{  
 if(p!=NULL)  
 delete p;  
 p=NULL;  
}  
String& String::operator=(const String &s)  
{  
if(p)   
{delete[]p; p=new char[strlen(s.p)+1];}  
else    
p=new char[strlen(s.p)+1];  
strcpy(p,s.p);  
        return *this;  
}  
String String::operator +(const String &s)  
{  
 String temp;  
 if(temp.p!=NULL)  
 delete[]temp.p;  
 temp.p = new char[strlen(p)+strlen(s.p)+1];  
 strcpy(temp.p,p);  
 strcat(temp.p,s.p);  
 return temp;  
}  
bool operator>(String &string1,String &string2)  
{  
 if(strcmp(string1.p,string2.p)>0)  
 return true;  
 else  
 return false;  
}  
bool operator<(String &string1,String &string2)  
{  
 if(!operator>(string1,string2))  
 return true;  
 else  
 return false;  
}  
bool operator==(String &string1,String &string2)  
{  
 if(strcmp(string1.p,string2.p)==0)  
 return true;  
 else  
 return false;  
}  
bool operator !=(String &string1, String &string2)  
{  
 if(!(string1.p==string2.p))  
 return true;  
 else  
 return false;  
}  
ostream& operator <<(ostream &output,const String &s)  
{  
 output<<s.p;  
 return output;  
}  
istream& operator >>(istream &intput,String &s)  
{  
 //cout<<"input a string:";  
  
char temp[100000] = {"",};  
  
 intput>>temp;  
s.p=temp;  
 return intput;  
}  
void String::display()  
{  
 cout<<p<<endl;  
}  
void compare(String &string1,String &string2)  
{if(operator>(string1,string2)==1)  
     { cout<<"string1>string2"<<endl;}  
else  
if(operator<(string1,string2)==1)  
{  
 cout<<"string1<string2"<<endl;  
}  
if(operator==(string1,string2)==1)  
{  
 cout<<"string1=string2<<endl";  
}  
if(operator==(string1,string2)!=1)  
{  
 cout<<"string1!=string2"<<endl;  
}  
}  
int main()  
{  
 String string1("Hello"),string2("World");  
 cout<<"输出string1:";  
 string1.display();  
 cout<<"输出string2:";  
 string2.display();  
 cout<<"比较string1和string2:";  
 compare(string1,string2);  
 String string4,string5,string6;  
 cout<<"string1和string2连接后赋值给string4:"<<endl;  
 string4=string1+string2;  
 string4.display();  
 cout<<"输入string5:";  
 cin>>string5;   
 cout<<"输出string5:"<<endl;  
 cout<<string5<<endl;  
 cout<<"string1赋值给string6:"<<endl;  
 string6=string1;  
 string6.display();  
 system("pause");  
 return 0;  
}   
0 0
原创粉丝点击