Skip to content

Instantly share code, notes, and snippets.

@ExtremeFine21
Created February 5, 2026 09:50
Show Gist options
  • Select an option

  • Save ExtremeFine21/6b1788f845ddfdc3479214499387590c to your computer and use it in GitHub Desktop.

Select an option

Save ExtremeFine21/6b1788f845ddfdc3479214499387590c to your computer and use it in GitHub Desktop.
#include <iostream>
#include <fstream>
#include <string>
#include <regex>
#include <cstdio>
#include <array>
#include <algorithm>
using namespace std;
// Отримання виводу команди (curl)
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(), buffer.size(), pipe) != nullptr) {
result += buffer.data();
}
pclose(pipe);
// прибрати \n та \r з кінця
while (!result.empty() && (result.back() == '\n' || result.back() == '\r')) {
result.pop_back();
}
return result;
}
int main() {
// ====== як на скріні ======
cout << "Введіть назву міста: ";
string cityInput;
getline(cin, cityInput);
// за умовою практики — працюємо з Варшавою
string city = "Warsaw";
// ====== отримуємо температуру ======
string url = "https://wttr.in/" + city + "?format=%t";
string temp = runCommand("curl -s \"" + url + "\"");
if (temp.empty()) {
cerr << "Не вдалося отримати погоду\n";
return 1;
}
// ====== зберігаємо в weather.txt ======
const char* destFile = "weather.txt";
{
ofstream out(destFile);
out << temp << "\n";
}
cout << "Збережено в " << destFile << "\n";
// ====== читаємо з файлу (як у тебе) ======
ifstream in(destFile);
if (!in) {
cerr << "Помилка відкриття файлу\n";
return 1;
}
string tempFromFile;
getline(in, tempFromFile);
in.close();
tempFromFile.erase(
remove(tempFromFile.begin(), tempFromFile.end(), '\r'),
tempFromFile.end()
);
// ====== створюємо weather.html ======
const char* htmlFile = "weather.html";
{
ofstream htmlOut(htmlFile);
htmlOut << "<!DOCTYPE html>\n<html>\n<head><meta charset=\"UTF-8\"></head>\n<body>\n";
htmlOut << "<p class=\"today-temp\">" << tempFromFile << "</p>\n";
htmlOut << "</body>\n</html>\n";
}
// ====== вилучення через std::regex (по завданню) ======
ifstream htmlIn(htmlFile);
string html((istreambuf_iterator<char>(htmlIn)), istreambuf_iterator<char>());
htmlIn.close();
regex pattern(R"(<p\s+class="[^"]*">([^<]*)</p>)");
smatch match;
string extractedTemp = tempFromFile;
if (regex_search(html, match, pattern)) {
extractedTemp = match[1];
}
// ====== ВИВІД ЯК НА СКРИНІ ======
cout << "Поточна температура у місті Варшава: " << extractedTemp << "\n";
cout << "Отримано з сайту: " << extractedTemp << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment