代码改变世界

第三次作业

2016-03-16 18:38  梁开宝  阅读(228)  评论(2编辑  收藏  举报

1:实现一个控制台程序,给定一段英文字符串,统计其中各个英文单词(4字符以上含4字符)的出现频率。 附加要求:读入一段文本文件,统计该文本文件中单词的频率。

源程序:

#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;

struct Num{
    int num = 1;
    char *s = NULL;
};

Num word[99];

int Judge(char b[])//判断单词
{
    bool j1 = (strlen(b) >= 4);
    bool j2 = (b[0] >= a‘ && b[0] <= z);
    if (!(j1&&j2))
        return -1;
    else
    for (int ns = 1; b[ns] != \0;)
    {
        if (!((b[ns] >= a‘ && b[ns] <= z‘) || (b[ns] >= 0‘ && b[ns] <= 9)))
            return 1;
        else
            ns++;
    }
}
int Sta(char a[], int n)   //判断该单词是否出现过
{
    if (n>0)
    for (int i = 0; i <n; i++)
    {
        if (!strcmp(a, word[i].s))  //出现
        {
            word[i].num++;      //+1
            return 1;
        }
    }
}
int main()
{
    char data[300];
    ifstream file("e://data.txt");
    if (!file){
        cout << "Unable to open ";
        exit(1);
    }
    while (!file.eof())
    {
        file.getline(data, 300);
    }
    file.close();
    int x = 0;
    while (data[x] != \0‘)                      //转小写
    {
        if (data[x] >= A‘&&data[x] <= Z)
            data[x] = data[x] + 32;
        x++;
    }
    const char *delim = " ,.‘‘’";              //将非字母数字作为分隔符
    char *p,
        *pNext = NULL;
    int n = 0,                                 //计数器 n ,t 初始化
        t = 0;
    p = strtok_s(data, delim, &pNext);          //strtovk_s函数根据分隔符分隔字符串
    while (p)
    {
        if (Judge(p) != 1)
        {
            if (Sta(p, n) != 1)
            {
                word[n].s = p;
                n++;
            }
        }
        p = strtok_s(NULL, delim, &pNext);
    }
    while (word[t].s)        //输出统计结果
    {
        cout << word[t].s << "" << word[t].num << \n;
        t++;
    }
    return 0;
}

输入:Word is case insensitive, i.e. “file”, “FILE” and “File” are considered the same word.

结果:

 file:3

word:2

 case:2

insensitive:1

same:1

这次作业用的是C++的方法,JAVA并没有学会,还有的困难的是C++的软件在电脑运行的不行,原先我估计花一天的时间这样写好,但是并没有那快,基础差,没法子,也是经过多次运行和请教舍友才成功。