#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;
// Функция подсчета (без ввода/вывода)
int countWords(string text, string word) {
int count = 0, pos = 0;
while ((pos = text.find(word, pos)) != string::npos) {
count++;
pos += word.length();
}
return count;
}
// Функция ввода
string getWord() {
string w; cin >> w; return w;
}
int main() {
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
string s1 = "Иванушка тут", s2 = "Иванушка и Иванушка", s3 = "Нет его";
cout << "Слово: ";
string search = getWord();
int c1 = countWords(s1, search), c2 = countWords(s2, search), c3 = countWords(s3, search);
// Проверка на отсутствие слова во всех текстах
if (c1 == 0 && c2 == 0 && c3 == 0) {
cout << "Ошибка: слово не найдено ни в одном тексте!";
}
else {
if (c1 >= c2 && c1 >= c3) cout << "В S1 больше: " << c1;
else if (c2 >= c1 && c2 >= c3) cout << "В S2 больше: " << c2;
else cout << "В S3 больше: " << c3;
}
return 0;
}
2 задание:
#include <iostream>
#include <string>
#include <vector>
#include <Windows.h>
using namespace std;
struct Dish {
string name;
double weight;
double price;
};
// Процедура ввода
void inputMenu(vector<Dish>& menu) {
for (int i = 0; i < menu.size(); i++) {
cout << "Блюдо " << i + 1 << endl;
cout << " Название: ";
// ws пропускает лишние пробелы/переносы строк, getline читает всю строку
getline(cin >> ws, menu[i].name);
cout << " Вес: ";
cin >> menu[i].weight;
cout << " Цена: ";
cin >> menu[i].price;
}
}
// Функция нахождения веса
double getTotalWeight(const vector<Dish>& menu) {
double total = 0;
for (const auto& d : menu) total += d.weight;
return total;
}
int main() {
setlocale(LC_ALL, "Russian");
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
int n;
cout << "Введите количество блюд: ";
cin >> n;
vector<Dish> dept1(n), dept2(n);
cout << "\n--- Отделение 1 ---" << endl;
inputMenu(dept1);
cout << "\n--- Отделение 2 ---" << endl;
inputMenu(dept2);
cout << "\nОбщий вес 1: " << getTotalWeight(dept1);
cout << "\nОбщий вес 2: " << getTotalWeight(dept2) << endl;
return 0;
}