码迷,mamicode.com
首页 > 其他好文 > 详细

wordCount

时间:2017-09-27 10:22:02      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:get   htm   std   note   argc   mui   综合   eclipse   net   

code来源:http://www.cnblogs.com/visionming/p/5303107.html

平台:windows8.1

语言:java 

运行环境:eclipse

有无bug:无

具体功能描述:统计文本的字符数、单词数和行数。

gethub:https://my.oschina.net/u/3698308/blog/1543894

#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

void CharCount();  //字符统计函数
void WordCount();  //单词统计函数
void LineCount();  //行数统计函数
void Muiltiple();  //综合统计函数,包括代码行,空行,注释行

int main(int argc,char *argv[])
{
    if ((strcmp(argv[1], "-c") == 0) && (strcmp(argv[2], "file.c") == 0))
    {
        CharCount();
    }
    
    if ((strcmp(argv[1], "-w") == 0) && (strcmp(argv[2], "file.c") == 0))
    
        WordCount();
    }
    if ((strcmp(argv[1], "-l") == 0) && (strcmp(argv[2], "file.c") == 0))
    {
        LineCount();
    }
    if ((strcmp(argv[1], "-a") == 0) && (strcmp(argv[2], "file.c") == 0))
    {
        Muiltiple();
    }
    return 0;


}

void CharCount() //字符统计函数
{
    FILE *fp;
    int c = 0;
    char ch;
    if((fp = fopen("file.c","r")) == NULL)
    {
        printf("file read failure.");
    }
    ch = fgetc(fp);
    while(ch != EOF)
    {
            c ++;
            ch = fgetc(fp);
    }
    printf("char count is :%d.\n",c);
    fclose(fp);
}

void WordCount() //单词统计函数
{
    FILE *fp;
    int w = 0;
    char ch;
    if((fp = fopen("file.c","r")) == NULL)
    {
        printf("file read failure.");
    }
    ch = fgetc(fp);
    while(ch != EOF)
    {
        if ((ch >= ‘a‘&&ch <= ‘z‘)||(ch >= ‘A‘&&ch <=‘Z‘)||(ch >= ‘0‘&&ch <= ‘9‘))
        {
            while ((ch >= ‘a‘&&ch <= ‘z‘)||(ch >= ‘A‘&&ch <= ‘Z‘)||(ch >= ‘0‘&&ch <= ‘9‘)||ch == ‘_‘)
            {
                ch = fgetc(fp);
            }
            w ++;
            ch = fgetc(fp);    
        }
        else 
        {
            ch = fgetc(fp);
        }
    }
    printf("word count is :%d.\n",w);
    fclose(fp);

}

void LineCount() //行数统计函数
{
    FILE *fp;
    int l = 1;
    char ch;
    if((fp = fopen("file.c","r")) == NULL)
    {
        printf("file read failure.");
    }
    ch = fgetc(fp);
    while(ch != EOF)
    {
        if (ch == ‘\n‘)
        {
            l ++;
            ch = fgetc(fp);
        }
        else
        {
            ch = fgetc(fp);
        }
    }
    printf("line count is :%d.\n",l);
    fclose(fp);
}

void Muiltiple()  //综合统计函数,包括代码行,空行,注释行
{
    FILE *fp;
    char ch;
    int c=0,e=0,n=0;
    if((fp = fopen("file.c","r")) == NULL)
    {
        printf("file read failure.");
    }
    ch = fgetc(fp);
    while(ch != EOF)
    {
        if (ch == ‘{‘||ch == ‘}‘)
        {
            e ++;
            ch = fgetc(fp);
        }
        else if (ch == ‘\n‘)
        {
            ch = fgetc(fp);
            while(ch == ‘\n‘)
            {
                e ++;
                ch = fgetc(fp);
            }
        }
        else if (ch == ‘/‘)
        {
            ch = fgetc(fp);
            if (ch == ‘/‘)
                while(ch != ‘\n‘)
                {
                    ch = fgetc(fp);
                }
                n ++;
                ch = fgetc(fp);
        }
        else
        {
            c ++;
            while (ch != ‘{‘&&ch != ‘}‘&&ch != ‘\n‘&&ch != ‘/‘&&ch != EOF)
            {
                ch = fgetc(fp);
            }
        }

    }
    printf("code line count is :%d.\n",c);
    printf("empt line count is :%d.\n",e);
    printf("note line count is :%d.\n",n);
    fclose(fp);
}

wordCount

标签:get   htm   std   note   argc   mui   综合   eclipse   net   

原文地址:http://www.cnblogs.com/lsdbk/p/7599672.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!