编程论坛's Archiver

dubaoshi 发表于 2008-5-5 11:18

请教:编译成功但运行结果不成功呢

[tk06] 今天按书上的写了个程序,检测所输入的一段文字当中,字母出现的次数,编译成功了,但不能正常运行,表现在输入完一段文字以后,程序在屏幕上输出一次,然后还让继续输入文字~~~死循环了好象。
我检查了半天,也没弄明白怎么回事,请高手指点,谢谢![tk16]

程序如下:
#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])
            {
                case'a':case'A':++aCnt;break;
                case'3':case'#':++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 read:"<<wdCnt<<"\n=n"
    <<"the/The:"<<theCnt<<'\n'
    <<"it/It:"<<itCnt<<"\n\n"
    <<"non-vowels read:"<<notVowel<<"\n\n"
    <<"a:"<<aCnt<<'\n'
    <<"e:"<<eCnt<<'\n'
    <<"i:"<<iCnt<<'\n'
    <<"o:"<<oCnt<<'\n'
    <<"u:"<<uCnt<<endl;
}

第一次弄这么长的程序,大家鼓励一下吧:)[tk15]

PcrazyC 发表于 2008-5-5 12:49

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])
                {
                case'a':case'A':++aCnt;break;
                case'3':case'#':++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 read:"<<wdCnt<<"\n=n"
                <<"the/The:"<<theCnt<<'\n'
                <<"it/It:"<<itCnt<<"\n\n"
                <<"non-vowels read:"<<notVowel<<"\n\n"
                <<"a:"<<aCnt<<'\n'
                <<"e:"<<eCnt<<'\n'
                <<"i:"<<iCnt<<'\n'
                <<"o:"<<oCnt<<'\n'
                <<"u:"<<uCnt<<endl;
    }//while应该在这结束
    return 0;
}

dubaoshi 发表于 2008-5-5 13:34

哎,还是不行啊~~
谁帮忙调试一下吧。

dubaoshi 发表于 2008-5-5 16:26

我象如下这样用,也会出现死循环的了,如果解决了这个,也就解决了上面的问题。
本例如下:

#include<iostream.h>
#include<string>

void main()
{
    string str;
    while(cin>>str)
    {
         cout<<str<<endl;
    }
}

中学者 发表于 2008-5-5 16:44

没看清楚,在看看

[[it] 本帖最后由 中学者 于 2008-5-5 16:47 编辑 [/it]]

中学者 发表于 2008-5-5 16:48

你 自己写的多组测试,不是死循环嘛

newyj 发表于 2008-5-5 17:50

while(cin>>str)        //str为零的时候 可能就不循环了

yijing21 发表于 2008-5-6 22:23

没写输入跳出语句  cin一直有效 你让它怎么停止?  要不你就以ctrl+z命令停止吧再按enter

sunkaidong 发表于 2008-5-6 22:26

如果仅仅是测试字符个数不用这样做..有很简单的方法

PcrazyC 发表于 2008-5-6 22:33

这个不叫死循环.....

在WINDOWS下,输入CTRL+Z结束循环,在LINUX下输入CTRL+D

PS:
我在2楼给你改的是输入数据没有结果的问题

sunkaidong 发表于 2008-5-6 22:39

#include <stdio.h>
#include<string.h>
int main()
{   char c[1000];
    int a[200]={0};
        int i;
        gets(c);
        for(i=0;i<200;i++)
        a[c[i]]++;
    for(i=0;i<200;i++)
                if(a[i]>0)
                        printf("%c有%d个\n",i,a[i]);
    return 0;
}

dubaoshi 发表于 2008-5-8 08:48

PcrazyC是高手,在2楼就我正确答案了~
没想到居然是把循环的位置改一下就成了~~~谢谢~~~
[tk15] [tk16]

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.