@Retr-0

Как рассчитать среднее арифметическое длин слов в строке на C++?

Необходимо реализовать данную программу, допустим, если у нас 3 слова в 3-х строках
  • Вопрос задан
  • 214 просмотров
Решения вопроса 1
myjcom
@myjcom Куратор тега C++
допустим

#include<iostream>
#include<algorithm>
#include<sstream>
#include<string>
#include<vector>
#include<numeric>
#include<iomanip>
using namespace std;

auto avg = [](istream& is, int n){
  vector<double> avgPerLine;
  generate_n(back_inserter(avgPerLine), n, [&](){
    string line;
    getline(is, line);

    auto words = istringstream(line);
    int  words_count = 0;
    int  words_length = 0;
    
    while(words >> line)
    {
      words_count++;
      words_length += line.length();
    }
    return words_count ? static_cast<double>(words_length) / words_count : 0.0;
  });
  return accumulate(avgPerLine.cbegin(), avgPerLine.cend(), 0.0) / avgPerLine.size();
};

void print(const string& msg, double value)
{
  cout << setprecision(2)
       << msg << "\n"
       << "avg == " << value << endl;
}

int main()
{
  setlocale(LC_ALL, "");

  string test_str = "one two three\nfour five six\nseven eight nine";
  string test_str2 = "word1\nword2\nword3";
  
  auto test  = istringstream(test_str);
  auto test2 = istringstream(test_str2);
  
  print(test_str, avg(test, 3));
  print(test_str2, avg(test2, 3));
 
  cout << "Введи три предложения, по одному в строке:\n";
  cout << "avg == " << avg(cin, 3) << endl;
  
  system("pause");

}
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы