zoukankan      html  css  js  c++  java
  • word count

    我引用的网上的代码,代码没有做太大改动,程序本身的注释再加上我注释了一点。

    本篇GitHub地址:https://github.com/WQY0903/class02/tree/master

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<windows.h>
    #define IN 1
    #define OUT 0
    void main() {
    FILE *fp;
    int length;
    fp = fopen("wang.txt", "r");//打开要计数的文件
    if (fp == NULL)
    {
    printf("can not open file");
    exit(0);
    }
    fseek(fp, 00, SEEK_END);
    length = ftell(fp);
    printf("%d ", length);//确定所要查找的文件中总字符数长度,并在屏幕中显示出来
    rewind(fp);
    char str[100000], c;
    fread(str, sizeof(char), length, fp);//从文件中读取所有的字符到str序列
    int i, num1 = 0, num2 = 0, num3, num4=0, word = OUT;
    for (i = 0; (c = str[i]) != ''; i++)
    {
    if (c == ' ')//判断字符中单词数
    {
    num2++;
    word = OUT;
    }
    else
    {
    if (word == OUT)
    {
    word = IN;
    num1++;
    }
    }
    if ((c = str[i]) == ' ')
    {
    num4++;
    }
    }
    num3 = length - num2-num4*2;//字符数
    num4++;//行数
    fclose(fp);
    printf("空格数为%d ", num2);
    printf("单词数为%d 行数为%d 字符数为%d ", num1, num4, num3);
    system("pause");

    }

    该程序中主要部分已经做了一些相关注释。WC程序,该算法的程序流程大致是选定txt文件,然后读取其中的字符数加入到其中的一个数组中;然后在数组中根据for循环进行行数,字符数,单词数的计数。

  • 相关阅读:
    es6里面的arr方法
    for循环比较
    window.location各属性的值
    浏览器的缓存机制
    es6中的双箭头函数
    前端开发模拟数据------webpack-api-mocker
    对象的深拷贝和浅拷贝
    Web应用程序的安全问题
    this指向问题
    postman使用篇最全整理
  • 原文地址:https://www.cnblogs.com/WQY0903/p/7610593.html
Copyright © 2011-2022 走看看