zoukankan      html  css  js  c++  java
  • 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 == ' ' || 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;
    }

  • 相关阅读:
    avalon ms-repeat avalon1
    抓取各个浏览器引擎关键字,,百度学术关键字
    JS中Float类型加减乘除 修复
    js jquery 权限单选 bug修改以及正确代码 购物车数量加减
    图片加载错误默认显示图片
    文本框中焦点竖杆长度控制
    Go语言中 Print,Println 和 Printf 的区别(八)
    typescript中新增的基本数据类型
    尝试自己搭一个简单的typescript运行环境
    Go语言流程控制中的break,continue和goto(七)
  • 原文地址:https://www.cnblogs.com/zgq0/p/7595431.html
Copyright © 2011-2022 走看看