从信息孤岛到智能星云:学习助手编织高校学习生活的全维度互联网络

发布于:2025-06-14 ⋅ 阅读:(14) ⋅ 点赞:(0)

一、项目背景与目标

随着高校信息化建设的推进,大学生在日常学习中对信息获取、时间管理、任务安排等方面的需求日益增长。为了帮助大学生更高效地管理学习生活,我们设计并实现了一个基于 API 调用的学习小助手系统。该系统通过调用外部服务接口(如天气预报、课程表查询、待办事项管理等),为用户提供便捷的信息查询和提醒功能。

二、功能模块设计与需求分析

2.1 功能模块划分

模块编号 功能名称 描述
M1 用户登录注册 提供用户注册与登录功能,支持个性化设置
M2 课程表查询 从学校教务系统 API 获取课程表信息
M3 天气预报 调用第三方天气 API 获取当前城市天气
M4 待办事项管理 使用本地或云服务保存待办事项
M5 考试提醒 提供倒计时功能和考试提醒
M6 学习资料推荐 接入教育平台 API 获取学习资源推荐
M7 时间管理 提供日历视图和时间安排功能
M8 新闻资讯推送 调用新闻 API 获取校园及社会热点新闻

2.2 非功能性需求

需求类型与规格

类型 要求
可靠性 系统需稳定运行,API请求失败时提供重试机制
安全性 用户数据加密存储,防止泄露
可扩展性 支持新增API接口和服务模块
易用性 提供命令行界面,操作简洁直观
性能要求 响应速度快,请求延迟低于1秒
兼容性 支持Windows、Linux和macOS平台

三、技术选型与架构设计

3.1 技术栈选择

技术/工具与用途对照表

技术/工具 用途说明
C++ 核心逻辑开发语言,用于封装 API 请求与业务处理
libcurl HTTP 请求库,用于发送 GET/POST 请求
nlohmann/json JSON 解析库,用于解析 API 返回的数据
SQLite 轻量级数据库,用于存储用户信息和待办事项
Makefile/CMake 构建系统,用于编译和链接项目
Git/GitHub 版本控制与代码托管

3.2 系统架构设计

在这里插入图片描述

四、开发流程详解

4.1 环境准备

4.1.1 安装依赖库

# Ubuntu/Linux
sudo apt-get install build-essential g++ cmake
sudo apt-get install libcurl4-openssl-dev

4.1.2 下载 JSON 库

nlohmann/jsonjson.hpp 文件放入项目目录下的 include 文件夹中。

4.2 封装 HTTP 请求类(libcurl)

4.2.1 创建 http_client.h

// http_client.h
#ifndef HTTP_CLIENT_H
#define HTTP_CLIENT_H
#include <string>
std::string sendGetRequest(const std::string& url);
#endif // HTTP_CLIENT_H

4.2.2 实现 http_client.cpp

// http_client.cpp
#include "http_client.h"
#include <iostream>
#include <curl/curl.h>
#include "json.hpp"

using json = nlohmann::json;

size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* s)
{
    size_t realsize = size * nmemb;
    s->append((char*)contents, realsize);
    return realsize;
}

std::string sendGetRequest(const std::string& url)
{
    CURL* curl;
    CURLcode res;
    std::string readBuffer;

    curl = curl_easy_init();
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);

        if(res != CURLE_OK)
            std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
    }
    return readBuffer;
}

4.3 调用天气 API 示例(OpenWeatherMap)

4.3.1 创建 weather_api.h

// weather_api.h
#ifndef WEATHER_API_H
#define WEATHER_API_H

#include <string>

void getWeatherInfo(const std::string& city, const std::string& apiKey);

#endif // WEATHER_API_H

4.3.2 实现 weather_api.cpp

// weather_api.cpp
#include "weather_api.h"
#include "http_client.h"
#include "json.hpp"
#include <iostream>

using json = nlohmann::json;

