Advertisement
Guest User

Untitled

a guest
May 28th, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. void sortsel (char **strings, int cnt, int sel);
  6.  
  7. int main (int argc, char** argv)
  8. {
  9. int cnt, sel, a, b;
  10. char **strings;
  11.  
  12. FILE *fin = fopen("strings.txt","r");
  13. //кол-во строк
  14. while(!feof(fin) && !ferror(fin))
  15. {
  16. fscanf(fin, "%*[^\n]%*c");
  17. cnt++;
  18. }
  19. rewind(fin);
  20.  
  21. if (!(strings=(char **)malloc(cnt*sizeof(char *))))
  22. {
  23. puts("Не удалось выделить память для записи строк.");
  24. exit(1);
  25. }
  26. else
  27. for (a=0; a<cnt; a++)
  28. if (!(strings[a]=(char *)malloc(200*sizeof(char))))
  29. {
  30. puts("Не удалось выделить память для записи строк.");
  31. exit(1);
  32. }
  33.  
  34. printf("Строки исходного файла:\n------------------------------------\n");
  35. for (b=0; b<cnt; b++)
  36. {
  37. fgets(strings[b], 200*sizeof(char), fin);
  38. printf("%s", strings[b]);
  39. }
  40.  
  41. printf("\nВведите количество строк для выборки: ");
  42. do {
  43. scanf("%d", &sel);
  44. } while (sel<=0);
  45. if (sel<=cnt)
  46. sortsel(strings, cnt, sel);
  47. else
  48. {
  49. printf("Недостаточно строк для выборки!\nВыборка из %d строк: ", cnt);
  50. sortsel(strings, cnt, cnt);
  51. }
  52. }
  53.  
  54. void sortsel (char **strings, int cnt, int sel)
  55. {
  56. system("clear");
  57. int a, b, c;
  58. int swap;
  59. char t[200];
  60. for(a=0;a<cnt-1;++a)
  61. {
  62. swap=0;
  63. c=a;
  64. strcpy(t, strings[a]);
  65. for(b=a+1;b<cnt;++b)
  66. {
  67. if(strlen(strings[b])<strlen(t))
  68. {
  69. c=b;
  70. strcpy(t, strings[b]);
  71. swap=1;
  72. }
  73. }
  74. if(swap)
  75. {
  76. strcpy(strings[c], strings[a]);
  77. strcpy(strings[a], t);
  78. }
  79. }
  80. printf("Последняя строка в выборке: %d\n%s", sel, strings[sel]);
  81. }
  82. } 
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement