Guest User

Untitled

a guest
Nov 27th, 2014
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <locale.h>
  4.  
  5. #define SIZE 40
  6. #define STANDART_LENGHT_LINE 255
  7.  
  8. int countRecurringCharacters(char *line);
  9.  
  10. int funcmp(const void *x1, const void* x2);
  11.  
  12. struct list {
  13. int numberLine;
  14. int repeatCount;
  15. };
  16.  
  17. int main(void) {
  18. struct list list[40];
  19. FILE *file;
  20. char *fileName = malloc(20 * sizeof(char));
  21. char *lineOperating = malloc(255 * sizeof(char));
  22. register int i = 0;
  23. register int count = 0;
  24.  
  25. printf("Введите имя файла: ");
  26. scanf("%20s", fileName);
  27.  
  28. if ((file = fopen(fileName, "r")) == NULL) {
  29. printf("Возникла ошибка при открытие файла!\n");
  30. exit(1);
  31. }
  32.  
  33. while (fscanf(file, "%254s", lineOperating) != EOF) {
  34. list[count].numberLine = count + 1;
  35. list[count].repeatCount = countRecurringCharacters(lineOperating);
  36. count++;
  37. }
  38.  
  39. printf("Исходная структура :\n");
  40. for (i = 0; i < count; i++) {
  41. printf("%d) %d\n", list[i].numberLine, list[i].repeatCount);
  42. }
  43.  
  44. printf("\n");
  45. qsort(list, count, sizeof(struct list), funcmp);
  46.  
  47. printf("Отсортированная структура :\n");
  48. for (i = 0; i < count; i++) {
  49. printf("%d) %d\n", list[i].numberLine, list[i].repeatCount);
  50. }
  51.  
  52. fclose(file);
  53. return 0;
  54. }
  55.  
  56. int countRecurringCharacters(char *line) {
  57. int count = 0;
  58. char countRepeat[256];
  59. register int i;
  60.  
  61. for(int i = 0; i < 256; i++) {
  62. countRepeat[i] = 0;
  63. }
  64.  
  65. while (line) {
  66. countRepeat[*line]++;
  67. line++;
  68. }
  69.  
  70. for (i = 0; i < 256; i++) {
  71. if (countRepeat[i] > 1) {
  72. count++;
  73. }
  74. }
  75.  
  76. return count;
  77. }
  78.  
  79. int funcmp(const void *x1, const void* x2) {
  80. struct list *s1 = (struct list*)x1;
  81. struct list *s2 = (struct list*)x2;
  82.  
  83. return s1->repeatCount - s2->repeatCount;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment