软件构造

CSV:

#include <fstream>

#include <iostream>

#include <vector>

#include <string>

 

// 保存到CSV

void saveToCSV(const std::vector<std::string>& expressions, const std::string& filename) {

    std::ofstream file(filename.c_str());

    if (!file.is_open()) {

        std::cerr << "Unable to open file for writing: " << filename << std::endl;

        return;

    }

    for (std::vector<std::string>::const_iterator it = expressions.begin(); it != expressions.end(); ++it) {

        file << *it << std::endl;

    }

    file.close();

}

 

// 从CSV读取

std::vector<std::string> readFromCSV(const std::string& filename) {

    std::vector<std::string> expressions;

    std::ifstream file(filename.c_str());

    if (!file.is_open()) {

        std::cerr << "Unable to open file for reading: " << filename << std::endl;

        return expressions;

    }

    std::string line;

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

        expressions.push_back(line);

    }

    file.close();

    return expressions;

}

 

int main() {

    std::vector<std::string> expressions;

    expressions.push_back("2 + 2=4");

    expressions.push_back("3 * 4=12");

    expressions.push_back("5 - 1=4");

    saveToCSV(expressions, "expressions.csv");

 

    std::vector<std::string> loadedExpressions = readFromCSV("expressions.csv");

    for (std::vector<std::string>::const_iterator it = loadedExpressions.begin(); it != loadedExpressions.end(); ++it) {

        std::cout << *it << std::endl;

    }

 

    return 0;

}

 

 

 

XML:

 

#include <fstream>

#include <iostream>

#include <vector>

#include <string>

 

// 保存到XML

void saveToXML(const std::vector<std::string>& expressions, const std::string& filename) {

    std::ofstream file(filename.c_str()); // 使用 c_str() 方法

    if (!file) {

        std::cerr << "Unable to open file for writing: " << filename << std::endl;

        return;

    }

    file << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;

    file << "<expressions>" << std::endl;

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

        file << "  <expression>" << expressions[i] << "</expression>" << std::endl;

    }

    file << "</expressions>" << std::endl;

    file.close();

}

 

// 从XML读取(简化版,不使用第三方库)

std::vector<std::string> readFromXML(const std::string& filename) {

    std::vector<std::string> expressions;

    std::ifstream file(filename.c_str()); // 使用 c_str() 方法

    if (!file) {

        std::cerr << "Unable to open file for reading: " << filename << std::endl;

        return expressions;

    }

    std::string line;

    std::string currentExpression;

    bool inExpression = false;

    while (getline(file, line)) {

        size_t startPos = line.find("<expression>");

        if (startPos != std::string::npos) {

            inExpression = true;

            // Remove the <expression> tag from the line

            line = line.substr(startPos + 12);

        }

        if (inExpression) {

            // Check if the line contains the closing tag

            size_t endPos = line.find("</expression>");

            if (endPos != std::string::npos) {

                // Extract the expression text and add it to the list

                currentExpression = line.substr(0, endPos);

                expressions.push_back(currentExpression);

                // Reset for the next expression

                currentExpression.clear();

                inExpression = false;

            } else {

                // If no closing tag, append the whole line

                currentExpression += line;

            }

        }

    }

    file.close();

    return expressions;

}

 

int main() {

    std::vector<std::string> expressions;

    expressions.push_back("2 + 2=4");

    expressions.push_back("3 * 4=12");

    expressions.push_back("5 - 1=4");

    saveToXML(expressions, "expressions.xml");

 

    std::vector<std::string> loadedExpressions = readFromXML("expressions.xml");

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

        std::cout << loadedExpressions[i] << std::endl;

    }

 

    return 0;

}

 

 

JSON:

#include <fstream>

#include <iostream>

#include <vector>

#include <string>

#include <cctype>

 

// 保存到JSON

void saveToJSON(const std::vector<std::string>& expressions, const std::string& filename) {

    std::ofstream file(filename.c_str()); // 使用 c_str() 方法

    if (!file) {

        std::cerr << "Unable to open file for writing: " << filename << std::endl;

        return;

    }

    file << " " << std::endl;

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

        file << "  \"" << expressions[i] << "\"";

        if (i < expressions.size() - 1) {

            file << ",";

        }

        file << std::endl;

    }

    file << " " << std::endl;

    file.close();

}

 

// 从JSON读取(简化版,不使用第三方库)

std::vector<std::string> readFromJSON(const std::string& filename) {

    std::vector<std::string> expressions;

    std::ifstream file(filename.c_str()); // 使用 c_str() 方法

    if (!file) {

        std::cerr << "Unable to open file for reading: " << filename << std::endl;

        return expressions;

    }

    std::string currentExpression;

    char ch;

    bool inQuotes = false;

    while (file.get(ch)) {

        if (ch == '"') {

            inQuotes = !inQuotes;

        } else if (ch == ',' && !inQuotes) {

            // Ignore commas outside quotes

            continue;

        } else if (isspace(ch) && !inQuotes) {

            // Ignore whitespace outside quotes

            if (!currentExpression.empty()) {

                expressions.push_back(currentExpression);

                currentExpression.clear();

            }

            continue;

        } else {

            currentExpression += ch;

        }

    }

    if (!currentExpression.empty()) {

        expressions.push_back(currentExpression);

    }

    file.close();

    return expressions;

}

 

int main() {

    std::vector<std::string> expressions;

    expressions.push_back("2 + 2=4");

    expressions.push_back("3 * 4=12");

    expressions.push_back("5 - 1=4");

    saveToJSON(expressions, "expressions.json");

 

    std::vector<std::string> loadedExpressions = readFromJSON("expressions.json");

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

        std::cout << loadedExpressions[i] << std::endl;

    }

 

    return 0;

}

 

 

 

posted @   不会JAVA的小袁  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示