|
楼主 |
发表于 2015-12-27 10:48:26
|
显示全部楼层
//string1.h
#include<iostream>
using std::ostream;
using std::istream;
class String
{
private:
char *CHAR;
int INT;
public:
String();
~String();
String(const char*);//String ax("avcd")或String ax="abcd"
String(const String &);//复制构造函数。
String & operator=(const String &);
String & operator=(const char *);
friend ostream &operator<<(ostream &,const String &);
friend istream &operator>>(istream &,const String &);
friend String operator+(const char *,const String &);
friend String operator+(const String &,const String &);
char &operator[](int );
bool operator==(const String &);
void Stringup();
void Stringlow();
int has(const char);
};
//string2.cpp
#include<string.h>
#include<cctype>
#include"String1.h"
using std::cout;
using std::cin;
String::String()
{
CHAR=new char[0];
INT=0;
}
String::~String()
{
delete [] CHAR;
cout<<"析构CHAR\n";
}
String::String(const char*c_r)
{
INT=strlen(c_r);
CHAR=new char[INT+1];
strcpy(CHAR,c_r);
}
String::String(const String &s1)
{
delete [] CHAR;
INT=strlen(s1.CHAR);
CHAR=new char[INT+1];
strcpy(CHAR,s1.CHAR);
}
String &String::operator=(const String &s2)
{
delete [] CHAR;
INT=strlen(s2.CHAR);
CHAR=new char[INT +1];
strcpy(CHAR,s2.CHAR);
return *this;
}
String &String::operator=(const char*s3)
{
delete [] CHAR;
INT=strlen(s3);
CHAR=new char[INT+1];
strcpy(CHAR,s3);
return *this;
}
ostream &operator<<(ostream &os,const String &s4)
{
os<<s4;
return os;
}
istream &operator>>(istream &on,String &s5)
{
char ip[100];
on.get(ip,100);
if(cin)
s5=ip;
while(cin&&cin.get()!='\n')
{
continue;
}
return on;
}
void String::Stringup()
{
for(int i=0;i<=INT;i++)
{
cout<<tolower(CHAR[i]);
}
}
void String::Stringlow()
{
for(int i=0;i<=INT;i++)
{
cout<<toupper(CHAR[i]);
}
}
bool String::operator==(const String &s6)
{
if(INT==s6.INT)
return true;
else
return false;
}
String operator+(const String &s7,const String&s8)
{
const int i=s7.INT+s8.INT;
char *ip=new char[i+1];
strcpy(ip,s7.CHAR);
strcat(ip,s7.CHAR);
String io;
io=ip;
return io;
}
String operator+(const char*ip,const String &s9)
{
int i=strlen(ip);
i+=s9.INT;
char *ppc=new char[i+1];
strcpy(ppc,ip);
std::strcat(ppc,s9.CHAR);
String io;
io=ppc;
return io;
}
int String::has(const char o)
{
int ii=0;
for(int i=0;i<=INT;i++)
{
if(CHAR[i]==o)
ii++;
}
return ii;
}
//string.cpp
#include "stdafx.h"
#include<iostream>
#include"String2.cpp"
using namespace std;
int main()
{
String s1("and I am a c++ student.");
String s2="please enter your name";
String s3;
cout << s2;
cin>>s3;
s2="my name is "+s3;
cout<<s2<<".\n";
s2=s2+s1;
s2.Stringup();
cout<<"the sting\n"<<s2<<"\ncontains"<<s2.has('A')<<"'A'characters in it.\n";
s1="red";
String rgb[3]={String(s1),String("green"),String("blue")};
cout<<"enter the name of a primary color for mixing light";
String ans;
bool success=false;
while(cin>>ans)
{
ans.Stringup();
for(int i=0;i<3;i++)
{
if(ans==rgb[i])
{
cout<<"that's right!\n";
success=true;
break;
}
}
if(success)
break;
else
cout<<"try again\n";
}
cout<<"bye\n";
return 0;
} |
|