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

  • 相关阅读:
    AYIT609暑假集训第一周下训练题题解
    AYIT609暑假集训第一周上训练题题解
    IELTS
    CodeForces451B
    CodeForces1352C
    CodeForces474B
    提供程序-java根据纬度 经度值获取用户的地理位置
    一款java写的将汉字转为拼音的工具类(转)
    MyBatis学习4---使用MyBatis_Generator生成Dto、Dao、Mapping(转)
    MyBatis学习3---使用SqlBuilder生成SQL语句(转)
  • 原文地址:https://www.cnblogs.com/zgq0/p/7595431.html
Copyright © 2020-2023  润新知