wc項目
文本文件數據統計程序
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_ */
wc項目