• 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;
    }

  • 相关阅读:
    十四、内存泄露和强软弱虚引用
    十五、对象的内存布局
    Android Service全解(三)之 Foreground Service(转)
    android中不同activity的传参调用和返回
    Android Service全解(一)之 startService(转)
    Android Service全解(二)之 bindService(转)
    android单点、多点触控之MotionEvent
    关于创建进程函数CreateProcess()字符串参数的说明
    sql中连接两个不同的数据库(A在同一个服务器,B不在一个服务器)
    asp.net小数点四舍五入的方法
  • 原文地址:https://www.cnblogs.com/zgq0/p/7595431.html
Copyright © 2020-2023  润新知