Created
February 5, 2026 09:39
-
-
Save ExtremeFine21/50d37c443b8d45cbda7ddd9fcad01540 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <fstream> | |
| #include <string> | |
| #include <regex> | |
| #include <cstdio> | |
| #include <array> | |
| using namespace std; | |
| // зчитати результат команди (curl) у string | |
| string runCommand(const string& cmd) { | |
| array<char, 256> buffer{}; | |
| string result; | |
| FILE* pipe = popen(cmd.c_str(), "r"); | |
| if (!pipe) return ""; | |
| while (fgets(buffer.data(), (int)buffer.size(), pipe) != nullptr) { | |
| result += buffer.data(); | |
| } | |
| pclose(pipe); | |
| // прибрати зайві \n в кінці | |
| while (!result.empty() && (result.back() == '\n' || result.back() == '\r')) { | |
| result.pop_back(); | |
| } | |
| return result; | |
| } | |
| int main() { | |
| // 1) Місто зафіксоване як Одеса (по твоєму запиту) | |
| string city = "Odessa"; | |
| // 2) Отримуємо погоду (простий рядок) | |
| // приклад: "Odessa: 🌦 +3°C" | |
| string url = "https://wttr.in/" + city + "?format=3"; | |
| string weatherLine = runCommand("curl -s \"" + url + "\""); | |
| if (weatherLine.empty()) { | |
| cout << "Не вдалося отримати погоду (curl)\n"; | |
| return 1; | |
| } | |
| // 3) Створюємо HTML файл (по завданню: зберегти код у файл) | |
| const string htmlFile = "weather.html"; | |
| ofstream out(htmlFile); | |
| if (!out.is_open()) { | |
| cout << "Не вдалося створити weather.html\n"; | |
| return 1; | |
| } | |
| out << "<!DOCTYPE html>\n<html>\n<head><meta charset=\"UTF-8\"><title>Weather</title></head>\n<body>\n"; | |
| out << "<p class=\"today-temp\">" << weatherLine << "</p>\n"; | |
| out << "</body>\n</html>\n"; | |
| out.close(); | |
| cout << "Створено файл: " << htmlFile << "\n"; | |
| // 4) Зчитуємо HTML і витягуємо контент <p class="...">...</p> через std::regex (як у завданні) | |
| ifstream in(htmlFile); | |
| string html((istreambuf_iterator<char>(in)), istreambuf_iterator<char>()); | |
| in.close(); | |
| regex pattern(R"(<p\s+class="[^"]*">([^<]*)</p>)"); | |
| smatch match; | |
| if (regex_search(html, match, pattern)) { | |
| cout << "Погода (з HTML): " << match[1] << "\n"; | |
| } else { | |
| cout << "Не знайдено потрібний <p class=\"...\">...</p>\n"; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment