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

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

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

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

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

出现 bug 的地方:

改过之后:

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

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

  1. #include "io.h"
  2. #include "math.h"
  3. #include "stdio.h"
  4. #include "string.h"
  5. #include "stdlib.h"
  6. #include "unordered_map"
  7.  
  8. using namespace std;
  9.  
  10. #define small 2
  11.  
  12. int wordnum = ;
  13. int charnum = ;
  14. int linenum = ;
  15.  
  16. struct wordsdata //存放单词信息
  17. {
  18. char words[]; //单词字符串
  19. int number; //出现次数
  20. wordsdata *next;
  21. };
  22. struct phrases
  23. {
  24. char *one;
  25. char *two;
  26. int num;
  27. };
  28.  
  29. int wordcmp(char *str1, char *str2);
  30. int gettop(struct wordsdata **word);
  31. int getwords(char *path, struct wordsdata **word);
  32. int getfiles(char *path, struct _finddata_t *fileinfo, long handle);
  33.  
  34. struct phrase_cmp
  35. {
  36. bool operator()(const phrases &p1, const phrases &p2) const
  37. {
  38. return ((wordcmp(p1.one, p2.one) < ) && (wordcmp(p1.two, p2.two) < ));
  39. }
  40.  
  41. };
  42. struct phrase_hash
  43. {
  44. size_t operator()(const phrases &ph) const
  45. {
  46. unsigned long __h = ;
  47. int temp;
  48. size_t i;
  49. for (i = ; ph.one[i]; i++)
  50. {
  51. temp = ph.one[i];
  52. if (temp > )
  53. {
  54. (temp > ) ? (temp -= ) : (temp -= );
  55. __h += ( * __h + temp);
  56. __h %= ;
  57. }
  58.  
  59. }
  60. for (i = ; ph.two[i]; i++)
  61. {
  62. temp = ph.two[i];
  63. if (temp > )
  64. {
  65. (temp > ) ? (temp -= ) : (temp -= );
  66. __h += ( * __h + temp);
  67. __h %= ;
  68. }
  69. }
  70.  
  71. return size_t(__h);
  72. }
  73.  
  74. };
  75.  
  76. typedef unordered_map<phrases, int, phrase_hash, phrase_cmp> Char_Phrase;
  77. Char_Phrase phrasemap;
  78. struct wordsdata *fourletter[ * * * ] = {}; //按首四字母排序
  79.  
  80. int main()
  81. {
  82. int j = ;
  83. long handle = ; // 用于查找的句柄
  84. struct _finddata_t fileinfo; // 文件信息的结构体
  85. char *path = __argv[];
  86.  
  87. getfiles(path, &fileinfo, handle);
  88.  
  89. gettop(fourletter);
  90.  
  91. system("pause");
  92. return ;
  93. }
  94.  
  95. int getfiles(char *path, struct _finddata_t *fileinfo, long handle)
  96. {
  97. handle = _findfirst(path, fileinfo); //第一次打开父目录
  98. if (handle == -)
  99. return -;
  100.  
  101. do
  102. {
  103. //printf("> %s\n", path); //显示目录名
  104.  
  105. if (fileinfo->attrib & _A_SUBDIR) //如果读取到子目录
  106. {
  107. if (strcmp(fileinfo->name, ".") != && strcmp(fileinfo->name, "..") != )
  108. {
  109. char temppath[] = ""; //记录子目录路径
  110. long temphandle = ;
  111. struct _finddata_t tempfileinfo;
  112. strcpy(temppath, path);
  113. strcat(temppath, "/*");
  114.  
  115. temphandle = _findfirst(temppath, &tempfileinfo); //第一次打开子目录
  116. if (temphandle == -)
  117. return -;
  118.  
  119. do //对子目录所有文件递归
  120. {
  121. if (strcmp(tempfileinfo.name, ".") != && strcmp(tempfileinfo.name, "..") != )
  122. {
  123. strcpy(temppath, path);
  124. strcat(temppath, "/");
  125. strcat(temppath, tempfileinfo.name);
  126. getfiles(temppath, &tempfileinfo, temphandle);
  127. }
  128. } while (_findnext(temphandle, &tempfileinfo) != -);
  129.  
  130. _findclose(temphandle);
  131. }//递归完毕
  132.  
  133. } //子目录读取完毕
  134. else
  135. getwords(path, fourletter);
  136.  
  137. } while (_findnext(handle, fileinfo) != -);
  138.  
  139. _findclose(handle); //关闭句柄
  140.  
  141. return ;
  142.  
  143. }
  144.  
  145. int getwords(char *path, struct wordsdata **word)
  146. {
  147. FILE *fp;
  148. int j = ;
  149. int cmp = ;
  150. int num = ; //计算首四位地址
  151. char temp = ; //读取一个字符 ACSII 码值
  152. int length = ;
  153.  
  154. char present[] = ""; //存储当前单词
  155.  
  156. char address[] = "";
  157. struct wordsdata *q = NULL;
  158. struct wordsdata *pre = NULL;
  159. struct wordsdata *neword = NULL;
  160. struct wordsdata *now = NULL;
  161. struct wordsdata *previous = NULL;
  162. struct phrases *newphrase = NULL;
  163.  
  164. if ((fp = fopen(path, "r")) == NULL)
  165. {
  166. //printf("error!!! \n", path);
  167. return ;
  168. }
  169. linenum++;
  170. while (temp != -)
  171. {
  172. //读取字符串
  173. temp = fgetc(fp);
  174. if (temp > && temp < )
  175. charnum++;
  176. if (temp == '\n' || temp == '\r')
  177. linenum++;
  178.  
  179. while ((temp >= '' && temp <= '') || (temp >= 'a' && temp <= 'z') || (temp >= 'A' && temp <= 'Z'))
  180. {
  181. if (length != - && length < )
  182. {
  183. if (temp >= 'A') //是字母
  184. {
  185. present[length] = temp;
  186. address[length] = (temp >= 'a' ? (temp - 'a') : (temp - 'A'));
  187. length++;
  188. }
  189. else //不是字母
  190. length = -;
  191. }
  192. else if (length >= )
  193. {
  194. present[length] = temp;
  195. length++;
  196. }
  197. temp = fgetc(fp);
  198. if (temp > && temp < )
  199. charnum++;
  200. if (temp == '\n' || temp == '\r')
  201. linenum++;
  202. } // end while
  203.  
  204. //判断是否为单词
  205. if (length >= )
  206. {
  207. wordnum++;
  208.  
  209. //计算首四位代表地址
  210. num = address[] * + address[] * + address[] * + address[];
  211.  
  212. //插入当前单词
  213. if (word[num] == NULL)
  214. {
  215. word[num] = new wordsdata;
  216. neword = new wordsdata;
  217. neword->number = ;
  218. neword->next = NULL;
  219. strcpy(neword->words, present);
  220. word[num]->next = neword;
  221. now = neword;
  222. }
  223. else
  224. {
  225. pre = word[num];
  226. q = pre->next;
  227. cmp = wordcmp(q->words, present);
  228.  
  229. while (cmp == small)
  230. {
  231. pre = q;
  232. q = q->next;
  233. if (q != NULL)
  234. cmp = wordcmp(q->words, present);
  235. else
  236. break;
  237. }
  238. if (q != NULL && cmp <= )
  239. {
  240. now = q;
  241. q->number++;
  242. if (cmp == )
  243. strcpy(q->words, present);
  244. }
  245.  
  246. else
  247. {
  248. neword = new wordsdata;
  249. neword->number = ;
  250. strcpy(neword->words, present);
  251. pre->next = neword;
  252. neword->next = q;
  253. now = neword;
  254. }
  255. }
  256.  
  257. if (previous != NULL)
  258. {
  259. newphrase = new phrases;
  260.  
  261. newphrase->one = previous->words;
  262. newphrase->two = now->words;
  263.  
  264. unordered_map<phrases, int>::const_iterator got = phrasemap.find( *newphrase);
  265. if (got != phrasemap.end())
  266. {
  267. phrasemap[*newphrase]++;
  268. }
  269. else
  270. {
  271. phrasemap.insert(pair<phrases, int>(*newphrase, ));
  272. }
  273. }
  274. previous = now;
  275.  
  276. //当前单词置空
  277. for (int j = ; present[j] && j < ; j++)
  278. present[j] = ;
  279. }
  280. length = ;
  281. }
  282.  
  283. fclose(fp);
  284. return ;
  285. }
  286.  
  287. int wordcmp(char *str1, char *str2)
  288. {
  289. char *p1 = str1;
  290. char *p2 = str2;
  291. char q1 = *p1;
  292. char q2 = *p2;
  293.  
  294. if (q1 >= 'a' && q1 <= 'z')
  295. q1 -= ;
  296.  
  297. if (q2 >= 'a' && q2 <= 'z')
  298. q2 -= ;
  299.  
  300. while (q1 && q2 && q1 == q2)
  301. {
  302. p1++;
  303. p2++;
  304.  
  305. q1 = *p1;
  306. q2 = *p2;
  307.  
  308. if (q1 >= 'a' && q1 <= 'z')
  309. q1 -= ;
  310.  
  311. if (q2 >= 'a' && q2 <= 'z')
  312. q2 -= ;
  313. }
  314.  
  315. while (*p1 >= '' && *p1 <= '')
  316. p1++;
  317. while (*p2 >= '' && *p2 <= '')
  318. p2++;
  319.  
  320. if (*p1 == && *p2 == ) //两单词等价
  321. return strcmp(str1, str2); //等价前者字典顺序小返回-1,大返回1,完全相等返回0
  322.  
  323. if (q1 < q2) //前者小
  324. return ;
  325.  
  326. if (q1 > q2) //后者小
  327. return ;
  328.  
  329. return ;
  330. }
  331.  
  332. int gettop(struct wordsdata **word)
  333. {
  334. int i = , j = ;
  335. struct wordsdata *topw[] = {};
  336. struct phrases *toph[] = {};
  337. struct wordsdata *w = NULL;
  338. FILE *fp;
  339. fp = fopen("result.txt", "w");
  340. fprintf(fp,"characters:%d \nwords:%d \nlines:%d\n", charnum,wordnum, linenum);
  341.  
  342. for (j = ; j < ; j++)
  343. {
  344. toph[j] = new struct phrases;
  345. toph[j]->num = ;
  346. topw[j] = new struct wordsdata;
  347. topw[j]->number = ;
  348. }
  349. for (i = ; i < ; i++)
  350. {
  351. if (word[i] != NULL)
  352. {
  353. w = word[i]->next;
  354. while (w != NULL)
  355. {
  356. topw[]->number = w->number;
  357. topw[]->next = w;
  358. j = ;
  359. while (j > && topw[j]->number > topw[j - ]->number)
  360. {
  361. topw[] = topw[j];
  362. topw[j] = topw[j - ];
  363. topw[j - ] = topw[];
  364. j--;
  365. }
  366. w = w->next;
  367. }
  368. }
  369. }
  370. for (j = ; j < ; j++)
  371. {
  372. if (topw[j]->number)
  373. fprintf(fp,"\n%s :%d", topw[j]->next->words, topw[j]->number);
  374. }
  375. for (Char_Phrase::iterator it = phrasemap.begin(); it != phrasemap.end(); it++)
  376. {
  377. toph[]->one = it->first.one;
  378. toph[]->two = it->first.two;
  379. toph[]->num = it->second;
  380. j = ;
  381. while (j > && toph[j]->num > toph[j - ]->num)
  382. {
  383. toph[] = toph[j];
  384. toph[j] = toph[j - ];
  385. toph[j - ] = toph[];
  386. j--;
  387. }
  388. }
  389. fprintf(fp, "\n");
  390. for (j = ; j < ; j++)
  391. {
  392. if (toph[j]->num)
  393. fprintf(fp,"\n%s %s :%d", toph[j]->one, toph[j]->two, toph[j]->num);
  394. }
  395. fclose(fp);
  396. return ;
  397. }

记一个男默女泪的 BUG的更多相关文章

  1. salesforce零基础学习(一百一十五)记一个有趣的bug

    本篇参考:https://help.salesforce.com/s/articleView?language=en_US&type=1&id=000319486 page layou ...

  2. 记一个神奇的Bug

    多年以后,当Abraham凝视着一行行新时代的代码在屏幕上川流不息的时候,他会想起2019年4月17日那个不平凡夜晚,以及在那个夜晚他发现的那个不可思议的Bug. 虽然像无数个普普通通的夜晚一样,我在 ...

  3. 【bug】记一个有趣的“bug”

    产品经理在使用我们用户功能的是,需要查询一个用户,知道这个用户的id,我说支持模糊查询的. 他输入"余XX",点击查询,怎么都查不出这个用户. 我到用户表里确认,确实有这个ID的用 ...

  4. 记一个深层的bug

    1. 业务场景 产品需要每隔几天进行一次组件的更新,在自动化测试中,每隔30s检测一次更新源上的某个文件MD5值是否与本地一致,不一致代表有更新的版本,开始更新. 2. 问题出现 一个再平常不过的繁忙 ...

  5. 记一个社交APP的开发过程——基础架构选型(转自一位大哥)

    记一个社交APP的开发过程——基础架构选型 目录[-] 基本产品形态 技术选型 最近两周在忙于开发一个社交App,因为之前做过一点儿社交方面的东西,就被拉去做API后端了,一个人头一次完整的去搭这么一 ...

  6. 一个iOS6系统bug+一个iOS7系统bug

    先看实际工作中遇到的两个bug:(1)iPhone Qzone有一个导航栏背景随着页面滑动而渐变的体验,当页面滑动到一定距离时,会改变导航栏上title文本的颜色,但是有一个莫名其妙的bug,如下:

  7. FIREDAC(DELPHI10 or 10.1)提交数据给ORACLE数据库的一个不是BUG的BUG

    发现FIREDAC(DELPHI10 or 10.1)提交数据给ORACLE数据库的一个不是BUG的BUG,提交的表名大小写是敏感的. 只要有一个表名字母的大小写不匹配,ORACLE就会认为是一个不认 ...

  8. pycharm下: conda installation is not found ----一个公开的bug的解决方案

    pycharm  conda installation is not  found ----一个公开的bug的解决方案 pycharm+anaconda 是当前的主流的搭建方案,但是常出现上述问题. ...

  9. 一个神奇的bug:OOM?优雅终止线程?系统内存占用较高?

    摘要:该项目是DAYU平台的数据开发(DLF),数据开发中一个重要的功能就是ETL(数据清洗).ETL由源端到目的端,中间的业务逻辑一般由用户自己编写的SQL模板实现,velocity是其中涉及的一种 ...

随机推荐

  1. eas之控制kdtable滚动条

    //滚动条支持三种状态 自动 隐藏 显示 public static final int SCROLL_STATE_AUTO=0://自动根据数据判断是否显示或隐藏 public static fin ...

  2. Vue.js大总结

    最近回顾了一下Vue.js的基础知识,把认为重要的几个点简单的罗列了出来 vue渐进式的理解 vue可以开发很多插件,可以把很多插件组合到一起,渐进的增加vue的功能 update beforeUpd ...

  3. 国庆day2

    a[问题描述]你是能看到第一题的 friends呢.—— hja世界上没有什么比卖的这 贵弹丸三还令人绝望事了,所以便么一道题.定义

  4. js对对象的校验技巧,随时更新

    js中,字符串长度用length. 若不确定一个Map里,是否存在某个对象,则用underfind 去校验

  5. Orcale用户管理

    类 ------表 对象----行 属性----列 软件开发流程: 需求调研 需求分析 概要分析 详细分析 编码 测试 上线 维护 论坛: 1.注册和登录 2.发帖,回帖(关注,浏览数) 用户:(昵称 ...

  6. 洛谷 P3178 BZOJ 4034 [HAOI2015]树上操作

    题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个操作,分为三种:操作 1 :把某个节点 x 的点权增加 a .操作 2 :把某个节点 x 为根的子树中所有点的点权都增加 ...

  7. C# ArcgisEngine开发中,对一个图层进行过滤,只显示符合条件的要素

    转自原文 C# ArcgisEngine开发中,对一个图层进行过滤,只显示符合条件的要素 有时候,我们要对图层上的地物进行有选择性的显示,以此来满足实际的功能要求. 按下面介绍的方法可轻松实现图层属性 ...

  8. Git的基本设置

    进入虚拟机环境中:首先我们对 Git 进行用户名和邮箱进行设置,请参照下面格式,替换为你自己常用的用户名和邮箱来完成设置: $ git config --global user.name " ...

  9. 杭电(hdu)ACM 1010 Tempter of the Bone

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  10. C++开发人脸性别识别教程(12)——加入性别识别功能

    经过之前几篇博客的解说,我们已经成功搭建了MFC应用框架,并实现了主要的图像显示和人脸检測程序,在这篇博文中我们要向当中加入性别识别代码. 关于性别识别,之前已经专门拿出两篇博客的篇幅来进行解说.这里 ...