文本文件数据统计程序
1.代码来源:借鉴同学代码并作出调整及修改
2.运行环境:Linux
3.编程语言:C++
4.bug:未发现
5.当前功能:根据输入的文件,显示出文本文件的字符数和行数。
5.功能扩展的方向:无扩展,仅对代码进行阅读。
6.gitbub代码地址:https://github.com/selfTboke/project2
7.实现
代码如下:
/*
* count.cpp
*
* Created on: Sep 9, 2017
* Author: moon
*/
#include "count.h"
#include <fstream>
using namespace std;
count::count() {
}
count::~count() {
}
void count::setFileName(string fileName) {
this->fileName = fileName;
}
int count::computingChar(void) {
std::ifstream in(fileName);
if (!in.is_open())
return 0;
in.seekg(0, std::ios_base::end);
std::streampos sp = in.tellg();
in.close();
return sp;
}
int count::computingLine(void) {
ifstream in;
int num = 0;
string str;
in.open(fileName);
while (getline(in, str)) {
num++;
}
in.close();
return num;
}
int count::computingWord(void) {
int num = 0;
char c;
char priorC;
ifstream in;
in.open(fileName);
in.get(c);
priorC = ' ';
while (!in.eof()) {
if (c >= 'a' && c <= 'z') {
if (priorC == ' ' || priorC == '\n' || priorC == '.'
|| priorC == ',' || priorC == ':' || priorC == '?'
|| priorC == '!') {
num++;
}
}
priorC = c;
in.get(c);
}
in.close();
return num;
}
/*
* count.h
*
* Created on: Sep 9, 2017
* Author: moon
*/
#ifndef count_H_
#define count_H_
#include <string>
class count {
public:
count();
virtual ~count();
private:
std::string fileName;
public:
/**
* @function:Setting value of fileName
*/
void setFileName(std::string fileName);
/**
* @function:Counting the number of character
* @return:If success,return the number of character,else return -1
*/
int computingChar(void);
/**
* @function:Counting the number of word
* @return:If success,return the number of word,else return -1
*/
int computingWord(void);
/**
* @function:Counting the number of line
* @return:If success,return the number of line,else return -1
*/
int computingLine(void);
};
#endif /* count_H_ */