C ++中的字计数器程序

时间:2014-02-04 03:30:16

标签: c++ arrays function

我正在学习C ++课程,并按如下方式进行了分配:

  

该程序从标准输入设备输入文本形式的手稿并分析   遇到的所有单词的长度。只有字母字符,数字和撇号   在单词中有助于单词长度。

教授给了我一些结构方面的帮助,但我仍然在苦苦挣扎。主要是每次从WordLength函数返回相应的长度时获取数组的正确位置。计划是使用数组元素1-15,超过15个字符长的任何单词都将进入数组的15个元素。我的代码如下:

#include <iostream>
#include <ctype.h>

using namespace std;

int WordLength();
void DisplayCount(int wordCount[]);

void main()
{
    int L;
    int Num_of_Char[16]={0};
    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[500];
    int End_Of_Word=0, Length=0, i=0;

    cout<<"Please enter some text:\n";
    cin.get(ch,500);

    while((!cin.eof)&&(!End_Of_Word))
    {
        if((i==0)&&(isspace(ch[i])))
        {
            ++i;
        }
        else if(isalnum(ch[i]))
        {
            ++Length;
            ++i;
        }
        else if ((ch[i]=='\'')&&((ch[i-1]=='s')||(ch[i-1]=='S'))&&(isspace(ch[i+1])))   //accounts for plural possessive of a word
        {
            ++Length;
            ++i;
        }
        else if ((ch[i]=='\'')&&((ch[i+1]=='s')||(ch[i+1]=='S')))   //accounts for single possessive of a word and keeps the hyphen as part of the word
        {
            ++Length;
            ++i;
        }
        else if((isspace(ch[i]))||(ispunct(ch[i]))||(ch[i]=='\0'))
        {
            ++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 Length\t\t"<<"Frequency\n";
    cout<<"\t-----------\t\t"<<"---------\n";

    for(int i=1; i<16; i++) 
    {
        cout<<"\t     "<<i<<"\t\t\t    "<<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
}

任何帮助,都将非常感谢!

1 个答案:

答案 0 :(得分:1)

最大的问题是你的WordLength()函数在循环的单次迭代后返回长度,所以它总是1.你需要将return语句放在循环之外。

至少还有一个问题。第一个单词没有计算在内,因为您从未将第一次调用的结果保存到WordLength()

最后,您还提示用户每次都输入文本,并且只获得他们在字符串中输入的第一个单词的长度。您可能希望在第一次调用WordLength()之前获取字符串。