C++程序需要行单词和字符计数
C++ program need lines words and characters count
>我有一个作业,我需要计算文件中的行、单词和字符。我在计算正确数量的字符和单词时遇到了问题,因为如果它获得双倍的空间,它就会像字符和单词一样计数。
输出应为
例
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提出这个问题的目的是找出他/她的代码不起作用的原因,因此我将从这个角度回答。
计算正确的字数
- C++将EOF(文件结尾(定义为-1,因此也要检查EOF,否则您将错过字数统计。
如果它得到双倍的空间,它就像一个字符和一个单词。
您可以使用布尔测试来解决此问题,如果您遇到空格,请打开布尔值,如果下一个字符也是空格,则跳过。
我想你的字符数不计入标点符号? 所以检查
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;
}
相关文章:
- 当您在此单词中搜索单词时调整字符数组的大小?
- 反转字符串中单词的位置,而不更改 O(1) 空格限制中特殊字符的顺序
- C ++程序在字符数组中插入单词
- C++ - 使用结构数据类型将单词中的单个小写字符更改为大写,反之亦然
- 延迟逐个字符打印单词
- 创建单词对齐的字符向量
- 在字符数组中存储和恢复一组单词
- 如何用单词排列向量中的每个字符升序?
- 如何在字符指针数组中保留指向每个单词的指针?
- 当找到来自 RHS 的字符"\"时,从主字符串中获取单词或子字符串,然后擦除其余部分
- 视觉C++查找以 "ing" 结尾且少于或等于 10 个字符的单词
- 需要将单词的文本文件存储到C 中的字符阵列中
- 从字符数组中删除单词
- 忽略给定单词之前的所有字符
- 如何计算C++字符数组中的单词
- 如何用新单词替换未知数量的相同字符
- (C++) 打印字符数组字符串的单词时出现问题
- 计数句子中单词的字符数
- 如何计算两个文件中字符单词和行的总数,并将它们相加以得出总数
- 使用getline,c 中的数组以相反的方式将所有字符单词转动