/
Oppq
/
Labs
Обзор
Документация
Войти
/
Oppq
/
Labs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Lab8/Lab2-4.cpp
396 строк
13 KB
Oppq
Lab8
21 май 2026, 07:41
Верифицирован
21 май 2026, 07:41
2cd3883
Код
Авторство
О чём код?
#include <iostream> #include <cmath> #include <iomanip> #include <fstream> #include <string> #include <ctime> #include "json.hpp" using json = nlohmann::json; // Лаб2 int main_lab2() { int choice; float x; int n = 0; float r; float u; json result; result["timestamp"] = time(0); cout << "Введите x (x > 0): "; cin >> x; result["x"] = x; if (x <= 0) { cout << "Значение x не подходит под условие"; result["error"] = "x <= 0"; return 1; } cout << "Выберите способ вычисления ln(x):"; cout << "1 - С заданной погрешностью eps"; cout << "2 - С заданным числом k первых членов ряда"; cout << "3 - По аналитической формуле (стандартная функция)"; cout << "Ваш выбор (1, 2 или 3): "; cin >> choice; result["choice"] = choice; if (choice < 1 || choice > 3) { cout << "Данного способа вычисления не существует в алгоритме" << endl; result["error"] = "invalid choice"; return 1; } switch (choice) { case 1: { float y = 0; cout << "Введите погрешность (0 < eps < 1): "; float eps; cin >> eps; result["eps"] = eps; if (eps < 0 || eps >= 1) { cout << "Погрешность не подходит под условие задачи"; result["error"] = "invalid eps"; return 1; } do { u = pow(((x - 1) / (x + 1)), 2 * n + 1) / (2 * n + 1); y += u; n++; } while (n < 1000); r = y * 2; result["result"] = r; result["method"] = "precision"; cout << "Итоговая сумма: " << setprecision(10) << r << endl; break; } case 2: { cout << "Введите количество членов, count: "; int count; cin >> count; float y = 0; result["count"] = count; if (count < 0) { cout << "count < 0 при заданном условии x"; result["error"] = "negative count"; return 1; } for (int n = 0; n < count; n++) { float u = pow((x - 1) / (x + 1), 2 * n + 1) / (2 * n + 1); y += u; } r = y * 2; result["result"] = r; result["method"] = "count"; cout << "Итоговая сумма: " << r << endl; break; } case 3: { r = log(x); result["result"] = r; result["method"] = "standard"; cout << "Итоговая сумма: " << r << endl; break; } } ofstream file("lab2_result.json"); file << result.dump(4); file.close(); cout << "Результат сохранён в lab2_result.json" << endl; return 0; } // Лаб3 struct Club { string nameclub; string season; }; struct BD { unsigned day, month, year; }; struct FP { unsigned number, goals; string name, surname; BD birth; Club clubs; }; int main_lab3() { json players_json = json::array(); FP players[5] = {}; for (int i = 0; i < 2; i++) { json player_json; cout << "Игрок № " << i + 1 << "\n"; cout << "Кол-во голов: "; cin >> players[i].goals; player_json["goals"] = players[i].goals; cout << "Имя игрока: "; cin >> players[i].name; player_json["name"] = players[i].name; cout << "Фамилия игрока: "; cin >> players[i].surname; player_json["surname"] = players[i].surname; cout << "День рождения (через Enter): "; cin >> players[i].birth.day >> players[i].birth.month >> players[i].birth.year; player_json["birth"]["day"] = players[i].birth.day; player_json["birth"]["month"] = players[i].birth.month; player_json["birth"]["year"] = players[i].birth.year; if (players[i].birth.day < 1 || players[i].birth.day > 31) { cout << "Некорректное число дня"; return 1; } if (players[i].birth.month < 1 || players[i].birth.month > 12) { cout << "Некорректное число месяца"; return 1; } if (players[i].birth.year < 1990 || players[i].birth.year > 2025) { cout << "Некорректное число года"; return 1; } cout << "Клуб: "; cin >> players[i].clubs.nameclub; player_json["club"]["name"] = players[i].clubs.nameclub; cout << "Сезон: "; cin >> players[i].clubs.season; player_json["club"]["season"] = players[i].clubs.season; players_json.push_back(player_json); } players[2] = {10, 800, "Месси", "Лионель", {24, 6, 1987}, {"Барселона", "2004-2005"}}; players[3] = {7, 750, "Роналду", "Криштиану", {5, 2, 1985}, {"Реал Мадрид", "2010-2011"}}; players[4] = {11, 600, "Неймар", "Сантос", {5, 2, 1992}, {"ПСЖ", "2021-2022"}}; json messi; messi["number"] = 10; messi["goals"] = 800; messi["name"] = "Месси"; messi["surname"] = "Лионель"; messi["birth"]["day"] = 24; messi["birth"]["month"] = 6; messi["birth"]["year"] = 1987; messi["club"]["name"] = "Барселона"; messi["club"]["season"] = "2004-2005"; players_json.push_back(messi); json ronaldo; ronaldo["number"] = 7; ronaldo["goals"] = 750; ronaldo["name"] = "Роналду"; ronaldo["surname"] = "Криштиану"; ronaldo["birth"]["day"] = 5; ronaldo["birth"]["month"] = 2; ronaldo["birth"]["year"] = 1985; ronaldo["club"]["name"] = "Реал Мадрид"; ronaldo["club"]["season"] = "2010-2011"; players_json.push_back(ronaldo); json neymar; neymar["number"] = 11; neymar["goals"] = 600; neymar["name"] = "Неймар"; neymar["surname"] = "Сантос"; neymar["birth"]["day"] = 5; neymar["birth"]["month"] = 2; neymar["birth"]["year"] = 1992; neymar["club"]["name"] = "ПСЖ"; neymar["club"]["season"] = "2021-2022"; players_json.push_back(neymar); cout << "Футбольный клуб:\n"; for (int i = 0; i < 5; i++) { cout << players[i].name << " " << players[i].surname << " - " << players[i].goals << " голов, " << players[i].birth.day << "." << players[i].birth.month << "." << players[i].birth.year << ", " << players[i].clubs.nameclub << " (" << players[i].clubs.season << ")\n"; } ofstream file("lab3_players.json"); file << players_json.dump(4); file.close(); cout << "Данные сохранены в lab3_players.json" << endl; return 0; } //Лаб4 struct Club4 { string nameclub; string season; }; struct BD4 { unsigned day, month, year; }; struct FP4 { unsigned number, goals; string name, surname; BD4 birth; Club4 clubs; }; json all_players = json::array(); void vvo(FP4& igr, int nom) { json j; cout << "\nИгрок № " << nom << "\n"; cout << "Имя: "; cin >> igr.name; j["name"] = igr.name; cout << "Фамилия: "; cin >> igr.surname; j["surname"] = igr.surname; cout << "Номер: "; cin >> igr.number; j["number"] = igr.number; cout << "Голы: "; cin >> igr.goals; j["goals"] = igr.goals; cout << "Дата (д м г): "; cin >> igr.birth.day >> igr.birth.month >> igr.birth.year; j["birth"]["day"] = igr.birth.day; j["birth"]["month"] = igr.birth.month; j["birth"]["year"] = igr.birth.year; cout << "Клуб: "; cin >> igr.clubs.nameclub; j["club"]["name"] = igr.clubs.nameclub; cout << "Сезон: "; cin >> igr.clubs.season; j["club"]["season"] = igr.clubs.season; all_players.push_back(j); } void pok(FP4 igr) { cout << igr.name << " " << igr.surname << " (#" << igr.number << ") - " << igr.goals << " голов, " << igr.birth.day << "." << igr.birth.month << "." << igr.birth.year << ", " << igr.clubs.nameclub << " (" << igr.clubs.season << ")\n"; } void zap(FP4 igr[]) { igr[2].number = 10; igr[2].goals = 800; igr[2].name = "Месси"; igr[2].surname = "Лионель"; igr[2].birth.day = 24; igr[2].birth.month = 6; igr[2].birth.year = 1987; igr[2].clubs.nameclub = "Барселона"; igr[2].clubs.season = "2004-2005"; json messi; messi["number"] = 10; messi["goals"] = 800; messi["name"] = "Месси"; messi["surname"] = "Лионель"; messi["birth"]["day"] = 24; messi["birth"]["month"] = 6; messi["birth"]["year"] = 1987; messi["club"]["name"] = "Барселона"; messi["club"]["season"] = "2004-2005"; all_players.push_back(messi); igr[3].number = 7; igr[3].goals = 750; igr[3].name = "Роналду"; igr[3].surname = "Криштиану"; igr[3].birth.day = 5; igr[3].birth.month = 2; igr[3].birth.year = 1985; igr[3].clubs.nameclub = "Реал Мадрид"; igr[3].clubs.season = "2010-2011"; json ronaldo; ronaldo["number"] = 7; ronaldo["goals"] = 750; ronaldo["name"] = "Роналду"; ronaldo["surname"] = "Криштиану"; ronaldo["birth"]["day"] = 5; ronaldo["birth"]["month"] = 2; ronaldo["birth"]["year"] = 1985; ronaldo["club"]["name"] = "Реал Мадрид"; ronaldo["club"]["season"] = "2010-2011"; all_players.push_back(ronaldo); igr[4].number = 11; igr[4].goals = 600; igr[4].name = "Неймар"; igr[4].surname = "Сантос"; igr[4].birth.day = 5; igr[4].birth.month = 2; igr[4].birth.year = 1992; igr[4].clubs.nameclub = "ПСЖ"; igr[4].clubs.season = "2021-2022"; json neymar; neymar["number"] = 11; neymar["goals"] = 600; neymar["name"] = "Неймар"; neymar["surname"] = "Сантос"; neymar["birth"]["day"] = 5; neymar["birth"]["month"] = 2; neymar["birth"]["year"] = 1992; neymar["club"]["name"] = "ПСЖ"; neymar["club"]["season"] = "2021-2022"; all_players.push_back(neymar); } bool pro(FP4 igr) { if (igr.birth.day < 1 || igr.birth.day > 31) { cout << "Ошибка: день от 1 до 31\n"; return false; } return true; } int main_lab4() { FP4 igr[5]; all_players = json::array(); for (int i = 0; i < 2; i++) { vvo(igr[i], i + 1); if (!pro(igr[i])) return 1; } zap(igr); for (int i = 0; i < 5; i++) { cout << i + 1 << ". "; pok(igr[i]); } ofstream file("lab4_players.json"); file << all_players.dump(4); file.close(); cout << "Данные сохранены в lab4_players.json" << endl; return 0; } // Лаб4 Рекурсия bool check(int& s) { char c; cin.get(c); if (c == ' ') { return true; } if (c < '0' || c > '9') { return false; } s = s + (c - '0'); return check(s); } int main_lab4_recursive() { json result; cout << "Введите число (окончите пробелом): "; int s = 0; cin >> ws; if (check(s) && s > 0 && s % 3 == 0) { cout << "YES"; result["result"] = "YES"; } else { cout << "NO"; result["result"] = "NO"; } result["sum"] = s; cout << endl; ofstream file("lab4_recursive_result.json"); file << result.dump(4); file.close(); cout << "Результат сохранён в lab4_recursive_result.json" << endl; cin.ignore(1000, '\n'); return 0; } // Главная функция int main() { cout << "\nЛаб2\n"; main_lab2(); cout << "\nЛаб3\n"; main_lab3(); cout << "\nЛаб4\n"; main_lab4(); cout << "\nЛаб4Рекурсия (второе задание)\n"; main_lab4_recursive(); return 0; }