• WC项目(补发)


    用C语言实现wc命令,统计文件的行数,字符数,词组数。楼主发的代码似乎并不是完整代码,但这些基本就是wc项目需求的。

    首先是添加单词,下面是代码://先记录单词,在运用指针指向他

    void addWord(char *w1) //添加单词
    {

    link *p1,*p2;
    //if(w1[0] <= 'Z' && w1[0] >= 'A') //转成小写字母
    //{
    // w1[0]+=32;
    //}
    for(p1=head;p1!=NULL;p1=p1->next) //判断单词在连表中是否存在
    {
    if(!strcmp(p1->w,w1))
    {
    p1->count++; //存在就个数加1
    return;
    }
    }

    p1=(struct word *)malloc(sizeof(word));//不存在添加新单词
    strcpy(p1->w,w1);
    p1->count=1;
    p1->next=NULL;
    count++; //总的单词数加加

    if(head==NULL)
    {
    head=p1;
    }
    else
    {
    for(p2=head;p2->next!=NULL;p2=p2->next);
    p2->next=p1;
    }
    }

    然后判断指向的是是不是字符或是其他
    int isnotWord(char a) //判断是否为字母
    {
    if(a <= 'z' && a >= 'a')
    {
    littleletter++;
    return 0;
    }
    else if(a <= 'Z' && a >= 'A')
    {
    bigletter++;
    return 0;
    }
    else if(a==' ')
    {
    space++;
    return 1;
    }
    else if(a>='1'&&a<='9')
    {
    number++;
    return 1;
    }
    else if(a==' ')
    {
    hang_num++;
    return 1;
    }
    else
    {
    other++;
    return 1;
    }

    }
    统计单词个数:
    #include <iostream>
    #include<cstdio>
    #include<cstdlib>
    using namespace std;
    //统计单词的个数
    int main()
    {
    char a;
    int count=0;
    while((a=getchar())!=' ')
    {
    if(a==' ')
    count++;
    }
    cout << count+1 << endl;
    return 0;
    }
    代码链接网址:https://github.com/TalosLin/QTL

  • 相关阅读:
    PE文件解析器的编写(二)——PE文件头的解析
    PE解析器的编写(一)——总体说明
    PE文件详解(九)
    PE文件详解(八)
    06_建造者模式
    05_模板方法模式
    04_抽象工厂模式
    03_简单工厂模式(静态工厂模式)
    02_工厂方法模式
    01_单例模式
  • 原文地址:https://www.cnblogs.com/qian8949695/p/8179546.html
Copyright © 2020-2023  润新知