使用C++构建实战项目:一个简单的任务管理系统

发布时间:2025-01-24 15:42

使用Toggl统计项目中每个任务的耗时。 #生活常识# #时间管理建议# #时间管理应用#

前言

       C++是一门功能强大且灵活的编程语言,广泛应用于系统开发、游戏开发和高性能计算等领域。本文将通过一个简单的任务管理系统项目,展示如何在实践中使用C++进行开发。本项目旨在帮助初学者掌握C++的基本语法和设计思想,同时体验项目从构思到实现的完整过程。

项目背景

       在日常工作和生活中,我们经常需要管理任务。虽然市场上有很多现成的任务管理工具,但通过自己动手开发一个可以根据自己需求定制的系统,不仅能提高编程能力,还可以深入理解C++的特性。本项目的目标是设计一个简单的命令行任务管理系统,支持以下功能:

添加任务查看所有任务完成任务删除任务

技术选型

C++标准库:使用C++标准库中的容器(如vector、list)来存储任务,并使用基本的I/O操作处理用户输入。面向对象设计:本项目将任务封装为一个类,方便后续扩展功能。文件持久化:实现任务列表的保存和加载,确保任务在程序关闭后仍然可以保留。

项目结构

TaskManager

├── main.cpp # 主程序入口

├── Task.h # 任务类定义

├── TaskManager.h # 任务管理类定义

├── TaskManager.cpp # 任务管理类实现

└── tasks.txt # 任务持久化文件

核心代码

任务类设计

       我们首先定义一个Task类,来表示一个任务对象。任务类包含任务的标题、描述和完成状态。

#ifndef TASK_H

#define TASK_H

#include <string>

class Task {

private:

std::string title;

std::string description;

bool completed;

public:

Task(const std::string& title, const std::string& description)

: title(title), description(description), completed(false) {}

void complete() { completed = true; }

std::string getTitle() const { return title; }

std::string getDescription() const { return description; }

bool isCompleted() const { return completed; }

std::string serialize() const;

static Task deserialize(const std::string& taskData);

};

#endif

任务管理类设计

       接下来我们实现TaskManager类,负责管理任务的增删查改。

#ifndef TASKMANAGER_H

#define TASKMANAGER_H

#include "Task.h"

#include <vector>

class TaskManager {

private:

std::vector<Task> tasks;

public:

void addTask(const std::string& title, const std::string& description);

void listTasks() const;

void completeTask(int index);

void deleteTask(int index);

void saveToFile(const std::string& filename) const;

void loadFromFile(const std::string& filename);

};

#endif

       实现TaskManager类的功能,包括添加任务、列出任务、完成任务和删除任务。

// TaskManager.cpp

#include "TaskManager.h"

#include <iostream>

#include <fstream>

void TaskManager::addTask(const std::string& title, const std::string& description) {

tasks.emplace_back(title, description);

std::cout << "Task added: " << title << std::endl;

}

void TaskManager::listTasks() const {

if (tasks.empty()) {

std::cout << "No tasks available." << std::endl;

} else {

for (size_t i = 0; i < tasks.size(); ++i) {

std::cout << i + 1 << ". " << tasks[i].getTitle()

<< (tasks[i].isCompleted() ? " [Completed]" : "") << std::endl;

}

}

}

void TaskManager::completeTask(int index) {

if (index > 0 && index <= tasks.size()) {

tasks[index - 1].complete();

std::cout << "Task " << index << " marked as completed." << std::endl;

} else {

std::cout << "Invalid task index." << std::endl;

}

}

void TaskManager::deleteTask(int index) {

if (index > 0 && index <= tasks.size()) {

tasks.erase(tasks.begin() + index - 1);

std::cout << "Task " << index << " deleted." << std::endl;

} else {

std::cout << "Invalid task index." << std::endl;

}

}

// 持久化功能,保存到文件

void TaskManager::saveToFile(const std::string& filename) const {

std::ofstream file(filename);

for (const auto& task : tasks) {

file << task.serialize() << std::endl;

}

}

void TaskManager::loadFromFile(const std::string& filename) {

std::ifstream file(filename);

std::string line;

while (std::getline(file, line)) {

tasks.push_back(Task::deserialize(line));

}

}

主程序

       最后,我们实现一个简单的主循环,让用户可以通过命令行添加、查看和管理任务。

// main.cpp

#include "TaskManager.h"

#include <iostream>

int main() {

TaskManager manager;

manager.loadFromFile("tasks.txt");

while (true) {

std::cout << "1. Add Task\n2. List Tasks\n3. Complete Task\n4. Delete Task\n5. Save and Exit\n";

int choice;

std::cin >> choice;

if (choice == 1) {

std::string title, description;

std::cin.ignore(); // 忽略换行符

std::cout << "Enter title: ";

std::getline(std::cin, title);

std::cout << "Enter description: ";

std::getline(std::cin, description);

manager.addTask(title, description);

} else if (choice == 2) {

manager.listTasks();

} else if (choice == 3) {

int taskIndex;

std::cout << "Enter task index to complete: ";

std::cin >> taskIndex;

manager.completeTask(taskIndex);

} else if (choice == 4) {

int taskIndex;

std::cout << "Enter task index to delete: ";

std::cin >> taskIndex;

manager.deleteTask(taskIndex);

} else if (choice == 5) {

manager.saveToFile("tasks.txt");

break;

}

}

return 0;

}

项目总结

       通过这个项目,我们不仅复习了C++的基本语法,还掌握了如何使用面向对象编程设计任务管理系统。同时,我们也接触了文件的读写操作,使任务能够跨程序运行保存。本项目可以作为更复杂应用的基础,例如添加任务的优先级、截止日期等,进一步优化任务管理逻辑。

网址:使用C++构建实战项目:一个简单的任务管理系统 https://www.yuejiaxmz.com/news/view/738954

相关内容

使用 Spring Boot 构建在线任务管理系统
NocoBase 实战教程 —— 任务管理系统
用python做一个任务管理系统
利用Python+Flask实现一个TODO任务管理系统网站
使用python制作一个简单的任务管理器
erp系统考勤项目管理系统
Swift实战:高效构建待办事项列表与任务管理应用
构建React TodoList应用:管理你的任务清单
任务/项目管理
如何高效管理项目任务?探索项目任务管理方法与实用工具大全!

随便看看