|
今天按书上的写了个程序,检测所输入的一段文字当中,字母出现的次数,编译成功了,但不能正常运行,表现在输入完一段文字以后,程序在屏幕上输出一次,然后还让继续输入文字~~~死循环了好象。
我检查了半天,也没弄明白怎么回事,请高手指点,谢谢!
程序如下:
#include<iostream.h>
#include<iomanip>
#include<cassert>
class String;//定义新类
istream& operator>>(istream&,String&);
ostream& operator<<(ostream&,const String&);
//以下为具体定义
class String{
public:
String();
String(const char*);
String(const String&);
~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;
};
//以下为成员函数
inline String::String()
{
_size=0;
_string=0;
}
inline String::String(const char *str)
{
if(!str){
_size=0;_string=0;
}
else{
_size=strlen(str);
_string=new char[_size+1];
strcpy(_string,str);
}
}
inline String::String(const String &rhs)
{
_size=rhs._size;
if(!rhs._string)
_string=0;
else{
_string=new char[_size+1];
strcpy(_string,rhs._string);
}
}
inline String::~String(){delete[] _string;}
inline 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;
}
inline 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;
}
inline ostream& operator<<(ostream &os,String &s)
{
return os<<s.c_str();
}
inline bool String::operator==(const char *s)
{
return strcmp(_string,s)?false:true;
}
inline bool String::operator==(const String &rhs)
{
return strcmp(_string,rhs._string)?false:true;
}
inline char& String::operator[](int elem)
{
assert(elem>=0&&elem<_size);
return _string[elem];
}
//以下为主程序
int main()
{
int aCnt=0,eCnt=0,iCnt=0,oCnt=0,uCnt=0,theCnt=0,itCnt=0,wdCnt=0,notVowel=0;
String buf,the("the"),it("it");
//cin>>buf;
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]) |
|