字符统计(改1)

此次改进稍微加了一个文件名和文件存储位置的输入:

会继续改进的

#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
int character=0;//字符数
int word=0;//单词数
int space=0;//空格数
char words[1000];//单词存储
int wordnum=0;//单词存储的数组位置
int Word(char *f);
int show();//显示单词
int main()
{
    int choice;//选择的输入
    FILE *fp;//文件指针
    char c; //文件字符读取存储
    char filename[20] ;
    char place[10];
    char Openname[40];
    printf("请输入文件所在位置:");
    gets(place);
    printf("请输入文件名:");
    gets(filename);
    strcat(filename, ".txt");
    strcpy(Openname,place);
    strcat(Openname,"\\");
    strcat(Openname,filename);
    fp = fopen(Openname, "r");
    if (fp == NULL)
    {
        printf("文件打开失败");
        return 0;
    }
    while (feof(fp) != 1)
    {
        c = fgetc(fp);
        if (isalnum(c) != 0 || c == '_' || c == '.')
        {
            Word(fp);
            words[wordnum] = c;
            wordnum++;
        }
        else if (c == ' ')
        {
            space++;
        }
        else
        {
            character++;
        }
    }
    printf("文件中字符,单词统计如下:\n");
    printf("单词数:%d,符号数:%d,空格数:%d\n", word, character, space);
    fclose(fp);
    printf("查看单词请按1,否则请按任意键");
    scanf_s("%d", &choice);
    if (choice == 1)
    {
        show();
    }
    else
        return 1;
}
int Word(char *f)
{
    char c;
    while (feof(f) != 1)
    {
        c = fgetc(f);
        if (isalnum(c) != 0 || c == '_' || c == '.')
        {
            character++;
            words[wordnum] = c;
            wordnum++;
        }
        else if(c==' ')
        {
            space++;
            word++;
            words[wordnum] = ' ';
            wordnum++;
            return 1;
        }
        else
        {
            word++;
            words[wordnum] = ' ';
            wordnum++;
            return 1;
        }
    }
}
int show()
{
    int a;
    for (int i = 0; i < wordnum+1; i++)
    {
        printf("%c", words[i]);
    }
    printf("谢谢使用");

}
View Code

 

posted @ 2016-04-11 20:55  会飛的虾  阅读(164)  评论(0编辑  收藏  举报