如何计算两个文件中字符单词和行的总数,并将它们相加以得出总数
How to count the total number of chars words and lines in two files and add them up to come up with the total?
所以在这里我得到了计算文件中字符和单词行数的代码。现在我需要为多个文件实现相同的内容。基本上,我的代码应该读取每个文件中字符单词和行的数量,并且应该生成总共的字符单词和行(将每个文件中的字符单词和行数相加。我还需要帮助执行格式不正确的参数、找不到文件和无法识别的参数的代码。这是我的代码,我试图编译它,但它只能读取每个文件中的字符单词和字符数,而不能将总数相加。谢谢你们。任何帮助将不胜感激或建议。
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main(int argc, char*argv[])
{
if (argc<2){
cout<<"You did not enter enough arguments, Please try again, close this cd window and enter the correct filename"<<endl;
}
else{
string filename;
ifstream thefile;
int i;
for(i=1,i<=argc;(i++);){
filename=argv[i];
int charactercounter=0, linecounter=1, wordcounter=1, totalLines, totalWords,totalCharacters;
thefile.open(filename);
if(!thefile.is_open()){
cout<<"file does not exist"<<endl;
}
else if(thefile.fail()){
cout<<"arguments can't be recognized"<<endl;
}
if(thefile.is_open() && thefile.good()){
while(!thefile.eof()){
char ch;
while(ch!=EOF){
charactercounter++;
totalCharacters=totalCharacters+charactercounter;
if (ch=='n')
linecounter++;
totalLines=totalLines+linecounter;
if (ch==' ')
wordcounter++;
totalWords=totalWords+wordcounter;
ch=thefile.get();
}
}
cout<<setw(12)<<"Lines"<<' '<< linecounter;
cout<<' ';
cout<<setw(12)<<"words"<<' '<< wordcounter;
cout<<' ';
cout<<setw(12)<<"characters"<<' '<< charactercounter;
cout<<' ';
cout<<setw(12)<<"filename"<<' '<<argv[i];
cout<<' ';
cout<<setw(12)<<"totallines"<<' '<<totalLines;
cout<<' ';
cout<<setw(12)<<"totalwords"<<' '<<totalWords;
cout<<' ';
cout<<setw(12)<<"totalchars"<<' '<<totalCharacters;
cout<<' ';
thefile.close();
}
}
}
}
添加了一些注释、函数和类,使代码更易于阅读。
我还尝试保留程序中的错误。
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
// -----------------------------------------------------------------------------
bool argc_checker(const int argc, const char* const argv[]) {
// Check commandline arguments
// If no problem, return true. Otherwise, return false.
if (argc < 2) {
cout << "You did not enter enough arguments, Please try again, "
"close this cd window and enter the correct filename" << endl;
return false;
}
return true;
}
// -----------------------------------------------------------------------------
bool check_file(const ifstream& thefile) {
// Return true if the file is ok. Otherwise, return false
if (!thefile.is_open()) {
cout << "file does not exist" << endl;
return false;
}
if (thefile.fail()) {
cout << "arguments can't be recognized" << endl;
return false;
}
if (thefile.is_open() && thefile.good()) {
return true;
}
return false;
}
// -----------------------------------------------------------------------------
class Word_counter {
// Count the number of character, words, and lines by taking characters
// one by one.
public:
Word_counter() { reset(); }
void take(const char ch) {
++_n_chars;
if (ch == ' ') ++_n_words;
if (ch == 'n') ++_n_lines;
}
void reset() {
// Reset the word counter.
_n_chars = 0;
_n_words = 1;
_n_lines = 1;
}
const int n_chars() const { return _n_chars; }
const int n_words() const { return _n_words; }
const int n_lines() const { return _n_lines; }
private:
int _n_chars;
int _n_words;
int _n_lines;
};
// -----------------------------------------------------------------------------
class Total_word_counter {
// Take a word counter and add its count to total counts.
public:
Total_word_counter() { reset(); }
void take(const Word_counter& wc) {
_n_total_chars += wc.n_chars();
_n_total_lines += wc.n_lines();
_n_total_words += wc.n_words();
}
void reset() {
// Reset the total word counter
_n_total_chars;
_n_total_lines;
_n_total_words;
}
const int n_total_chars() const { return _n_total_chars; }
const int n_total_words() const { return _n_total_words; }
const int n_total_lines() const { return _n_total_lines; }
private:
int _n_total_chars;
int _n_total_words;
int _n_total_lines;
};
// -----------------------------------------------------------------------------
template <typename T>
void simple_print_table(const std::string& name, const T& thing) {
cout << setw(12) << name << ' ' << thing << ' ';
}
// -----------------------------------------------------------------------------
int main(int argc, char *argv[]) {
// check if arguments are ok. if not, exit the program.
if (!argc_checker(argc, argv)) exit(1);
// Create word counters
Word_counter wc;
Total_word_counter total_wc;
// loop over each commandline argument
for (int i = 1, i <= argc; (i++); ) {
string filename(argv[i]);
ifstream the_file(filename);
// check if file is ok. if not skip to next cycle of the for loop.
if (!check_file(the_file)) {
the_file.close();
continue;
}
// loop over each character of the_file.
char ch;
while (!the_file.eof() && (ch != EOF)) {
wc.take(ch);
ch = the_file.get(); // pass each char to the word counter
}
total_wc.take(wc); // add result of word counter to total_wc
// Print result of counting
simple_print_table("Lines", wc.n_lines());
simple_print_table("words", wc.n_words());
simple_print_table("characters", wc.n_chars());
simple_print_table("totallines", total_wc.n_total_lines());
simple_print_table("totalwords", total_wc.n_total_words());
simple_print_table("totalchars", total_wc.n_total_chars());
// Rest word counter
wc.reset(); // reset the wc so it is ready to count next file
// Close the file
the_file.close();
}
}
// -----------------------------------------------------------------------------
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 递归函数计算序列中的平方和(并输出过程)
- (C++)分析树以计算返回错误值的简单算术表达式
- 我的字符计数代码计算错误.为什么
- 在计算中使用二的幂有多有利可图
- 如何计算文件中的"columns"数?
- 计算排序向量的向量中唯一值的计数
- 如何使用 std::累积在 C++ 中计算总和立方体
- 使用Qt C++计算类似Git的SHA1哈希
- OpenCV C++.快速计算混淆矩阵
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- C++如何计算用户输入的数字中的偶数位数
- 如何计算数据类型的范围,例如int
- 类似枚举的计算常量
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 计算缩放多边形的比例,得到给定的多边形面积
- 在C++中如何在没有pow的情况下进行基础计算
- 允许预先计算和计算的结果
- Matlab 计算与'C/C++'计算..哪一个是有效的?