如何计算输入为cstring数组的单词和行 [英] How do I count words and lines that were input as a cstring array

查看:128
本文介绍了如何计算输入为cstring数组的单词和行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此程序应从用户提供的文本文件中读取一个段落,然后将每行存储在一个破烂的char数组中,并计算该段落的单词和行的总数,然后显示结果。

This program should read a paragraph from a text file provided by the user and then store each line in a ragged char array and count the total number of words and lines of the paragraph then display the result.

我不知道为什么行数总是给我3
的结果,为什么单词数总是缺少2个单词。

I can't figure out why does the number of lines keep giving me the result of 3 and why the number of words is always missing 2 words.

请帮助并记住,我不是专业人士,最近才刚开始学习c ++。

please help and keep in mind that I'm no professional just started learning c++ recently.

#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

int main()
{
    ifstream infile;//declarations
    char filename[45];
    char **data = 0;
    char **temp = 0;
    char *str = 0;
    char buffer[500], c;
    int numoflines = 0, numofwords = 0;

    cout << "Please enter filename: ";//prompt user to enter filename
    cin >> filename;

    infile.open(filename);//open file

    if (!infile)//check if file was successfully opened
    {
        cout << "File was not successfully opened" << endl << endl;//display error message
        return 0;
    } //end of if

    data = new char*[numoflines];

    while (!infile.eof())
    {
        temp = new char*[numoflines + 1];

        for (int i = 0; i < numoflines; i++)
        {
            infile.getline(buffer, 500);//read line
            for (int i = 0; i < strlen(buffer); i++)//count number of words in line
            {
                c = buffer[i];
                if (isspace(c))
                    numofwords++;
            }
            str = new char[strlen(buffer) + 1];//allocate a dynamic array
            strcpy(str, buffer);
            data[i] = str;
            temp[i] = data[i];
        }//end of for

        infile.getline(buffer, 500);
        for (int i = 0; i < strlen(buffer); i++)//count number of words in line
        {
            c = buffer[i];
            if (isspace(c))
                numofwords++;
        }

        temp[numoflines] = new char[strlen(buffer) + 1];
        strcpy(temp[numoflines], buffer);

        delete[] data;
        data = temp;
        numoflines++;
    }

    cout << "Number of words: " << numofwords << endl;
    cout << "Number of lines: " << numoflines << endl;

    return 0;
}


推荐答案

行数的概念仅是观看概念。它没有编码到文件中。根据编辑器窗口的大小,以下单个段落可以显示为一行或16行:

The concept of number of lines is a viewing concept only. It's not encoded into the file. The following single paragraph can be displayed on one line or 16 lines depending upon the size of the editor window:

如果要指定字符宽度为在窗口中,可以从中计算出行数,但是照原样,段落和字数统计都一样。并成功打开了 ifstream infile ,该文件很容易获得:

If you wanted to specify a char width of the window, lines could be calculated from that, but as is, paragraphs and wordcount are as good as you will do. And given a successfully opened ifstream infile that is fairly simple to obtain:

auto numoflines = 0;
auto numofwords = 0;
string paragraph;

while(getline(infile, paragraph)) {
    numofwords += distance(istream_iterator<string>(istringstream(paragraph)), istream_iterator<string>());
    ++numoflines;
}

cout << "Number of words: " << numofwords << "\nNumber of lines: " << numoflines << endl;

注意:

Visual Studio支持 istringstream 的内联构造,因此,如果不使用它,则需要在单独的行上进行构造。

Visual Studio supports inline construction of an istringstream so if you're not using that you'll need to construct on a separate line.

这篇关于如何计算输入为cstring数组的单词和行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