Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions lab15/t14_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <cmath>

double calculateExpression(const std::vector<double>& numbers, const std::vector<char>& operators) {
double result = numbers[0];

for (size_t i = 0; i < operators.size(); ++i) {
if (operators[i] == '+') {
result += numbers[i + 1];
} else if (operators[i] == '-') {
result -= numbers[i + 1];
}
}

return result;
}

int main() {
std::ifstream inputFile("expression.txt");
if (!inputFile.is_open()) {
std::cerr << "Не вдалося відкрити файл для читання." << std::endl;
return 1;
}

std::string expression;
std::getline(inputFile, expression);

inputFile.close();

std::vector<double> numbers;
std::vector<char> operators;

std::istringstream iss(expression);
double number;
char op;

while (iss >> number) {
numbers.push_back(number);

if (iss >> op) {
operators.push_back(op);
}
}

double result = calculateExpression(numbers, operators);
std::cout << "Значення виразу: " << result << std::endl;

return 0;
}
61 changes: 61 additions & 0 deletions lab15/t14_12.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>

void createAndProcessFile(const std::string& filename, int numLines) {
std::ofstream outputFile(filename);

if (!outputFile.is_open()) {
std::cerr << "Не вдалося відкрити файл для запису." << std::endl;
return;
}

for (int i = 0; i < numLines; ++i) {
std::string line = " Line " + std::to_string(i + 1) + " ";
outputFile << line << "\n";
}

outputFile.close();

std::ifstream inputFile(filename);

if (!inputFile.is_open()) {
std::cerr << "Не вдалося відкрити файл для читання." << std::endl;
return;
}

std::vector<std::string> lines;

std::string line;
while (std::getline(inputFile, line)) {
line.erase(std::unique(line.begin(), line.end(), [](char a, char b) { return a == ' ' && b == ' '; }), line.end());
line.erase(line.find_last_not_of(" \t") + 1);

lines.push_back(line);
}

inputFile.close();

std::ofstream editedFile(filename);

if (!editedFile.is_open()) {
std::cerr << "Не вдалося відкрити файл для запису." << std::endl;
return;
}

for (const auto& editedLine : lines) {
editedFile << editedLine << "\n";
}

editedFile.close();
}

int main() {
createAndProcessFile("example.txt", 5);

std::cout << "Файл був створений та відредагований." << std::endl;

return 0;
}
34 changes: 34 additions & 0 deletions lab15/t14_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>

int main() {
std::ifstream inputFile("text.txt");
if (!inputFile.is_open()) {
std::cerr << "Не вдалося відкрити файл для читання." << std::endl;
return 1;
}

std::vector<std::string> words;
std::string word;
while (inputFile >> word) {
words.push_back(word);
}

inputFile.close();

for (size_t i = 0; i < words.size() - 2; ++i) {
if (words[i] == "die" || words[i] == "der" || words[i] == "das") {
std::transform(words[i + 2].begin(), words[i + 2].end(), words[i + 2].begin(), ::toupper);
}
}

for (const auto& w : words) {
std::cout << w << " ";
}
std::cout << std::endl;

return 0;
}
51 changes: 51 additions & 0 deletions lab15/t14_3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>

std::string decryptWord(const std::string& encryptedWord) {
std::string decryptedWord = encryptedWord;
std::reverse(decryptedWord.begin(), decryptedWord.end());
return decryptedWord;
}

int main() {
std::ifstream inputFile("encrypted_text.txt");
if (!inputFile.is_open()) {
std::cerr << "Не вдалося відкрити файл для читання." << std::endl;
return 1;
}

std::string line;
std::vector<std::string> words;

while (std::getline(inputFile, line)) {
std::istringstream iss(line);
std::string word;

while (iss >> word) {
size_t lastCharIndex = word.find_last_of(",.!?");

if (lastCharIndex != std::string::npos) {
words.push_back(word.substr(0, lastCharIndex));
words.push_back(word.substr(lastCharIndex));
} else {
words.push_back(word);
}
}
}
inputFile.close();
std::ofstream outputFile("decrypted_text.txt");
if (!outputFile.is_open()) {
std::cerr << "Не вдалося відкрити файл для запису." << std::endl;
return 1;
}
for (const auto& word : words) {
outputFile << decryptWord(word) << " ";
}
outputFile.close();
std::cout << "Розшифрування завершено. Результат записано в decrypted_text.txt." << std::endl;

return 0;
}
47 changes: 47 additions & 0 deletions lab15/t14_4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <iostream>
#include <fstream>
#include <stack>

bool checkTdTags(const std::string& line) {
std::stack<char> tagStack;

for (char ch : line) {
if (ch == '<') {
tagStack.push(ch);
} else if (ch == '>') {
if (!tagStack.empty() && tagStack.top() == '<') {
tagStack.pop();
} else {
return false;
}
}
}

return tagStack.empty();
}

int main() {
std::ifstream inputFile("your_file.txt");

if (!inputFile.is_open()) {
std::cerr << "Не вдалося відкрити файл для читання." << std::endl;
return 1;
}

std::string line;
int lineNumber = 0;

while (std::getline(inputFile, line)) {
lineNumber++;
if (!checkTdTags(line)) {
std::cout << "Помилка в рядку " << lineNumber << ": неправильна розстановка тегів <td>." << std::endl;
return 1;
}
}

inputFile.close();

std::cout << "Розстановка тегів <td> вірна у всіх рядках файлу." << std::endl;

return 0;
}
70 changes: 70 additions & 0 deletions lab15/t14_5.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <algorithm>
#include <limits>

struct Student {
std::string surname;
int course;
std::string group;
std::vector<int> grades;
};

double calculateAverageGrade(const Student& student) {
if (student.grades.empty()) {
return 0.0;
}

int sum = std::accumulate(student.grades.begin(), student.grades.end(), 0);
return static_cast<double>(sum) / student.grades.size();
}

std::string findStudentWithLowestAverage(const std::vector<Student>& students) {
double lowestAverage = std::numeric_limits<double>::max();
std::string lowestSurname;

for (const auto& student : students) {
double average = calculateAverageGrade(student);
if (average < lowestAverage) {
lowestAverage = average;
lowestSurname = student.surname;
}
}

return lowestSurname;
}

void createStudentFile(const std::string& filename, const std::vector<Student>& students) {
std::ofstream outputFile(filename);

if (!outputFile.is_open()) {
std::cerr << "Не вдалося відкрити файл для запису." << std::endl;
return;
}

for (const auto& student : students) {
outputFile << student.surname << " " << student.course << " " << student.group << " ";
for (const auto& grade : student.grades) {
outputFile << grade << ",";
}
outputFile << "\n";
}

outputFile.close();
}

int main() {
std::vector<Student> students = {
{"Kondrashov", 2, "A1", {85, 90, 78}},
{"Schevchenko", 3, "B2", {92, 88, 76}},
{"Matseniuk", 1, "C3", {80, 95, 82}}
};

createStudentFile("students.txt", students);
std::string lowestSurname = findStudentWithLowestAverage(students);
std::cout << "Студент з найгіршою середньою оцінкою: " << lowestSurname << std::endl;

return 0;
}
Loading