fork(1) download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <strstream>
  4. #include <cctype>
  5. #include <string>
  6.  
  7.  
  8. //кол-во слов в строке
  9. int count_words(const char* s){
  10. int n = 0;
  11. while(*s){
  12. while(*s && ! isalnum(*s))
  13. ++s;
  14. if(isalnum(*s)){
  15. ++n;
  16. while(isalnum(*s))
  17. ++s;
  18. }
  19. }
  20. return n;
  21. }
  22.  
  23. //удаление слова
  24. void remove_avg(std::string& s){
  25. int inx;
  26. int cnt = count_words(s.c_str());
  27. if(cnt <= 2)
  28. return;
  29.  
  30. cnt = (cnt + 2) / 3;
  31. inx = 0;
  32.  
  33. const char* i, *p = s.c_str();
  34. while(*p){
  35. while(*p && ! isalnum(*p))
  36. ++p;
  37.  
  38. if(isalnum(*p)){
  39. i = p + 1;
  40. while(isalnum(*i))
  41. ++i;
  42.  
  43. if(inx++ >= cnt){
  44. s.erase((p - s.c_str()), i - p);
  45. break;
  46. }
  47. p = i;
  48. }
  49. }
  50. }
  51.  
  52.  
  53. //удаление среднего слова
  54. void output_rem(std::ostream& _o, std::istream& _i){
  55. std::string l;
  56. while((! _i.eof()) && (! _i.fail())){
  57. std::getline(_i, l);
  58. if(l.length() > 0){
  59. remove_avg(l);
  60. _o << l << std::endl;
  61. } else
  62. _o << std::endl;
  63. }
  64. _o.flush();
  65. }
  66.  
  67.  
  68.  
  69. int main(void){
  70. char s[] = "fox dog bat cat wolf bea cow\r\n"\
  71. "AA BB CC DD EE FF\r\n"\
  72. "A1 B2 C3 D4 C5";
  73. std::istrstream ist(s);
  74. output_rem(std::cout, ist);
  75.  
  76. /* вот работать с файлом
  77. std::ifstream fin("input.txt");
  78. std::ofstream fout("output.txt");
  79. output_rem(fout, fin);
  80. fin.close();
  81. fout.close();
  82. */
  83. return 0;
  84. }
  85.  
Success #stdin #stdout 0s 3276KB
stdin
Standard input is empty
stdout
fox dog bat  wolf bea cow
AA BB  DD EE FF
A1 B2  D4 C5