c++ - 字长频率,程序只打开cmd,什么都不做,C++

标签 c++

我一直在做一项任务。我已经尝试了很多解决方案,但我无法找到一种方法来使其工作。我的任务是创建一个代码,从文件中读取文本并显示单词长度的频率。也就是说,如果它读取“My name is Jon”,它应该显示“1 = 0, 2 = 2, 3 = 1, 4 = 1”(第一个数字是单词的长度,第二个是频率)。我写了一段代码,我很确定它可以正常工作,但它不起作用,它甚至不显示错误或什么都不显示,它只是打开 cmd 而什么都不做。这是我的代码:

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

using namespace std;

int NextWordLength(void); //function prototypes
void DisplayFrequencyTable(const int Words[]);

const int WORD_LENGTH = 16; // global constant for array

int main()
{
    int WordLength;  //actual length of word 0 to x
    int NumOfWords[WORD_LENGTH] = {0}; //array hold # of lengths of words

    WordLength=NextWordLength();
    while (WordLength)  //continue to loop until no word i.e. 0
    {
        (WordLength <= 14) ? (++NumOfWords[WordLength]):(++NumOfWords[15]);
        WordLength=NextWordLength();
    }
    DisplayFrequencyTable(NumOfWords);
}

int NextWordLength(void)
{
    fstream fin ("in.txt", ios::in);
    char Ch;
    int EndOfWord = 0; //tells when we have read in one word
    int LengthOfWord = 0;

    Ch = cin.get();  //get first character

    while (!cin.eof() && !EndOfWord)
    {
        while (isspace(Ch) || ispunct(Ch)) //skips elading white spaces
        {
            Ch = cin.get(); //and leading punctation marks
        }
        if (isalnum(Ch)) // if character is a letter or number
        {
            ++LengthOfWord;
        }
        Ch = cin.get(); //get next character
        if((Ch=='-')&&(cin.peek()=='\n')) //check for hyphenated word over two lines
        {
            Ch = cin.get();
            Ch = cin.get();

        }
        if ((Ch=='-')&&(isalpha(cin.peek()))) // check for hyphenated word in one line
        {
            ++LengthOfWord; //count the hyphen as part of word
            Ch = cin.get(); //get next character
        }
        if((Ch=='\n')&& (isalpha(cin.peek()))) //check for apostrophe in the word
        {
            ++LengthOfWord; //count apostrophe in word length
            Ch = cin.get(); //and get the next letter
        }
        if(isspace(Ch) || ispunct(Ch) || cin.eof()) //is it end of word
        {
            EndOfWord++;
        }
    }
    return LengthOfWord;
}


void DisplayFrequencyTable(const int Words[])
{
    int TotalWords = 0, TotalLength = 0;
    cout << "\nWord Length Frequency\n";
    cout << "------------ ----------\n";

    for (int i=1; i<=WORD_LENGTH-1; i++)
    {
        cout << setw(4)<<i<<setw(18)<<Words[i]<<endl;
        TotalLength += (i*Words[i]);
        TotalWords += Words[i];
    }
    cout << "\nAverage word length is ";

    if (TotalLength)
    {
        cout << float(TotalLength)/TotalWords << endl;
    }
    else cout << 0 << endl;
}

提前致谢。希望有人能帮忙。

最佳答案

尽管在 NextWordLength 中声明了 fin,但您的函数从不使用它。相反,它从 cin 读取,因此您的程序希望您输入文本以供其处理。

关于c++ - 字长频率,程序只打开cmd,什么都不做,C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24436365/

相关文章:

c++ - 从派生 * 到基 * 的转换存在但无法访问

c++ - 处理和触发从 QML 到 C++ 的事件,反之亦然

C#、C++ 项目组合 : Could not load file or assembly

c++ - 我在这个套接字中错过了什么? (Windows 套接字)

c++ - 从内容更改的文件夹中删除文件

c++ - 在 C/C++ 中求和 bool 值

c++ - Lambda 的实际使用示例

c++ - gcc 中自定义对象的 dlib 序列化失败

c++ - Const 引用以延长对象的生命,然后是 const_cast,这是个好主意吗?

c++ - 并行构建 OpenGL 模型?