diff --git a/lab15/t14_1.cpp b/lab15/t14_1.cpp new file mode 100644 index 0000000..3e3d4e0 --- /dev/null +++ b/lab15/t14_1.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include + +double calculateExpression(const std::vector& numbers, const std::vector& 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 numbers; + std::vector 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; +} diff --git a/lab15/t14_12.cpp b/lab15/t14_12.cpp new file mode 100644 index 0000000..76b2ab6 --- /dev/null +++ b/lab15/t14_12.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +#include +#include + +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 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; +} diff --git a/lab15/t14_2.cpp b/lab15/t14_2.cpp new file mode 100644 index 0000000..22e187a --- /dev/null +++ b/lab15/t14_2.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include + +int main() { + std::ifstream inputFile("text.txt"); + if (!inputFile.is_open()) { + std::cerr << "Не вдалося відкрити файл для читання." << std::endl; + return 1; + } + + std::vector 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; +} diff --git a/lab15/t14_3.cpp b/lab15/t14_3.cpp new file mode 100644 index 0000000..f4e0d23 --- /dev/null +++ b/lab15/t14_3.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include + +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 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; +} diff --git a/lab15/t14_4.cpp b/lab15/t14_4.cpp new file mode 100644 index 0000000..253498b --- /dev/null +++ b/lab15/t14_4.cpp @@ -0,0 +1,47 @@ +#include +#include +#include + +bool checkTdTags(const std::string& line) { + std::stack 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 << ": неправильна розстановка тегів ." << std::endl; + return 1; + } + } + + inputFile.close(); + + std::cout << "Розстановка тегів вірна у всіх рядках файлу." << std::endl; + + return 0; +} diff --git a/lab15/t14_5.cpp b/lab15/t14_5.cpp new file mode 100644 index 0000000..eea531d --- /dev/null +++ b/lab15/t14_5.cpp @@ -0,0 +1,70 @@ +#include +#include +#include +#include +#include +#include + +struct Student { + std::string surname; + int course; + std::string group; + std::vector 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(sum) / student.grades.size(); +} + +std::string findStudentWithLowestAverage(const std::vector& students) { + double lowestAverage = std::numeric_limits::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& 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 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; +} diff --git a/lab15/t16_1.cpp b/lab15/t16_1.cpp new file mode 100644 index 0000000..106a4b6 --- /dev/null +++ b/lab15/t16_1.cpp @@ -0,0 +1,144 @@ +#include +#include +#include + +class Person { + std::string name; + unsigned byear; + +public: + int input() { + std::cout << "Enter name: "; + std::cin >> name; + + std::cout << "Enter birth year: "; + std::cin >> byear; + + return 0; + } + + void show() const { + std::cout << "Name: " << name << ", Birth Year: " << byear; + } + + friend std::ostream& operator<<(std::ostream& os, const Person& person) { + os << person.name << " " << person.byear; + return os; + } + + friend std::istream& operator>>(std::istream& is, Person& person) { + is >> person.name >> person.byear; + return is; + } +}; + +class Friend : public Person { + std::string phoneNumber; + +public: + int input() { + Person::input(); + + std::cout << "Enter phone number: "; + std::cin >> phoneNumber; + + return 0; + } + + void show() const { + Person::show(); + std::cout << ", Phone Number: " << phoneNumber; + } + + friend std::ostream& operator<<(std::ostream& os, const Friend& friendObj) { + os << static_cast(friendObj) << " " << friendObj.phoneNumber; + return os; + } + + friend std::istream& operator>>(std::istream& is, Friend& friendObj) { + is >> static_cast(friendObj) >> friendObj.phoneNumber; + return is; + } +}; + +class PhoneBook { + Friend friends[100]; + int count; + +public: + PhoneBook() : count(0) {} + + int addFriend(const Friend& friendObj) { + if (count < 100) { + friends[count++] = friendObj; + return 0; + } else { + return -1; + } + } + + std::string findPhoneNumber(const std::string& name) const { + for (int i = 0; i < count; ++i) { + if (friends[i].name == name) { + return friends[i].phoneNumber; + } + } + + return "Not found"; + } + + int updatePhoneNumber(const std::string& name, const std::string& newPhoneNumber) { + for (int i = 0; i < count; ++i) { + if (friends[i].name == name) { + friends[i].phoneNumber = newPhoneNumber; + return 0; + } + } + + return -1; + } + + void saveToFile(const std::string& filename) const { + std::ofstream outputFile(filename); + + if (!outputFile.is_open()) { + std::cerr << "Unable to open file for writing." << std::endl; + return; + } + + for (int i = 0; i < count; ++i) { + outputFile << friends[i] << "\n"; + } + + outputFile.close(); + } +}; + +int main() { + PhoneBook phoneBook; + Friend friend1; + friend1.input(); + phoneBook.addFriend(friend1) + Friend friend2; + friend2.input(); + phoneBook.addFriend(friend2); + phoneBook.saveToFile("phonebook.txt"); + std::string nameToFind = "John Doe"; + std::string newPhoneNumber = "555-1234"; + + std::cout << "Phone number for " << nameToFind << ": " << phoneBook.findPhoneNumber(nameToFind) << std::endl; + + if (phoneBook.updatePhoneNumber(nameToFind, newPhoneNumber) == 0) { + std::cout << "Phone number updated successfully." << std::endl; + } else { + std::cout << "Failed to update phone number. Entry not found." << std::endl; + } + + std::cout << "Phonebook after update:\n"; + for (int i = 0; i < phoneBook.getCount(); ++i) { + phoneBook.getFriend(i).show(); + std::cout << std::endl; + } + + return 0; +} diff --git a/lab15/t16_2.cpp b/lab15/t16_2.cpp new file mode 100644 index 0000000..2202d26 --- /dev/null +++ b/lab15/t16_2.cpp @@ -0,0 +1,134 @@ +#include +#include + +class Point { +protected: + double x, y; + +public: + Point() : x(0), y(0) {} + + virtual void input() { + std::cout << "Enter x-coordinate: "; + std::cin >> x; + std::cout << "Enter y-coordinate: "; + std::cin >> y; + } + + virtual void output() const { + std::cout << "(" << x << ", " << y << ")"; + } +}; + +class Point3D : public Point { +protected: + double z; + +public: + Point3D() : z(0) {} + + void input() override { + Point::input(); + std::cout << "Enter z-coordinate: "; + std::cin >> z; + } + + void output() const override { + Point::output(); + std::cout << ", " << z; + } +}; + +class Segment { +private: + Point start; + Point end; + +public: + Segment() {} + + void input() { + std::cout << "Enter start point:\n"; + start.input(); + std::cout << "Enter end point:\n"; + end.input(); + } + + void output() const { + std::cout << "Segment from "; + start.output(); + std::cout << " to "; + end.output(); + } + + double length() const { + double dx = end.x - start.x; + double dy = end.y - start.y; + return std::sqrt(dx * dx + dy * dy); + } +}; + +class Segment3D { +private: + Point3D start; + Point3D end; + +public: + Segment3D() {} + + void input() { + std::cout << "Enter start point:\n"; + start.input(); + std::cout << "Enter end point:\n"; + end.input(); + } + + void output() const { + std::cout << "Segment from "; + start.output(); + std::cout << " to "; + end.output(); + } + + double length() const { + double dx = end.x - start.x; + double dy = end.y - start.y; + double dz = end.z - start.z; + return std::sqrt(dx * dx + dy * dy + dz * dz); + } +}; + +int main() { + Point p1; + Point3D p3d1; + Segment s1; + Segment3D s3d; + + std::cout << "Enter 2D point:\n"; + p1.input(); + + std::cout << "Enter 3D point:\n"; + p3d1.input(); + + std::cout << "Enter 2D segment:\n"; + s1.input(); + + std::cout << "Enter 3D segment:\n"; + s3d.input(); + + std::cout << "\n2D Point:\n"; + p1.output(); + + std::cout << "\n3D Point:\n"; + p3d1.output(); + + std::cout << "\n2D Segment:\n"; + s1.output(); + std::cout << "\nLength: " << s1.length() << "\n"; + + std::cout << "\n3D Segment:\n"; + s3d.output(); + std::cout << "\nLength: " << s3d.length() << "\n"; + + return 0; +} diff --git a/lab15/t16_3.cpp b/lab15/t16_3.cpp new file mode 100644 index 0000000..2e879be --- /dev/null +++ b/lab15/t16_3.cpp @@ -0,0 +1,190 @@ +#include +#include +#include +#include + +class Person { +protected: + std::string name; + unsigned byear; + +public: + Person() : byear(0) {} + + virtual void input() { + std::cout << "Enter name: "; + std::cin >> name; + std::cout << "Enter birth year: "; + std::cin >> byear; + } + + virtual void output() const { + std::cout << "Name: " << name << ", Birth Year: " << byear; + } + + const std::string& getName() const { + return name; + } +}; + +class Passenger : public Person { +private: + std::string departure; + std::string destination; + int seat; + +public: + Passenger() : seat(0) {} + + void input() override { + Person::input(); + std::cout << "Enter departure location: "; + std::cin >> departure; + std::cout << "Enter destination location: "; + std::cin >> destination; + std::cout << "Enter seat number: "; + std::cin >> seat; + } + + void output() const override { + Person::output(); + std::cout << ", Departure: " << departure << ", Destination: " << destination << ", Seat: " << seat; + } + + int getSeat() const { + return seat; + } + + const std::string& getDeparture() const { + return departure; + } + + const std::string& getDestination() const { + return destination; + } +}; + +class Kaca { +private: + std::vector passengers; + +public: + void addPassenger(const Passenger& passenger) { + passengers.push_back(passenger); + } + + void displayPassengers() const { + for (const auto& passenger : passengers) { + passenger.output(); + std::cout << std::endl; + } + } + + const Passenger* findPassengerByName(const std::string& name) const { + for (const auto& passenger : passengers) { + if (passenger.getName() == name) { + return &passenger; + } + } + return nullptr; // Passenger not found + } + + const Passenger* findPassengerByDeparture(const std::string& departure) const { + for (const auto& passenger : passengers) { + if (passenger.getDeparture() == departure) { + return &passenger; + } + } + return nullptr; + + const Passenger* findPassengerByDestination(const std::string& destination) const { + for (const auto& passenger : passengers) { + if (passenger.getDestination() == destination) { + return &passenger; + } + } + return nullptr; + } + + const Passenger* findAvailableSeat() const { + int smallestAvailableSeat = std::numeric_limits::max(); + const Passenger* availablePassenger = nullptr; + + for (const auto& passenger : passengers) { + if (passenger.getSeat() < smallestAvailableSeat) { + smallestAvailableSeat = passenger.getSeat(); + } + } + + if (smallestAvailableSeat != std::numeric_limits::max()) { + for (const auto& passenger : passengers) { + if (passenger.getSeat() == smallestAvailableSeat) { + availablePassenger = &passenger; + break; + } + } + } + + return availablePassenger; + } +}; + +int main() { + Kaca kaca; + + Passenger passenger1; + passenger1.input(); + kaca.addPassenger(passenger1); + + Passenger passenger2; + passenger2.input(); + kaca.addPassenger(passenger2); + + Passenger passenger3; + passenger3.input(); + kaca.addPassenger(passenger3); + + std::cout << "All Passengers:\n"; + kaca.displayPassengers(); + + std::string nameToFind = "John Doe"; + const Passenger* foundPassengerByName = kaca.findPassengerByName(nameToFind); + if (foundPassengerByName != nullptr) { + std::cout << "Passenger found by name:\n"; + foundPassengerByName->output(); + std::cout << std::endl; + } else { + std::cout << "Passenger with name " << nameToFind << " not found.\n"; + } + + std::string departureToFind = "CityA"; + const Passenger* foundPassengerByDeparture = kaca.findPassengerByDeparture(departureToFind); + if (foundPassengerByDeparture != nullptr) { + std::cout << "Passenger found by departure:\n"; + foundPassengerByDeparture->output(); + std::cout << std::endl; + } else { + std::cout << "Passenger with departure " << departureToFind << " not found.\n"; + } + + std::string destinationToFind = "CityB"; + const Passenger* foundPassengerByDestination = kaca.findPassengerByDestination(destinationToFind); + if (foundPassengerByDestination != nullptr) { + std::cout << "Passenger found by destination:\n"; + foundPassengerByDestination->output(); + std::cout << std::endl; + } else { + std::cout << "Passenger with destination " << destinationToFind << " not found.\n"; + } + + const Passenger* availableSeat = kaca.findAvailableSeat(); + if (availableSeat != nullptr) { + std::cout << "Available seat found:\n"; + availableSeat->output(); + std::cout << std::endl; + } else { + std::cout << "No available seat found.\n"; + } + + return 0; +}