void getWeatherInfo(const std::string& city, const std::string& apiKey)
{
    std::string url = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "&appid=" + apiKey + "&units=metric";

    std::string response = sendGetRequest(url);
    json j = json::parse(response);

    if (j.contains("main")) {
        double temp = j["main"]["temp"];
        std::cout << "Current temperature in " << city << ": " << temp << "°C" << std::endl;
    } else {
        std::cout << "Error fetching weather data." << std::endl;
    }
}

4.4 用户登录注册模块

4.4.1 创建 user_manager.h

// user_manager.h
#ifndef USER_MANAGER_H
#define USER_MANAGER_H

#include <string>

bool registerUser(const std::string& username, const std::string& password);
bool loginUser(const std::string& username, const std::string& password);

#endif // USER_MANAGER_H

4.4.2 实现 user_manager.cpp

// user_manager.cpp
#include "user_manager.h"
#include <fstream>
#include <sstream>
#include <unordered_map>
#include <filesystem>

std::unordered_map<std::string, std::string> users;

bool loadUsersFromFile() {
    std::ifstream file("users.db");
    if (!file.is_open()) return false;

    std::string line;
    while (std::getline(file, line)) {
        std::istringstream ss(line);
        std::string username, password;
        if (std::getline(ss, username, ':') && std::getline(ss, password)) {
            users[username] = password;
        }
    }
    file.close();
    return true;
}

bool saveUserToFile(const std::string& username, const std::string& password) {
    std::ofstream file("users.db", std::ios::app);
    if (!file.is_open()) return false;
    file << username << ":" << password << std::endl;
    file.close();
    return true;
}

bool registerUser(const std::string& username, const std::string& password) {
    if (users.find(username) != users.end()) {
        std::cout << "Username already exists!" << std::endl;
        return false;
    }
    if (saveUserToFile(username, password)) {
        users[username] = password;
        std::cout << "Registration successful!" << std::endl;
        return true;
    }
    return false;
}

bool loginUser(const std::string& username, const std::string& password) {
    if (loadUsersFromFile()) {
        auto it = users.find(username);
        if (it != users.end() && it->second == password) {
            std::cout << "Login successful!" << std::endl;
            return true;
        }
        std::cout << "Invalid username or password." << std::endl;
    }
    return false;
}

4.5 主程序入口

4.5.1 创建 main.cpp

// main.cpp
#include <iostream>
#include "weather_api.h"
#include "user_manager.h"

int main()
{
    std::string choice;
    std::cout << "Welcome to Study Helper!\n";
    std::cout << "1. Register\n2. Login\nChoose: ";
    std::cin >> choice;

    std::string username, password;

    if (choice == "1") {
        std::cout << "Enter username: ";
        std::cin >> username;
        std::cout << "Enter password: ";
        std::cin >> password;
        registerUser(username, password);
    } else if (choice == "2") {
        std::cout << "Enter username: ";
        std::cin >> username;
        std::cout << "Enter password: ";
        std::cin >> password;
        if (loginUser(username, password)) {
            std::string city;
            std::cout << "Enter city for weather info: ";
            std::cin >> city;
            getWeatherInfo(city, "YOUR_API_KEY_HERE"); // 替换为你自己的 API Key
        }
    }

    return 0;
}

五、编译与运行

5.1 编译命令

g++ -o study_helper main.cpp http_client.cpp weather_api.cpp user_manager.cpp -lcurl

5.2 运行程序

./study_helper

六、API 接口调用流程图

在这里插入图片描述

七、测试用例与验证结果

测试项 输入数据 预期结果 实际结果 通过状态
注册新用户 username: test, password: pwd 成功写入文件 成功写入
登录成功 username: test, password: pwd 登录成功 成功登录
登录失败 username: wrong, password: pwd 提示用户名或密码错误 正确提示
天气查询正常 city: Beijing 显示北京当前温度 正确显示温度
天气查询失败 city: InvalidCityName 提示错误信息 正确提示

八、小结

  • 本项目基于 C++ 开发实现的大学生学习小助手,通过集成 libcurl 网络库与 nlohmann/json 解析库,构建了具备多场景服务能力的实用工具。

网站公告

今日签到

点亮在社区的每一天
去签到