首页 > 其他分享 >软件构造

软件构造

时间:2024-10-20 18:36:35浏览次数:1  
标签:std 构造 vector file 软件 expressions include line

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;

}

 

 

 

标签:std,构造,vector,file,软件,expressions,include,line
From: https://www.cnblogs.com/yuanxinglan/p/18487598

相关文章

  • 软考论文之论软件维护方法及其应用
    一、论点论据软件维护,就是在软件已经交付使用之后,为了改正错误或满足新的需要而修改软件的过程。可以选择以下4~5种主要的影响软件维护工作的因素,进行论述影响软件维护工作的主要因素有:1、可理解性:通过阅读源代码和文档,了解软件功能和运行的容易程度。2、可测试性:验证软件程......
  • “物品复活”软件开发 PSP数据统计
    计划Planning需求:设计一个功能相对完整、页面相对美观的简单程序。对软件性能不做过高要求。时间成本:我在此之前并未做过软件的开发,进度可能会比较慢,因此希望在作业截止前完成就行。依赖关系:先设计用户数据库、物品信息数据库再进行软件的开发似乎更为合理,但我也没有数......
  • 【AU2024】Adobe 音频编辑和制作软件Audition(简称AU)WIN/MAC下载及使用教程
    AdobeAU软件简介AdobeAudition(简称AU)是一款由Adobe公司开发的音频编辑和制作软件,前身为CoolEditPro,于2003年被Adobe收购并更名为Audition。AU以其强大的音频处理能力、丰富的音频效果和直观的操作界面,广泛应用于配音、电台广播、音乐制作等多个领域。它不仅支持音频的录制......
  • 数码摄影师、图形设计师及高端用户设计的桌面图像编辑和管理软件下载Adobe Lightroom
    目录一、软件概述1.1名称与定位1.2版本与更新1.3系统兼容性二、系统要求2.1最低系统要求2.2推荐系统要求三、下载与安装3.1下载链接3.2安装步骤四、功能介绍4.1照片管理4.2照片编辑4.3高级功能一、软件概述1.1名称与定位AdobeLightroomClassic......
  • 最新微商大师精准引流获客软件,多平台私域裂变拓客
    微商大师全功能营销助手特别推荐28个功能内置防封稳如泰山·自动加群好友·添加附近的人拍照加人·手机号加人图文齐发检测僵尸粉自动通过好友·评论点赞Fans微商大师转发朋友圈克隆朋友圈......
  • 最新微商大师精准引流获客软件,多平台私域裂变拓客
    微商大师全功能营销助手特别推荐28个功能内置防封稳如泰山·自动加群好友·添加附近的人拍照加人·手机号加人图文齐发检测僵尸粉自动通过好友·评论点赞Fans微商大师转发朋友圈克隆朋友圈......
  • 最新微商大师精准引流获客软件,多平台私域裂变拓客
    微商大师全功能营销助手特别推荐28个功能内置防封稳如泰山·自动加群好友·添加附近的人拍照加人·手机号加人图文齐发检测僵尸粉自动通过好友·评论点赞Fans微商大师转发朋友圈克隆朋友圈......
  • 最新微商大师精准引流获客软件,多平台私域裂变拓客
    微商大师全功能营销助手特别推荐28个功能内置防封稳如泰山·自动加群好友·添加附近的人拍照加人·手机号加人图文齐发检测僵尸粉自动通过好友·评论点赞Fans微商大师转发朋友圈克隆朋友圈......
  • 最新微商大师精准引流获客软件,多平台私域裂变拓客
    微商大师全功能营销助手特别推荐28个功能内置防封稳如泰山·自动加群好友·添加附近的人拍照加人·手机号加人图文齐发检测僵尸粉自动通过好友·评论点赞Fans微商大师转发朋友圈克隆朋友圈......
  • 金蝶财务软件SQL数据库恢复
    金蝶财务软件SQL数据库的恢复过程可以根据具体情况采取不同的方法和策略。以下是一些详细的步骤和建议:一、确认备份情况首先,需要确认是否有金蝶数据库的备份存在。备份是数据恢复的基础,没有备份的情况下恢复数据将非常困难。验证备份文件的完整性和时效性,确保备份文件没有损坏且......