具体代码为:
#include <iostream.h>
class String;
istream& operator>>(istream&,String&);
ostream& operator<<(ostream&,String&);
class String
{
public:
String();
String(const char*);
String(const String&);
virtual ~String();
String& operator=(const String&);
String& operator=(const char*);
bool operator==(const String&);
bool operator==(const char*);
char& operator[](int);
int size() { return _size;}
char* c_str() {return _string;}
private:
int _size;
char *_string;
void init(const char*);
void init(const String&);
};
////////////////////////////////////////////////////////////////
#include "String.h"
#include <string.h>
#include <assert.h>
#include <iomanip.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
inline void
String::init(const char *s)
{
if(!s)
{
_size=0;
_string=0;
}
else
{
_size = strlen(s);
_string = new char[_size+1];
strcpy(_string,s);
}
}
inline void
String::init(const String &rhs)
{
_size=rhs._size;
if(!rhs._string)
_string=0;
else
{
_string = new char[_size+1];
strcpy(_string,rhs._string);
}
}
String::String()
{
init(0);
}
String::String(const char *s)
{
init(s);
}
String::String(const String &rhs)
{
init(rhs);
}
bool String::operator ==(const String &rhs)
{
if(_size != rhs._size)
return false;
return strcmp(_string,rhs._string)?false:true;
}
bool String::operator==(const char *s)
{
return strcmp(_string,s)?false:true;
}
char& String::operator [] (int elem)
{
assert(elem >= 0 && elem < _size);
return _string[elem];
}
String&
String::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;
}
String&
String::operator = (const String &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;
}
istream& operator>>(istream &io,String &s)
{
const int limit_string_size = 4096;
char inBuf[limit_string_size];
io >> setw(limit_string_size) >> inBuf;
s = inBuf;
return io;
}
ostream& operator<<(ostream& os,String &s)
{
return os << s.c_str();
}
String::~String()
{
delete [] _string;
}
主函数:
#include "String.h"
int main()
{
int aCnt=0,iCnt=0,eCnt=0,oCnt=0,uCnt=0,bCnt=0,dCnt=0,fCnt=0,sCnt=0,
tCnt=0,theCnt=0,itCnt=0,wdCnt=0,notVowel=0;
String buf,the("the"),it("it");
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";
cout<<"Words read:"<<wdCnt<<"\n\n"
<<"the/The:"<<theCnt<<"\n"
<<"it/It:"<<itCnt<<"\n\n"
<<"non-vowels word:"<<notVowel<<"\n\n"
<<"a:"<<aCnt<<"\n"
<<"e:"<<eCnt<<"\n"
<<"i:"<<iCnt<<"\n"
<<"o:"<<oCnt<<"\n"
<<"u:"<<uCntendl;
return 0;
}
问题所在:在主函数中的while循环怎么也退不出来,不知道什么原因?请各位高手多多指点!谢谢!
#include <iostream.h>
class String;
istream& operator>>(istream&,String&);
ostream& operator<<(ostream&,String&);
class String
{
public:
String();
String(const char*);
String(const String&);
virtual ~String();
String& operator=(const String&);
String& operator=(const char*);
bool operator==(const String&);
bool operator==(const char*);
char& operator[](int);
int size() { return _size;}
char* c_str() {return _string;}
private:
int _size;
char *_string;
void init(const char*);
void init(const String&);
};
////////////////////////////////////////////////////////////////
#include "String.h"
#include <string.h>
#include <assert.h>
#include <iomanip.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
inline void
String::init(const char *s)
{
if(!s)
{
_size=0;
_string=0;
}
else
{
_size = strlen(s);
_string = new char[_size+1];
strcpy(_string,s);
}
}
inline void
String::init(const String &rhs)
{
_size=rhs._size;
if(!rhs._string)
_string=0;
else
{
_string = new char[_size+1];
strcpy(_string,rhs._string);
}
}
String::String()
{
init(0);
}
String::String(const char *s)
{
init(s);
}
String::String(const String &rhs)
{
init(rhs);
}
bool String::operator ==(const String &rhs)
{
if(_size != rhs._size)
return false;
return strcmp(_string,rhs._string)?false:true;
}
bool String::operator==(const char *s)
{
return strcmp(_string,s)?false:true;
}
char& String::operator [] (int elem)
{
assert(elem >= 0 && elem < _size);
return _string[elem];
}
String&
String::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;
}
String&
String::operator = (const String &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;
}
istream& operator>>(istream &io,String &s)
{
const int limit_string_size = 4096;
char inBuf[limit_string_size];
io >> setw(limit_string_size) >> inBuf;
s = inBuf;
return io;
}
ostream& operator<<(ostream& os,String &s)
{
return os << s.c_str();
}
String::~String()
{
delete [] _string;
}
主函数:
#include "String.h"
int main()
{
int aCnt=0,iCnt=0,eCnt=0,oCnt=0,uCnt=0,bCnt=0,dCnt=0,fCnt=0,sCnt=0,
tCnt=0,theCnt=0,itCnt=0,wdCnt=0,notVowel=0;
String buf,the("the"),it("it");
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";
cout<<"Words read:"<<wdCnt<<"\n\n"
<<"the/The:"<<theCnt<<"\n"
<<"it/It:"<<itCnt<<"\n\n"
<<"non-vowels word:"<<notVowel<<"\n\n"
<<"a:"<<aCnt<<"\n"
<<"e:"<<eCnt<<"\n"
<<"i:"<<iCnt<<"\n"
<<"o:"<<oCnt<<"\n"
<<"u:"<<uCntendl;
return 0;
}
问题所在:在主函数中的while循环怎么也退不出来,不知道什么原因?请各位高手多多指点!谢谢!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货