You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++入门:根据Item ID提取文件中对应商品价格与名称

根据Item ID提取对应商品价格与名称的C++实现方案

对于你这种按固定格式存储的商品数据,最直接的做法是先把文件中的所有商品信息加载到一个键值映射中,用Item ID作为键,关联对应的价格和名称。之后就可以通过输入ID快速查询。

步骤拆解

  • 定义数据结构:用结构体封装每个商品的价格和名称,方便后续存储和访问。
  • 读取并解析文件:按文件格式(每3行一组商品,空行分隔)读取内容,处理注释行,把解析后的ID、价格、名称存入映射。
  • 查询交互:接收用户输入的ID,从映射中查找并输出结果。

完整示例代码

#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <algorithm>

// 存储商品信息的结构体
struct ItemInfo {
    double price;
    std::string name;
};

// 去除字符串中的注释内容
std::string removeComment(const std::string& line) {
    size_t commentPos = line.find("//");
    if (commentPos != std::string::npos) {
        return line.substr(0, commentPos);
    }
    return line;
}

// 去除字符串首尾空白字符
std::string trim(const std::string& line) {
    size_t start = line.find_first_not_of(" \t\n\r");
    size_t end = line.find_last_not_of(" \t\n\r");
    if (start == std::string::npos || end == std::string::npos) {
        return "";
    }
    return line.substr(start, end - start + 1);
}

int main() {
    std::map<std::string, ItemInfo> itemMap;
    std::ifstream file("Item Documentation.txt");

    if (!file.is_open()) {
        std::cerr << "无法打开文件!" << std::endl;
        return 1;
    }

    std::string line;
    std::string currentId;
    ItemInfo currentItem;
    int lineCounter = 0; // 跟踪当前是一组数据的第几行(0:ID,1:价格,2:名称)

    while (std::getline(file, line)) {
        std::string cleanedLine = trim(removeComment(line));
        if (cleanedLine.empty()) {
            // 空行作为商品分隔符,重置计数器
            lineCounter = 0;
            continue;
        }

        switch (lineCounter) {
            case 0:
                currentId = cleanedLine;
                lineCounter++;
                break;
            case 1:
                currentItem.price = std::stod(cleanedLine);
                lineCounter++;
                break;
            case 2:
                currentItem.name = cleanedLine;
                itemMap[currentId] = currentItem;
                lineCounter = 0; // 重置,准备下一组商品
                break;
        }
    }

    file.close();

    // 交互查询
    std::string inputId;
    std::cout << "请输入要查询的Item ID: ";
    std::cin >> inputId;

    auto it = itemMap.find(inputId);
    if (it != itemMap.end()) {
        std::cout << "商品名称: " << it->second.name << std::endl;
        std::cout << "商品价格: " << it->second.price << std::endl;
    } else {
        std::cout << "未找到ID为" << inputId << "的商品!" << std::endl;
    }

    return 0;
}

代码说明

  1. 结构体ItemInfo:把价格(double)和名称(string)打包,对应商品核心信息。
  2. 文件解析逻辑:
    • 用removeComment清理行内注释,trim去掉首尾空白(换行、空格等)。
    • 通过lineCounter跟踪当前读取的是一组商品中的第几行,分别对应ID、价格、名称。
    • 空行作为商品组的分隔符,遇到后重置计数器,开始处理下一个商品。
  3. 查询逻辑:用std::map存储ID与商品信息的映射,find方法快速定位;如果数据量极大,可换成std::unordered_map实现O(1)查找。
  4. 错误处理:先检查文件是否打开成功,查询时判断ID是否存在并给出明确提示。

注意事项

  • 确保文件路径正确:如果程序和「Item Documentation.txt」不在同一目录,需填写完整路径(如"C:/docs/Item Documentation.txt")。
  • 输入ID需与文件格式完全一致(比如文件中是0001,输入时不能只输1),若需支持模糊匹配,可在读取ID时统一格式(如去除前导零)。

内容的提问来源于stack exchange,提问作者Cyrus Bellefeuille

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 13:00:56