xml-txt(c++)

该文章展示了一个C++程序,用于读取包含学生信息的XML文件,按照学生的成绩从高到低排序,然后将排序后的结果写入到一个txt文件中。程序通过查找XML标签位置提取信息,并使用自定义的比较函数对成绩进行排序。
摘要由CSDN通过智能技术生成

xml文本如下,读入xml文本,按成绩大小由大到小排序输出成txt

<?xml version="1.0"?>
<grades>
    <grade>
       <id>2019001</id>
       <name>张三</name>
       <course>机器学习</course>
       <score>85</score>
    </grade>
    <grade>
       <id>2019002</id>
       <name>李四</name>
       <course>操作系统</course>
       <score>90</score>
    </grade>
    <grade>
       <id>2019003</id>
       <name>王五</name>
       <course>数据结构</course>
       <score>95</score>
    </grade>
    <grade>
       <id>2019003</id>
       <name>刘六</name>
       <course>数据结构</course>
       <score>68</score>
    </grade>
    <grade>
       <id>2019003</id>
       <name>初七</name>
       <course>数据结构</course>
       <score>74</score>
    </grade>
    <grade>
       <id>2019003</id>
       <name>朱八</name>
       <course>数据结构</course>
       <score>100</score>
    </grade>
    <grade>
       <id>2019003</id>
       <name>李九</name>
       <course>数据结构</course>
       <score>69</score>
    </grade>
</grades>
  1. 创建xml文件

  1. 打开vs,新建c++空项目

  1. 添加---新建项---WEB---xml文件--添加

  1. 将xml文本放入,保存---生成xml文档

2.在.cpp文件中输入如下代码

c++代码

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

using namespace std;

//声明5个函数
int find_id(const string&);//查找id所在
int find_name(const string&);
int find_course(const string&);
int find_score(const string&);
int ASCALL_TO_INT(const char&);//成绩转换为int型,便于比较
/*另外Sortstruct同样可以进行字符串比较
*比如a.strlen() 和 b.strlen() 大小
*相等则一位位比较
*/


/*学生相关信息类*/
class student {
public:
    string id;
    string name;
    string course;
    int score;
};
//声明函数
bool Sortstruct(const student*, const student*); //vector的比较函数

/*主函数*/
int main()
{
    ifstream ifs;//创建读文件对象
    ofstream ofs;//创建写文件独享
    ifs.open("D:/研究生复试李泽平/test.xml", ios::in);
    ofs.open("D:/研究生复试李泽平/test.txt", ios::out | ios::ate);
    vector<student*> stuptrs;//创建vector容器保存学生类对象指针

    string a; 
    char temp[20];
    memset(temp, '\0', 20);//初始化temp字符串,20个字节 变成\0
    while (getline(ifs, a)) //若到文本末则退出
    {
        int i = find_id(a);//调用find函数找到id所在行
        if (i == -1)
            continue;//如果i=-1;表示当前行不存在id则直接进行下一轮
  
        student *stuptr = new student;
        stuptrs.push_back(stuptr); //尾部扩容
        /*拷贝ID*/
        for (int j = 0; j < 7; j++)    //7位学号
            temp[j] = a[i + j];
        stuptr->id = temp;//将temp中的学号保存至当前学生对象
        memset(temp, '\0', 20);//初始化temp;

        /*拷贝名字*/
        getline(ifs, a);
        i = find_name(a);
        for (int j = 0; a[i + j] != '<'; j++)
            temp[j] = a[i + j];
        stuptr->name = temp;
        memset(temp, '\0', 20);

        /*拷贝课程*/
        getline(ifs, a);
        i = find_course(a);
        for (int j = 0; a[i + j] != '<'; j++)
            temp[j] = a[i + j];
        stuptr->course = temp;
        memset(temp, '\0', 20);

        /*拷贝分数*/
        getline(ifs, a);
        i = find_score(a);
        for (int j = 0; a[i + j] != '<'; j++)
            temp[j] = a[i + j];
        if (temp[1] == '\0')//调用转换函数确定分数
            stuptr->score = ASCALL_TO_INT(temp[0]);
        else if (temp[2] == '\0')
            stuptr->score = ASCALL_TO_INT(temp[0]) * 10 + ASCALL_TO_INT(temp[1]);
        else
            stuptr->score = 100;
        memset(temp, '\0', 20);

    }

    //调用algorithm头文件中的sort函数,按Sortstruct规则排序
    sort(stuptrs.begin(), stuptrs.end(), Sortstruct);

    /*遍历写入txt并删除新建的结构体内存*/
    for (auto n : stuptrs) {
        ofs << n->id << ',' << n->name << ',' << n->course << ',' << n->score << endl;
        delete n;
    }

    return 0;
}

int find_id(const string& a) {
    int flag = -1;
    int i = 0;
    for (i; a[i + 3] != '\0'; i++) {
        if (a[i] == '<' && a[i + 1] == 'i' && a[i + 2] == 'd' && a[i + 3] == '>') {
            flag = i + 4;
            break;
        }
    }
    return flag;
}
int find_name(const string& a) {
    int flag = -1;
    int i = 0;
    for (i; a[i + 5] != '\0'; i++) {
        if (a[i] == '<' && a[i + 1] == 'n' && a[i + 2] == 'a' && a[i + 3] == 'm' && a[i + 4] == 'e' && a[i + 5] == '>') {
            flag = i + 6;
            break;
        }
    }
    return flag;
}
int find_course(const string& a) {
    int flag = -1;
    int i = 0;
    for (i; a[i + 7] != '\0'; i++) {
        if (a[i] == '<' && a[i + 1] == 'c' && a[i + 2] == 'o' && a[i + 3] == 'u' && a[i + 4] == 'r' && a[i + 5] == 's' && a[i + 6] == 'e' && a[i + 7] == '>') {
            flag = i + 8;
            break;
        }
    }
    return flag;
}
int find_score(const string& a) {
    int flag = -1;
    int i = 0;
    for (i; a[i + 6] != '\0'; i++) {
        if (a[i] == '<' && a[i + 1] == 's' && a[i + 2] == 'c' && a[i + 3] == 'o' && a[i + 4] == 'r' && a[i + 5] == 'e' && a[i + 6] == '>') {
            flag = i + 7;
            break;
        }
    }
    return flag;
}

int ASCALL_TO_INT(const char& ASCALL)
{
    return ASCALL - '0';
}

//sort函数的比较函数
bool Sortstruct(const student* stu1, const student* stu2)
{
    return stu1->score > stu2->score;//降序为真
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值