fork download
  1. #include <iostream>
  2. #include <strstream>
  3. #include <fstream>
  4. #include <cctype>
  5. #include <cstring>
  6. #include <malloc.h>
  7. #define BLOCK 64
  8.  
  9.  
  10. //Пузырьковая сортировка
  11. template<typename T>
  12. void bsort(T* f, T* l){
  13. bool lp = true;
  14. T* e = l - 1;
  15.  
  16. for(; (f != l) && lp; ++f){
  17. lp = false;
  18. for(T* p = e; p > f; --p){
  19. if(*p < *(p - 1)){
  20. std::swap(*p, *(p - 1));
  21. lp = true;
  22. }
  23. }
  24. }
  25. }
  26.  
  27.  
  28. //структура содержит указатель на слово, длинну, кол-во гласных
  29. struct info {
  30. const char* p;
  31. unsigned short n, v;
  32.  
  33. bool operator < (const info& inf) const {
  34. return (v < inf.v);
  35. }
  36.  
  37. static bool _alloc(info** arr, size_t n, size_t& m){
  38. if(*arr == NULL){
  39. *arr = (info*)malloc(BLOCK * sizeof(info));
  40. if(*arr != NULL)
  41. m = BLOCK;
  42. } else if(n >= m){
  43. info* p = (info*)realloc(*arr, (m + BLOCK) * sizeof(info));
  44. if(p == NULL)
  45. return false;
  46. m = m + BLOCK;
  47. *arr = p;
  48. }
  49. return (*arr != NULL);
  50. }
  51. };
  52.  
  53.  
  54. void output_ws(std::ostream& _out, std::istream& _in){
  55. char buf[256];
  56. std::string s;
  57.  
  58. //читаем входной поток в строку
  59. while(! _in.eof() && ! _in.fail()){
  60. _in.read(buf, sizeof(buf)-1);
  61. if(_in.gcount() > 0)
  62. s.append(buf, buf + _in.gcount());
  63. }
  64.  
  65. size_t cnt = 0, mem = 0;
  66. info* arr = NULL;
  67.  
  68. unsigned short v, n;
  69. const char* i, *p = s.c_str();
  70.  
  71. while(*p){
  72. while(*p && ! std::isalpha(*p))
  73. ++p;
  74.  
  75. i = p;
  76. n = v = 0;
  77. while(std::isalpha(*i)){
  78. if(std::strchr("aeiouyAEIOUY", *i) != NULL)
  79. ++v;
  80. ++i;
  81. ++n;
  82. }
  83.  
  84. if(n > 0){
  85. if(info::_alloc(&arr, cnt, mem)){
  86. arr[cnt].p = p;
  87. arr[cnt].n = n;
  88. arr[cnt].v = v;
  89. ++cnt;
  90. }
  91. }
  92. p = i;
  93. }
  94.  
  95. //сортируем
  96. bsort(arr, arr + cnt);
  97.  
  98. //выводим
  99. const info* end = arr + cnt;
  100. for(const info* it = arr; it != end; ++it){
  101. _out.write(it->p, (int)it->n);
  102. _out << std::endl;
  103. }
  104. free(arr);
  105. }
  106.  
  107.  
  108. int main(void){
  109. char s[] = "information found\n no matching symbolic";
  110. std::istrstream sp(s);
  111. output_ws(std::cout, sp);
  112.  
  113. /* вывести из файла
  114. std::ifstream fp("input.txt");
  115. output_ws(std::cout, fp);
  116. fp.close();
  117. */
  118. return 0;
  119. }
  120.  
Success #stdin #stdout 0s 3232KB
stdin
Standard input is empty
stdout
no
found
matching
symbolic
information