• 字符统计(改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("文件中字符,单词统计如下:
    ");
        printf("单词数:%d,符号数:%d,空格数:%d
    ", 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
  • 相关阅读:
    无监督聚类K-means算法
    Python程序执行顺序
    修改jupyter notebook响应的浏览器
    Vijos1035 贪婪的送礼者 [map的应用]
    POJ 2976 Dropping tests [二分]
    POJ 3111 K Best 最大化平均值 [二分]
    HDU 2899 Strange fuction [二分]
    HDU 2141 can you find it [二分]
    HDU 4004 The Frog's Games [二分]
    HDU 1969 Pie [二分]
  • 原文地址:https://www.cnblogs.com/shenpfei/p/5380123.html
Copyright © 2020-2023  润新知