wc.exe

这是一个由c语言实现的统计程序,它能正确统计程序文件的字符数、单词数、行数,以及递归处理目录下符合条件的文件和返回高级选项(代码行、空行、注释行)。

代码如下:

#include "stdio.h"

#include "string.h"

#include "stdlib.h"
 
int charcalculate=0;
 
int wordcalculate=0;
 
int linecalculate=0;
 
void calculate(char * file)
{
    FILE * fp;
    char x;
    if((fp=fopen(file,"r"))==NULL)
    {
        printf("read file failed!\n");
        exit(-1);
    }
    while(!feof(fp))
    {
        a=fgetc(fp);
        if(x!=' '&&x!='\n'&&x!='\t'&&x!=','&&x!='.'&&x!='!'&&x!=';'&&x!='=')
            charcalculate++;
        if(x==' '||x=='\n'||x=='\t'||x==','||x=='.'||x=='!'||x=='='||x==';')
            wordcalculate++;
        if(x=='\n'||x=='\t')
            linecalculate++;
    }
    linecalculate++;
    charcalculate--;         
    fclose(fp);
}
 
int main(int argc, char* argv[])             
{
    FILE *fp;
 
    calculate(argv[2]);
    while(1)
    {
        if((fp=fopen(argv[2],"r"))==NULL)
        {  
        printf("FileNull\n\n\n");
        scanf("%s%s%s",argv[0],argv[1],argv[2]);
        continue;
        }
        else if(!strcmp(argv[1],"-c"))                 
            printf("File:%sCharNum:%d\n",argv[2],charcalculate);
        else if(!strcmp(argv[1],"-w"))                  
            printf("File:%sWordNum:%d\n",argv[2],wordcalculate);
        else if(!strcmp(argv[1],"-l"))                
            printf("File:%sLineNum:%d\n",argv[2],linecalculate);
        else if(!strcmp(argv[1],"exit"))
        {
            printf("Exit!\n");
            break;
        }
        else
            printf("NullPoint\n");
        printf("\n\n");
        scanf("%s%s%s",argv[0],argv[1],argv[2]);
    }
    return 0;
     
}

posted on 2017-09-25 20:59  逗b南波万  阅读(141)  评论(0编辑  收藏  举报

导航