C++程序需要行单词和字符计数

C++ program need lines words and characters count

本文关键字:字符 单词 程序 C++      更新时间:2023-10-16

>我有一个作业,我需要计算文件中的行、单词和字符。我在计算正确数量的字符和单词时遇到了问题,因为如果它获得双倍的空间,它就会像字符和单词一样计数。

输出应为

lines  words  characters  filename
 3     5        29        testfile

法典:

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <iomanip>
using namespace std;
int main(int argc, const char * argv[]) {
string lines, words, chars, file1, file2;
ifstream infile;
ofstream outfile;
char c;
int countLines = 0;
int countChars = 0;
int countWords = 0;
cout<< "Enter the file name" << endl;
cin >> file1;
infile.open(file1.c_str());

while(!infile.eof())
{
    if(infile.peek() == 1)
        break;
    c = infile.get();
    if(c != 'n')
       countChars++;
    else
        countLines++;
    if(c == ' '|| c =='n')
        countWords++;
}
  //  countChars = countChars - countWords;

cout << setw(12) << countLines << setw(12) << countWords << setw(12) << countChars  << endl;
infile.close();

return 0;
}

使用 getline 逐行读取文件

while(getline(file,str))
{
    countLines++;
    countChars += str.length();
    countWords += CountWords(str);
 }

哪个文件是 iofstream 对象,str 是字符串。对于计算字数(CountWords(,您有几种方法。其中之一是:

#include <vector>
#include <string>
#include <boost/algorithm/string/split.hpp>
int countWords(std::string str) {
  vector< std::string > result;
  boost::algorithm::split_regex(result, str, regex( "\s+" ));
  return result.size();
}
我相信

OP提出这个问题的目的是找出他/她的代码不起作用的原因,因此我将从这个角度回答

计算正确的字数

  1. C++将EOF(文件结尾(定义为-1,因此也要检查EOF,否则您将错过字数统计。

如果它得到双倍的空间,它就像一个字符和一个单词。

  1. 您可以使用布尔测试来解决此问题,如果您遇到空格,请打开布尔值,如果下一个字符也是空格,则跳过。

  2. 我想你的字符数不计入标点符号? 所以检查c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'.如果您的作业也将标点符号计为字符数,请忽略这一点。

下面是根据您的代码修改的正确版本代码。

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <iomanip>
using namespace std;
int main(int argc, const char * argv[]) {
    string lines, words, chars, file1, file2;
    ifstream infile;
    ofstream outfile;
    char c;
    bool findNextString = false;
    int countLines = 0;
    int countChars = 0;
    int countWords = 0;
    cout << "Enter the file name" << endl;
    cin >> file1;
    infile.open(file1.c_str());

    while (!infile.eof())
    {
        if (infile.peek() == 1)
            break;
        c = infile.get();
        // use the boolean to find next valid string
        if (findNextString && c == ' ')
            continue;
        else
            findNextString = false;
        // there is a structure issue with your code.
        // you should think of the priority of checking
        // do not check by rejection, because you will count in punctuation too.
        if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
        {
            countChars++;
        }
        else if (c == 'n')
        {
            countLines++;
            countWords++; // <- add word too
        }
        else if (c == ' ' || c == EOF)
        {
            countWords++;
            findNextString = true;
        }
    }
    cout << setw(12) << countLines << setw(12) << countWords << setw(12) << countChars << endl;
    infile.close();
    return 0;
}