角色计数程序无限循环

Character counting program infinite loop

本文关键字:无限循环 程序 色计 角色      更新时间:2023-10-16

我似乎正在陷入我正在为课堂上处理的角色计数程序上的无限循环。请看一下,让我知道我需要改进的地方。我已经在这件事上工作了一段时间,并继续遇到相同的情况。谢谢。

#include <iostream>
#include <ctype.h>
using namespace std;
int WordLength();
void DisplayCount(int wordCount[]);
int main()
{
    int L;
    int Num_of_Char[16]={0};
    cout<<"Please enter some text:n";
    L=WordLength();
    while (L)
    {
        L=WordLength();
        Num_of_Char[L]+=1;
    }
    DisplayCount(Num_of_Char);
}
/***************************************WordLength*******************************************
*   Action:         Analyzes the text that has been entered and decides what is and isn't   *
*                   a word (mainly by separating words by whitespaces and not accepting     *
*                   most punctuation as part of a word with the exception of hyphens which  *
*                   carry a partial word to the next line as well as apostrophes.           *
*                                                                                           *
*   Parameters:                                                                             *
*       IN:                                                                                 *
*                                                                                           *
*       OUT:                                                                                *
*                                                                                           *
*                                                                                           *
*   Returns:        The length of each word.                                                *
*                                                                                           *
*   Precondition:                                                                           *
*********************************************************************************************/
int WordLength()
{
    char ch;
    int End_Of_Word=0, Length=0; 
    ch=cin.get();
    while((!cin.eof())||(!End_Of_Word))
    {
        while (!isspace(ch))    //Spaces are delimeters of words
        {
            if(isalnum(ch))     //if current character is a alpha numeric character it is counted as part of the word
            {
                ++Length;
                cin.get(ch);
            }
            if ((ch==''')&&(isalnum(cin.peek())))  //apostrophes are counted as part of the word
            {
                ++Length;
            }
            if ((ch=='-')&&(isalnum(cin.peek()=='n'))) //the hyphen as part of the word if followed by newline
            {
                ++Length;
            }
            if((isspace(ch))||(ispunct(ch))||(ch=='n'))
            {
                ++End_Of_Word;
            }
        }
    }
    return Length;
}

/***************************************DisplayCount*****************************************
*   Action:         Displays how many words have a specific character count between 1 and   *
*                   15 characters. Then displays the average word character size.           *
*                                                                                           *
*   Parameters:                                                                             *
*       IN:         wordArray, which points to the array that holds the count of each word's*
*                   character size.                                                         *
*                                                                                           *
*       OUT:        Displays the array contents in a grid style as well as an average       *
*                   word size based on the contents of the array.                           *
*                                                                                           *
*   Returns:                                                                                *
*                                                                                           *
*   Precondition:   wordArray points to an int array                                        *
*********************************************************************************************/
void DisplayCount(int wordArray[])
{
    double sum = 0;
    cout<<"tWord Lengthtt"<<"Frequencyn";
    cout<<"t-----------tt"<<"---------n";
    for(int i=1; i<16; i++) 
    {
        cout<<"t     "<<i<<"ttt    "<<wordArray[i]<<endl;   //Displays the contents of each element
        sum+=(i*wordArray[i]);  //Keeps a running total of contents of array
    }
    cout<<"tAverage word length:  "<<sum/(15)<<endl;       //Displays the average word length
}

仅在某些条件下执行字符输入,可确保在其他情况下不会进行。

例如,在代码

    while (!isspace(ch))    //Spaces are delimeters of words
    {
        if(isalnum(ch))     //if current character is a alpha numeric character it is counted as part of the word
        {
            ++Length;
            cin.get(ch);
        }

如果ch不是一个空间,则外循环将继续迭代,如果碰巧ch不是字母数字,则cin.get将不执行,然后继续进行外循环的下一个迭代,等等在。

实现在这种情况下发生的事情的一种好方法是在源代码级调试器中运行该程序。Windows中的Visual Studio具有不错的功能。然后,您只需单步执行程序执行即可注意,每个步骤都很好。


一个无关但重要的问题:C角色分类功能(例如isalnum)不接受EOF以外的负面参数。因此,对于国际,使用char值的简单调用通常会具有不确定的行为,并且可能会在调试构建中崩溃。因此,始终将char的实际参数投入到unsigned char


另一个无关但重要的事情:void main是非标准的,会导致您的程序被许多编译器(包括G )拒绝,并且不仅仅是一个字符,而不仅仅是int main。换句话说,写void main非常愚蠢。AFAIK出于未知的原因&ndash;但这意味着您可以遇到void main,例如在Visual Studio生成的代码中。