Created
September 11, 2023 21:55
-
-
Save xsa-dev/514db25b53d21f79ac0ba52a4f465e74 to your computer and use it in GitHub Desktop.
very simple task list with cpp
This file contains 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 <vector> | |
#include <string> | |
// Структура для представления задачи | |
struct Task { | |
std::string title; | |
std::string description; | |
}; | |
// Функция для добавления задачи в вектор | |
void addTask(std::vector<Task>& tasks, const std::string& title, const std::string& description) { | |
Task task; | |
task.title = title; | |
task.description = description; | |
tasks.push_back(task); | |
std::cout << "Задача добавлена." << std::endl; | |
} | |
// Функция для просмотра всех задач | |
void viewTasks(const std::vector<Task>& tasks) { | |
if (tasks.empty()) { | |
std::cout << "Список задач пуст." << std::endl; | |
return; | |
} | |
std::cout << "Список задач:" << std::endl; | |
for (size_t i = 0; i < tasks.size(); ++i) { | |
std::cout << "Задача " << i + 1 << ":" << std::endl; | |
std::cout << "Заголовок: " << tasks[i].title << std::endl; | |
std::cout << "Описание: " << tasks[i].description << std::endl; | |
std::cout << "------------------------" << std::endl; | |
} | |
} | |
// Функция для удаления задачи по индексу | |
void deleteTask(std::vector<Task>& tasks, size_t index) { | |
if (index >= 0 && index < tasks.size()) { | |
tasks.erase(tasks.begin() + index); | |
std::cout << "Задача удалена." << std::endl; | |
} else { | |
std::cout << "Некорректный индекс задачи." << std::endl; | |
} | |
} | |
int main() { | |
std::vector<Task> tasks; | |
while (true) { | |
std::cout << "Выберите действие:" << std::endl; | |
std::cout << "1. Добавить задачу" << std::endl; | |
std::cout << "2. Просмотреть задачи" << std::endl; | |
std::cout << "3. Удалить задачу" << std::endl; | |
std::cout << "4. Выйти из программы" << std::endl; | |
int choice; | |
std::cin >> choice; | |
switch (choice) { | |
case 1: { | |
std::string title, description; | |
std::cout << "Введите заголовок задачи: "; | |
std::cin.ignore(); | |
std::getline(std::cin, title); | |
std::cout << "Введите описание задачи: "; | |
std::getline(std::cin, description); | |
addTask(tasks, title, description); | |
break; | |
} | |
case 2: | |
viewTasks(tasks); | |
break; | |
case 3: { | |
int index; | |
std::cout << "Введите индекс задачи для удаления: "; | |
std::cin >> index; | |
deleteTask(tasks, index - 1); | |
break; | |
} | |
case 4: | |
std::cout << "Программа завершена." << std::endl; | |
return 0; | |
default: | |
std::cout << "Некорректный выбор. Попробуйте снова." << std::endl; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment