linux下wc功能的简单实现

1.代码来源:自己编写

2.运行环境:linux终端

3.编程语言:c/c++语言

4.bug:未发现

5.当前功能:可以统计字符的字符数、行数、单词数

6.使用方法:wc -l 文件名-->统计行数、wc -w 文件名-->统计但词数、wc -c 文件名-->统计字符数

7.gitbub代码地址:https://github.com/moonzhu/wc

8.实现:

/*
* WC.h
*
* Created on: Sep 9, 2017
* Author: moon
*/

#ifndef WC_H_
#define WC_H_
#include <string>

class WC {
public:
WC();
virtual ~WC();
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 /* WC_H_ */

 

/*
* WC.cpp
*
* Created on: Sep 9, 2017
* Author: moon
*/

#include "WC.h"
#include <fstream>
using namespace std;

WC::WC() {
}

WC::~WC() {
}

void WC::setFileName(string fileName) {
this->fileName = fileName;
}

int WC::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 WC::computingLine(void) {
ifstream in;
int num = 0;
string str;

in.open(fileName);
while (getline(in, str)) {
num++;
}
in.close();
return num;
}

int WC::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;
}

 

//============================================================================
// Name : wc.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include "WC.h"
#include <string.h>
using namespace std;

int main(int argc, char **argv) {
WC wc;

if (argc < 3)
return -1;
string fileName(argv[2]);
wc.setFileName(fileName);
if (strcmp(argv[1], "-c") == 0) {
cout << wc.computingChar() << endl;
} else if (strcmp(argv[1], "-w") == 0) {
cout << wc.computingWord() << endl;
} else if (strcmp(argv[1], "-l") == 0) {
cout << wc.computingLine() << endl;
} else if (strcmp(argv[1], "-s") == 0) {

}

return 0;
}

转载于:https://www.cnblogs.com/zgq0/p/7595431.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值