zoukankan      html  css  js  c++  java
  • 文件词数统计

    好久没写代码了。

    拿起了我熟悉的C++,参考别人代码的情况下还整了半多小时。

    #include <iostream>
    #include <cstdio>
    #include <fstream>
    #include <string>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cstring>
    #include <io.h>
    using namespace std;
    void console_get_name();
    void display_map(map<string,int> &wmap);
    void file_get_map(string Arg);
    void file_txt_get_map(char* A);
    void console_get_single();
    void file_info_get_map(char* fileinfo);
    void one_backslash_become_two_backslash(char* Path);
    int main(int argc ,char** argv)
    {
        if(argc==1)
            console_get_single();
        if(argc==3)
        {
            string arg=argv[2];
            file_get_map(arg);
        }
        char a[200]="",b[20]="*.txt",c[20]="\",d[20]="-s";
        if(strcmp(d,argv[1])==0)
        {
            console_get_name();
            return 0;
        }
        char buf[80];
        getcwd(buf,sizeof(buf));
        strcat(a,buf);
        strcat(a,c);
        strcat(a,argv[1]);
        strcat(a,c);
        strcat(a,b);
    //printf("%s
    ",argv[0]);
        one_backslash_become_two_backslash(a);
    // strcat(a,b);
    //printf("%s
    ",a);
        long Handle;
        struct _finddata_t FileInfo;
        if((Handle=_findfirst(a,&FileInfo))==-1L)
        {
            char aa[200];
            strcpy(aa,argv[1]);
            file_txt_get_map(aa);
        }
        else
        {
    
            file_info_get_map(FileInfo.name);
            while(_findnext(Handle,&FileInfo)==0)
                file_info_get_map(FileInfo.name);
            _findclose(Handle);
        }
    
    // get_map();
    
        return 0;
    }
    
    void display_map(map<string,int> &wmap)
    {
        map<string,int>::const_iterator map_it;
        int tot=0;
        for(map_it=wmap.begin(); map_it != wmap.end(); map_it ++)
            tot++;
        cout<<"total "<<tot<<endl;
        for(map_it=wmap.begin(); map_it != wmap.end(); map_it ++)
        {
            cout << map_it->first <<" "<< map_it->second << endl;
        }
    
    
    }
    void file_txt_get_map(char* A)
    {
        string filename;
        filename=A;
    //cin.get();
        filename+=".txt";
        ifstream fin(filename.c_str()); // read in str[]
        string temp;
        map<string,int> wmap;
        while(fin>>temp)
        {
            int len=temp.size();
            char tmp[20]="";
            int j=0;
            for(int i=0; i<len; i++)
            {
                if (isalpha(temp[i]))
                {
                    tmp[j]=temp[i];
                    j++;
                }
    
            }
            string tmp2(tmp);
            wmap[tmp2]++;
        }
        display_map(wmap);
        fin.close();
        cin.get();
    }
    void file_get_map(string Arg)
    {
        string filename;
        filename=Arg;
    // cin.get();
        ifstream fin(filename.c_str()); // read in str[]
        string temp;
        map<string,int> wmap;
        while(fin>>temp)
        {
            int len=temp.size();
            char tmp[20]="";
            int j=0;
            for(int i=0; i<len; i++)
            {
                if (isalpha(temp[i]))
                {
                    tmp[j]=temp[i];
                    j++;
                }
    
            }
            string tmp2(tmp);
            wmap[tmp2]++;
        }
        display_map(wmap);
        fin.close();
        cin.get();
    }
    void console_get_single()
    {
        string temp;
        map<string,int> wmap;
        while(cin>>temp)
        {
    
            int len=temp.size();
            char tmp[20]="";
            int j=0;
            for(int i=0; i<len; i++)
            {
                if (isalpha(temp[i]))
                {
                    tmp[j]=temp[i];
                    j++;
                }
    
            }
            string tmp2(tmp);
            wmap[tmp2]++;
            if (getchar()=='
    ')
                break;
        }
        display_map(wmap);
        cin.get();
    }
    void console_get_name()
    {
        string filename;
        cin>>filename;
    //cin.get();
        filename+=".txt";
        ifstream fin(filename.c_str()); // read in str[]
        string temp;
        map<string,int> wmap;
        while(fin>>temp)
        {
            int len=temp.size();
            char tmp[20]="";
            int j=0;
            for(int i=0; i<len; i++)
            {
                if (isalpha(temp[i]))
                {
                    tmp[j]=temp[i];
                    j++;
                }
    
            }
            string tmp2(tmp);
            wmap[tmp2]++;
        }
        display_map(wmap);
        fin.close();
        cin.get();
    }
    void one_backslash_become_two_backslash(char* Path)
    {
        int len=strlen(Path);
        char Path2[100];
        for(int i=0,j=0; i<len; i++)
        {
            if(Path[i]=='\')
            {
                Path2[j++]='\';
                Path2[j++]='\';
            }
            else Path2[j++]=Path[i];
        }
        strcpy(Path2,Path);
    }
    void file_info_get_map(char* fileinfo)
    {
        string filename=fileinfo;
    // cin >> filename;
    // cin.get();
        ifstream fin(filename.c_str()); // read in str[]
        string temp;
        map<string,int> wmap;
        while(fin>>temp)
        {
            int len=temp.size();
            char tmp[20]="";
            int j=0;
            for(int i=0; i<len; i++)
            {
                if (isalpha(temp[i]))
                {
                    tmp[j]=temp[i];
                    j++;
                }
    
            }
            string tmp2(tmp);
            wmap[tmp2]++;
        }
        display_map(wmap);
        fin.close();
    // cin.get();
    }

    读入文件用fstream

    首先处理单词,使用ispunct,把标点去掉。

    然后我们怎么存。因为要统计个数,本能的想到使用map(哈希也是可以的)。(然后去百度了个map的代码改了改)map默认按key升序排序,由于题目没要求所以我没弄成降序。

    输出格式控制也懒得弄了。

    读入进来然后用迭代器遍历一下就可以输出了。

    本地测试成功。

    这里五个题算法上基本上就用这个就可以了,输入输出如何控制是接下来的比较重点的问题。

    另外我测试的时候是先打开这个wf.exe,然后在里面输入我测试的文件名test.txt。不知道怎样才能实现成wf -s test.txt的效果,还需要再学习一个。

    修改了一些代码,可以算total了。

  • 相关阅读:
    IIR数字滤波器的实现(C语言)
    V-rep学习笔记:视觉传感器2
    V-rep学习笔记:视觉传感器1
    使用Kinect2.0控制VREP中的虚拟模型
    四元数插值与均值(姿态平滑)
    Kinect2.0获取关节姿态(Joint Orientation)
    V-rep学习笔记:Reflexxes Motion Library 2
    Kinect2.0骨骼跟踪与数据平滑
    四元数与欧拉角(RPY角)的相互转换
    Kinect2.0关节角度获取
  • 原文地址:https://www.cnblogs.com/gaoyb348/p/7528270.html
Copyright © 2011-2022 走看看