姗姗来迟的词频统计代码 BUG 的发现

1. 此前提交的第一次代码作业总结博客

http://www.cnblogs.com/ustczwq/p/8680704.html

2. BUG 本天成,妙手偶得之

虽然代码已经提交,但总是感觉哪个地方不太对,bug 存在得过于莫名其妙。然后,随手打开代码,稍微调试了一下,当我发现 bug 的时候,不知道该说些什么好,只想讲脏话。

出现 bug 的地方:

改过之后:

看出来了吧,妈卖批,三目运算符没赋值。改完之后,输出结果立马正确。怪不得用 unordered_map 的时候哈希表的查询出问题了,我 TM 定义的哈希函数有问题。虽然迟了,但那种优化是对的,简单补一篇,算是对原博客的完善。

3. 加了几个等于号之后的源代码

 #include "io.h"
#include "math.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "unordered_map" using namespace std; #define small 2 int wordnum = ;
int charnum = ;
int linenum = ; struct wordsdata //存放单词信息
{
char words[]; //单词字符串
int number; //出现次数
wordsdata *next;
};
struct phrases
{
char *one;
char *two;
int num;
}; int wordcmp(char *str1, char *str2);
int gettop(struct wordsdata **word);
int getwords(char *path, struct wordsdata **word);
int getfiles(char *path, struct _finddata_t *fileinfo, long handle); struct phrase_cmp
{
bool operator()(const phrases &p1, const phrases &p2) const
{
return ((wordcmp(p1.one, p2.one) < ) && (wordcmp(p1.two, p2.two) < ));
} };
struct phrase_hash
{
size_t operator()(const phrases &ph) const
{
unsigned long __h = ;
int temp;
size_t i;
for (i = ; ph.one[i]; i++)
{
temp = ph.one[i];
if (temp > )
{
(temp > ) ? (temp -= ) : (temp -= );
__h += ( * __h + temp);
__h %= ;
} }
for (i = ; ph.two[i]; i++)
{
temp = ph.two[i];
if (temp > )
{
(temp > ) ? (temp -= ) : (temp -= );
__h += ( * __h + temp);
__h %= ;
}
} return size_t(__h);
} }; typedef unordered_map<phrases, int, phrase_hash, phrase_cmp> Char_Phrase;
Char_Phrase phrasemap;
struct wordsdata *fourletter[ * * * ] = {}; //按首四字母排序 int main()
{
int j = ;
long handle = ; // 用于查找的句柄
struct _finddata_t fileinfo; // 文件信息的结构体
char *path = __argv[]; getfiles(path, &fileinfo, handle); gettop(fourletter); system("pause");
return ;
} int getfiles(char *path, struct _finddata_t *fileinfo, long handle)
{
handle = _findfirst(path, fileinfo); //第一次打开父目录
if (handle == -)
return -; do
{
//printf("> %s\n", path); //显示目录名 if (fileinfo->attrib & _A_SUBDIR) //如果读取到子目录
{
if (strcmp(fileinfo->name, ".") != && strcmp(fileinfo->name, "..") != )
{
char temppath[] = ""; //记录子目录路径
long temphandle = ;
struct _finddata_t tempfileinfo;
strcpy(temppath, path);
strcat(temppath, "/*"); temphandle = _findfirst(temppath, &tempfileinfo); //第一次打开子目录
if (temphandle == -)
return -; do //对子目录所有文件递归
{
if (strcmp(tempfileinfo.name, ".") != && strcmp(tempfileinfo.name, "..") != )
{
strcpy(temppath, path);
strcat(temppath, "/");
strcat(temppath, tempfileinfo.name);
getfiles(temppath, &tempfileinfo, temphandle);
}
} while (_findnext(temphandle, &tempfileinfo) != -); _findclose(temphandle);
}//递归完毕 } //子目录读取完毕
else
getwords(path, fourletter); } while (_findnext(handle, fileinfo) != -); _findclose(handle); //关闭句柄 return ; } int getwords(char *path, struct wordsdata **word)
{
FILE *fp;
int j = ;
int cmp = ;
int num = ; //计算首四位地址
char temp = ; //读取一个字符 ACSII 码值
int length = ; char present[] = ""; //存储当前单词 char address[] = "";
struct wordsdata *q = NULL;
struct wordsdata *pre = NULL;
struct wordsdata *neword = NULL;
struct wordsdata *now = NULL;
struct wordsdata *previous = NULL;
struct phrases *newphrase = NULL; if ((fp = fopen(path, "r")) == NULL)
{
//printf("error!!! \n", path);
return ;
}
linenum++;
while (temp != -)
{
//读取字符串
temp = fgetc(fp);
if (temp > && temp < )
charnum++;
if (temp == '\n' || temp == '\r')
linenum++; while ((temp >= '' && temp <= '') || (temp >= 'a' && temp <= 'z') || (temp >= 'A' && temp <= 'Z'))
{
if (length != - && length < )
{
if (temp >= 'A') //是字母
{
present[length] = temp;
address[length] = (temp >= 'a' ? (temp - 'a') : (temp - 'A'));
length++;
}
else //不是字母
length = -;
}
else if (length >= )
{
present[length] = temp;
length++;
}
temp = fgetc(fp);
if (temp > && temp < )
charnum++;
if (temp == '\n' || temp == '\r')
linenum++;
} // end while //判断是否为单词
if (length >= )
{
wordnum++; //计算首四位代表地址
num = address[] * + address[] * + address[] * + address[]; //插入当前单词
if (word[num] == NULL)
{
word[num] = new wordsdata;
neword = new wordsdata;
neword->number = ;
neword->next = NULL;
strcpy(neword->words, present);
word[num]->next = neword;
now = neword;
}
else
{
pre = word[num];
q = pre->next;
cmp = wordcmp(q->words, present); while (cmp == small)
{
pre = q;
q = q->next;
if (q != NULL)
cmp = wordcmp(q->words, present);
else
break;
}
if (q != NULL && cmp <= )
{
now = q;
q->number++;
if (cmp == )
strcpy(q->words, present);
} else
{
neword = new wordsdata;
neword->number = ;
strcpy(neword->words, present);
pre->next = neword;
neword->next = q;
now = neword;
}
} if (previous != NULL)
{
newphrase = new phrases; newphrase->one = previous->words;
newphrase->two = now->words; unordered_map<phrases, int>::const_iterator got = phrasemap.find( *newphrase);
if (got != phrasemap.end())
{
phrasemap[*newphrase]++;
}
else
{
phrasemap.insert(pair<phrases, int>(*newphrase, ));
}
}
previous = now; //当前单词置空
for (int j = ; present[j] && j < ; j++)
present[j] = ;
}
length = ;
} fclose(fp);
return ;
} int wordcmp(char *str1, char *str2)
{
char *p1 = str1;
char *p2 = str2;
char q1 = *p1;
char q2 = *p2; if (q1 >= 'a' && q1 <= 'z')
q1 -= ; if (q2 >= 'a' && q2 <= 'z')
q2 -= ; while (q1 && q2 && q1 == q2)
{
p1++;
p2++; q1 = *p1;
q2 = *p2; if (q1 >= 'a' && q1 <= 'z')
q1 -= ; if (q2 >= 'a' && q2 <= 'z')
q2 -= ;
} while (*p1 >= '' && *p1 <= '')
p1++;
while (*p2 >= '' && *p2 <= '')
p2++; if (*p1 == && *p2 == ) //两单词等价
return strcmp(str1, str2); //等价前者字典顺序小返回-1,大返回1,完全相等返回0 if (q1 < q2) //前者小
return ; if (q1 > q2) //后者小
return ; return ;
} int gettop(struct wordsdata **word)
{
int i = , j = ;
struct wordsdata *topw[] = {};
struct phrases *toph[] = {};
struct wordsdata *w = NULL;
FILE *fp;
fp = fopen("result.txt", "w");
fprintf(fp,"characters:%d \nwords:%d \nlines:%d\n", charnum,wordnum, linenum); for (j = ; j < ; j++)
{
toph[j] = new struct phrases;
toph[j]->num = ;
topw[j] = new struct wordsdata;
topw[j]->number = ;
}
for (i = ; i < ; i++)
{
if (word[i] != NULL)
{
w = word[i]->next;
while (w != NULL)
{
topw[]->number = w->number;
topw[]->next = w;
j = ;
while (j > && topw[j]->number > topw[j - ]->number)
{
topw[] = topw[j];
topw[j] = topw[j - ];
topw[j - ] = topw[];
j--;
}
w = w->next;
}
}
}
for (j = ; j < ; j++)
{
if (topw[j]->number)
fprintf(fp,"\n%s :%d", topw[j]->next->words, topw[j]->number);
}
for (Char_Phrase::iterator it = phrasemap.begin(); it != phrasemap.end(); it++)
{
toph[]->one = it->first.one;
toph[]->two = it->first.two;
toph[]->num = it->second;
j = ;
while (j > && toph[j]->num > toph[j - ]->num)
{
toph[] = toph[j];
toph[j] = toph[j - ];
toph[j - ] = toph[];
j--;
}
}
fprintf(fp, "\n");
for (j = ; j < ; j++)
{
if (toph[j]->num)
fprintf(fp,"\n%s %s :%d", toph[j]->one, toph[j]->two, toph[j]->num);
}
fclose(fp);
return ;
}

最新文章

  1. 关于android的@TargetApi和@SuppressLint(&quot;NewApi&quot;)
  2. 结构体类型定义(C语言)
  3. Python-Tkinter几何布局管理(转)
  4. Source Insight 中使用 AStyle 代码格式工具
  5. spring+mybati java config配置引起的bean相互引用日志报警告问题
  6. java web 学习一
  7. web前端之 CSS引入第三方插件
  8. U3D navmesh寻路简单示范
  9. 【Spring】Spring学习笔记-01-入门级实例
  10. MongoDB基础教程系列--第二篇 MongoDB基本操作(一)
  11. linux PMBus总线及设备驱动分析
  12. Silverlight/WPF 系列汇总
  13. 电脑小白和ta的小白电脑——Tomcat服务器
  14. carthage和cocoapods
  15. linux中iptables配置文件及命令详解
  16. 使用glusterfs 作为 kubernetes PersistentVolume PersistentVolumeClaim 持久化仓库,高可用Rabbitmq,高可用mysql,高可用redis
  17. java使用poi生成导出Excel(新)
  18. Apollo源码阅读笔记(一)
  19. 洛谷P3724 大佬 [AH2017/HNOI2017] dp+bfs
  20. IDEA初使用:解决搜狗输入法不跟随BUG

热门文章

  1. Python基础练级攻略:day01
  2. Qt Quick编程(1)
  3. echarts常用实例
  4. PAT 1079. Total Sales of Supply Chain
  5. 洛谷 P2634 BZOJ 2152 【模板】点分治(聪聪可可)
  6. HDU 3432
  7. android 点击返回键退出程序的方法
  8. poj1426--Find The Multiple(广搜,智商题)
  9. 将Latex tex文档转换成 word文档(上)
  10. 【翻译自mos文章】Oracle GoldenGate 怎么在源头的传输进程和目的端的server/collector进程之间分配 port?