zoukankan      html  css  js  c++  java
  • word count

    WC项目要求:实现一个统计程序,它能正确统计程序文件中的字符数、单词数、行数、以及其他功能,并能够快速地处理多个文件。

    代码来源:http://www.cnblogs.com/hgcrown/p/5311155.html 

    Github 地址https://github.com/fengyulouxiao/WC/commit/dced8a000ec67828c23313dd9294f2d01ad9cb0e

    内容:

    #include "iostream"
    #include "string"
    #include "stdio.h"

    using namespace std;

    FILE *file;
    int chars = 0, words = 0, lines = 1, symbols = 0, s[500];
    char c;
    //定义全局变量

    void index()
    {
    cout << " ********************主菜单*********************" << endl;
    cout << " * *" << endl;
    cout << " * 1、显示字符数、单词数和行数 *" << endl;
    cout << " * *" << endl;
    cout << " * 2、显示符号数 *" << endl;
    cout << " * *" << endl;
    cout << " * 3、显示行数及行分类 *" << endl;
    cout << " * *" << endl;
    cout << " * 4、显示所有 *" << endl;
    cout << " * *" << endl;
    cout << " * 5、退出 *" << endl;
    cout << " * *" << endl;
    cout << " ***********************************************" << endl;
    cout << " * 请选择需要的功能:";
    }//主菜单函数

    void analyse()
    {
    while ((c = fgetc(file)) != EOF)
    {
    chars++;
    if (((c <= 'z') && (c >= 'a')) || ((c <= 'Z') && (c >= 'A')) || ((c >= '0') && (c <= '9')))
    {
    words++;
    while ((c = fgetc(file)) != EOF)
    {
    chars++;
    if (((c <= 'z') && (c >= 'a')) || ((c <= 'Z') && (c >= 'A')) || ((c >= '0') && (c <= '9')))
    {
    }
    else if (c == '\n')
    {
    s[lines] = 1;
    lines++;
    break;
    }
    else if (c == ' ')
    break;
    else
    {
    symbols++;
    break;
    }
    }
    }
    else if (c == '/')
    {
    if ((c = fgetc(file)) == '/')
    {
    symbols += 2;
    s[lines] = 2;
    fseek(file, -1L, SEEK_CUR);
    }
    }
    else if (c == '\n')
    {
    if (s[lines] != 2)
    s[lines] = 1;
    lines++;
    }
    else if (c == ' ')
    {
    }
    else
    symbols++;
    }
    if (s[lines] != 2)
    s[lines] = 1;
    }//分析所有字符数、单词数、行数及行类的函数

    int main()
    {
    char name[30], b;
    int a, i, j;
    cout << " * 请输入源文件名:";
    for (;;)
    {
    cin >> name;
    if ((file = fopen(name, "r")) != NULL)
    break;
    else
    cout << " * 文件路径错误!请重新输入源文件名:";
    }
    analyse();
    fclose(file);
    index();
    cin >> a;
    while (a != 5)
    {
    switch (a)
    {
    case 1:cout << " * 字符数:" << chars << endl;
    cout << " * 单词数:" << words << endl;
    cout << " * 行数 :" << lines << endl;
    cout << " * 按任意键返回:";
    b = getchar();
    b = getchar();
    break;
    case 2:cout << " * 符号数:" << symbols << endl;
    cout << " * 按任意键返回:";
    b = getchar();
    b = getchar();
    break;
    case 3:cout << " * 行数:" << lines << endl;
    for (i = 1; i <= lines; i++)
    {
    if (s[i] == 1)
    {
    if (i<10)
    cout << " * 第" << i << "行为 代码行" << endl;
    else if (i<100)
    cout << " * 第" << i << "行为 代码行" << endl;
    else
    cout << " * 第" << i << "行为 代码行" << endl;
    }
    else if (s[i] == 2)
    {
    if (i<10)
    cout << " * 第" << i << "行为 注释行" << endl;
    else if (i<100)
    cout << " * 第" << i << "行为 注释行" << endl;
    else
    cout << " * 第" << i << "行为 注释行" << endl;
    }
    }
    cout << " * 按任意键返回:";
    b = getchar();
    b = getchar();
    break;
    case 4:cout << " * 字符数:" << chars << endl;
    cout << " * 单词数:" << words << endl;
    cout << " * 符号数:" << symbols << endl;
    cout << " * 行数 :" << lines << endl;
    for (j = 1; j <= lines; j++)
    {
    if (s[j] == 1)
    {
    if (j<10)
    cout << " * 第" << j << "行为 代码行" << endl;
    else if (j<100)
    cout << " * 第" << j << "行为 代码行" << endl;
    else
    cout << " * 第" << j << "行为 代码行" << endl;
    }
    else if (s[j] == 2)
    {
    if (j<10)
    cout << " * 第" << j << "行为 注释行" << endl;
    else if (j<100)
    cout << " * 第" << j << "行为 注释行" << endl;
    else
    cout << " * 第" << j << "行为 注释行" << endl;
    }
    }
    cout << " * 按任意键返回:";
    b = getchar();
    b = getchar();
    break;
    }
    system("cls");
    index();
    cin >> a;
    }
    return 0;
    }//主函数

    博主是用了代码system("cls")进行清屏操作和C中的fseek(file,-1L,SEEK_CUR)代码,没有用c++中的文件操作,需要改善。

  • 相关阅读:
    linux下安装elasticsearch5.6.3
    linux下安装git
    环境安装备忘录 Zookeeper
    环境安装备忘录 JDK
    环境安装备忘录 Tomcat
    MySql 通过show status 优化数据库性能
    MySQL执行计划解读 转他人文章
    2015年12月21日 my.cnf 配置
    mysql 如何查看my.cnf的 位置
    mysql状态查看 QPS/TPS/缓存命中率查看
  • 原文地址:https://www.cnblogs.com/xc2017/p/7554038.html
Copyright © 2011-2022 走看看