免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 970 | 回复: 3
打印 上一主题 下一主题

奇怪的程序 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-03-20 20:56 |只看该作者 |倒序浏览
#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 :perator==(const String1 &rhs)
{
        if (_size!=rhs._size)
                return false;
        return strcmp(_string,rhs._string)?false:true;
}
bool String1 :perator==(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:perator=(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:perator =(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:perator [](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();
}











void 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;
}

论坛徽章:
0
2 [报告]
发表于 2005-03-21 08:53 |只看该作者

奇怪的程序

能否排一下版。

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
3 [报告]
发表于 2005-03-21 09:27 |只看该作者

奇怪的程序

有什么问题?
  1. #include <iostream>;
  2. #include <cstring>;
  3. #include<iomanip>;
  4. #include<cassert>;


  5. using namespace std;

  6. class String1;
  7. istream& operator>;>;(istream&, String1&);
  8. ostream& operator<<(ostream&,const String1&);

  9. class String1
  10. {
  11.   public:
  12.   String1();
  13.   String1(const char*);
  14.   String1(const String1 &);
  15.   ~String1();
  16.   String1& operator=(const String1&);
  17.   String1& operator=(const char*);
  18.   bool operator==(const String1 &);
  19.   bool operator==(const char*);
  20.   char & operator[](int );
  21.   int size()   {return _size;}
  22.   char * c_str()    {return _string;}
  23.   private:
  24.   int _size;
  25.   char *_string;
  26. };

  27. bool String1::operator==(const String1 &rhs)
  28. {
  29. if (_size!=rhs._size)
  30. return false;
  31. return strcmp(_string,rhs._string)?false:true;
  32. }
  33. bool String1::operator==(const char *s)
  34. {

  35. return strcmp(_string, s)?false:true;
  36. }

  37. inline String1::String1()
  38. {
  39. _size=0;
  40. _string=0;
  41. }
  42. inline String1::String1(const char *str)   //出现问题的地方{
  43. {
  44. if(!str)
  45. {   
  46.         _size=0; _string=0;
  47. }
  48. else
  49. {
  50. _string=new char[_size+1];
  51. strcpy(_string,str);
  52. }
  53. }
  54. inline String1::String1(const String1 &rhs)
  55. {
  56. _size=rhs._size;
  57. if (rhs._string)
  58. _string=0;
  59. else
  60. {
  61. _string=new char [_size+1];
  62. strcpy(_string,rhs._string);
  63. }
  64. }
  65. inline String1::~String1()
  66. {
  67. delete [] _string;
  68. }
  69. inline String1& String1::operator=(const char *s)
  70. {
  71. if (!s)
  72. {
  73. _size=0;
  74. delete[] _string;
  75. _string=0;
  76. }
  77. else
  78. {
  79. _size=strlen(s);
  80. delete [] _string;
  81. _string=new char [_size+1];
  82. strcpy(_string,s);
  83. }
  84.    return *this;
  85. }
  86. inline String1& String1::operator =(const String1 &rhs)
  87. {
  88. if(this!=&rhs)
  89. {
  90. delete [] _string;
  91. _size=rhs._size;
  92. if(!rhs._string)
  93. _string=0;
  94. else{
  95. _string=new char [_size+1];
  96. strcpy(_string,rhs._string);
  97. }
  98. return *this;
  99. }
  100. }


  101. inline char &  String1::operator [](int elem)
  102. {
  103. assert(elem>;=0&&elem<_size);
  104. return _string[elem];
  105. }


  106. inline istream&   operator>;>;(istream &io, String1  &s)
  107. {
  108. const  int  limit_string_size=4096;
  109. char inBuf[limit_string_size];
  110. io>;>;setw(limit_string_size)>;>;inBuf;
  111. s=inBuf;
  112. return io;
  113. }

  114. inline ostream& operator<<(ostream&  os,String1 &s)
  115. {
  116. return os<<s.c_str();
  117. }











  118. int main()
  119. {
  120. int aCnt=0,eCnt=0,iCnt=0,oCnt=0,uCnt=0,
  121. theCnt=0,itCnt=0,wdCnt=0,notVowel=0;
  122. String1 buf, the("the"),it ("it");

  123. cout<<"输入"<<endl;

  124. while (cin>;>;buf)
  125. {
  126. ++wdCnt;
  127. cout<<buf<<" ";
  128. if(wdCnt%12==0)
  129. cout<<endl;
  130. if(buf==the||buf =="The")
  131. ++theCnt;
  132. else   if(buf==it||buf=="It")
  133. ++itCnt;
  134. for(int ix=0;ix<buf.size();++ix)
  135. {
  136. switch (buf[ix])
  137. {
  138. case'a':  case'A':  ++aCnt; break;
  139. case'e':  case'E':  ++eCnt; break;
  140. case'i':  case'I':  ++iCnt; break;
  141. case'o':  case'O':  ++oCnt; break;
  142. case'u':  case'U':  ++uCnt; break;
  143. default: ++notVowel; break;
  144. }
  145. }
  146. }
  147. cout<<"\n\n"
  148. <<"words tead:"<<wdCnt<<"\n\n"
  149. <<"the\\The:"<<  theCnt<<"\n"
  150. <<"it\\It:"<<  itCnt <<"\n"
  151. <<"non-vowels read:"<<notVowel<<"\n\n"
  152. <<"a:"<<aCnt<<"\n"
  153. <<"e:"<<eCnt<<"\n"
  154.        <<"i:"<<iCnt<<"\n"
  155. <<"o:"<<oCnt<<"\n"
  156. <<"u:"<<uCnt<<"\n";
  157. // return 1;
  158. }
复制代码
结果有问题.

论坛徽章:
0
4 [报告]
发表于 2005-03-21 21:29 |只看该作者

奇怪的程序

有,昨天没看出来,就是编译时没错,执行不了;
今天终于看出来了。
inline String1 ::String1(const char *str)   //出现问题的地方{
if(!str)
{   
        _size=0; _string=0;
}
else
{ 这里没有对_size赋值。。。。
_string=new char[_size+1];
strcpy(_string,str);
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP