本文介绍了计算文本中字母的出现,然后进行整理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好, 我必须要做一个代码,打开一个文件,读取其内容,计算每个字母的幻影数,将其存储在表格中,然后按递减的顺序进行排序! 这是我的代码,但是假定整理我的表的那部分代码不起作用,有人可以帮我吗? :((
hello, I have to do a code which open a file read its content, calculate the number of apparition of each letter, store it in a table, then sort it out decreasing! Here are my code, but the chunk of code which suppose sort out my table doesn''t work, can anyone help me please? :((
#include<stdio.h> #include<stdlib.h> #include<conio.h> #include <string.h> int main(void) { FILE* fichier = NULL; int i,j; int *occ = NULL; int taille=128; char chaine[256] = ""; fichier = fopen("C:\\Users\\xx\\yy\\doc.txt", "r");::the path of the file occ = (int*)calloc(129, sizeof(int)); if (fichier != NULL) { while (fgets(chaine,sizeof chaine , fichier) != NULL) { printf("%s\n", chaine); for(i=0; i<strlen(chaine) ; i++) { occ[chaine[i]]++; } printf("display the occurences :\n"); for(i = 0; i <= 256; i++) { if(occ[i]) { printf("%c => %d\n", i, occ[i]); } } for(i=0;i<strlen(chaine); i++) { printf("%c = %d -> %d \n", chaine[i],chaine[i], occ[chaine[i]]); } //sort out the table for(i=0;i<256;i++) { for (j=0;j<256;j++) { if(occ[i]>occ[i+1]) { int k = occ[i]; occ[i]=occ[i+1]; occ[i+1]=k; } } printf("%d\n",occ[i]); } } } getch(); fclose(fichier); return 0; }推荐答案
您的代码似乎正在使用所谓的冒泡排序".这是人类已知的最慢的排序形式. :) 如果无法使其正常运行,则可以考虑更改为使用其他算法,例如''快速排序'' [ ^ ],顾名思义就是相当快. 祝你好运. :) Your code appears to be using what is known as a ''Bubble Sort''. This is the slowest form of sorting known to mankind. :) If you are unable to get it working properly you might consider changing to use another algorithm, such as the ''Quick Sort''[^], which as it''s name suggests is quite quick. Good luck. :)
更多推荐
计算文本中字母的出现,然后进行整理
发布评论