计算文本中字母的出现,然后进行整理

编程入门 行业动态 更新时间:2024-10-12 01:21:35
本文介绍了计算文本中字母的出现,然后进行整理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

你好, 我必须要做一个代码,打开一个文件,读取其内容,计算每个字母的幻影数,将其存储在表格中,然后按递减的顺序进行排序! 这是我的代码,但是假定整理我的表的那部分代码不起作用,有人可以帮我吗? :((

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. :)

更多推荐

计算文本中字母的出现,然后进行整理

本文发布于:2023-10-27 04:15:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532280.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字母   文本中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!