- 论坛徽章:
- 1
|
奇怪的程序
有什么问题?
- #include <iostream>;
- #include <cstring>;
- #include<iomanip>;
- #include<cassert>;
- using namespace std;
- class String1;
- istream& operator>;>;(istream&, String1&);
- ostream& operator<<(ostream&,const String1&);
- class String1
- {
- public:
- String1();
- String1(const char*);
- String1(const String1 &);
- ~String1();
- String1& operator=(const String1&);
- String1& operator=(const char*);
- bool operator==(const String1 &);
- bool operator==(const char*);
- char & operator[](int );
- int size() {return _size;}
- char * c_str() {return _string;}
- private:
- int _size;
- char *_string;
- };
- bool String1::operator==(const String1 &rhs)
- {
- if (_size!=rhs._size)
- return false;
- return strcmp(_string,rhs._string)?false:true;
- }
- bool String1::operator==(const char *s)
- {
- return strcmp(_string, s)?false:true;
- }
- inline String1::String1()
- {
- _size=0;
- _string=0;
- }
- inline String1::String1(const char *str) //出现问题的地方{
- {
- if(!str)
- {
- _size=0; _string=0;
- }
- else
- {
- _string=new char[_size+1];
- strcpy(_string,str);
- }
- }
- inline String1::String1(const String1 &rhs)
- {
- _size=rhs._size;
- if (rhs._string)
- _string=0;
- else
- {
- _string=new char [_size+1];
- strcpy(_string,rhs._string);
- }
- }
- inline String1::~String1()
- {
- delete [] _string;
- }
- inline String1& String1::operator=(const char *s)
- {
- if (!s)
- {
- _size=0;
- delete[] _string;
- _string=0;
- }
- else
- {
- _size=strlen(s);
- delete [] _string;
- _string=new char [_size+1];
- strcpy(_string,s);
- }
- return *this;
- }
- inline String1& String1::operator =(const String1 &rhs)
- {
- if(this!=&rhs)
- {
- delete [] _string;
- _size=rhs._size;
- if(!rhs._string)
- _string=0;
- else{
- _string=new char [_size+1];
- strcpy(_string,rhs._string);
- }
- return *this;
- }
- }
- inline char & String1::operator [](int elem)
- {
- assert(elem>;=0&&elem<_size);
- return _string[elem];
- }
- inline istream& operator>;>;(istream &io, String1 &s)
- {
- const int limit_string_size=4096;
- char inBuf[limit_string_size];
- io>;>;setw(limit_string_size)>;>;inBuf;
- s=inBuf;
- return io;
- }
- inline ostream& operator<<(ostream& os,String1 &s)
- {
- return os<<s.c_str();
- }
- int main()
- {
- int aCnt=0,eCnt=0,iCnt=0,oCnt=0,uCnt=0,
- theCnt=0,itCnt=0,wdCnt=0,notVowel=0;
- String1 buf, the("the"),it ("it");
- cout<<"输入"<<endl;
- while (cin>;>;buf)
- {
- ++wdCnt;
- cout<<buf<<" ";
- if(wdCnt%12==0)
- cout<<endl;
- if(buf==the||buf =="The")
- ++theCnt;
- else if(buf==it||buf=="It")
- ++itCnt;
- for(int ix=0;ix<buf.size();++ix)
- {
- switch (buf[ix])
- {
- case'a': case'A': ++aCnt; break;
- case'e': case'E': ++eCnt; break;
- case'i': case'I': ++iCnt; break;
- case'o': case'O': ++oCnt; break;
- case'u': case'U': ++uCnt; break;
- default: ++notVowel; break;
- }
- }
- }
- cout<<"\n\n"
- <<"words tead:"<<wdCnt<<"\n\n"
- <<"the\\The:"<< theCnt<<"\n"
- <<"it\\It:"<< itCnt <<"\n"
- <<"non-vowels read:"<<notVowel<<"\n\n"
- <<"a:"<<aCnt<<"\n"
- <<"e:"<<eCnt<<"\n"
- <<"i:"<<iCnt<<"\n"
- <<"o:"<<oCnt<<"\n"
- <<"u:"<<uCnt<<"\n";
- // return 1;
- }
复制代码 结果有问题. |
|