987ssxml

该C++代码实现从XML文件中提取学生成绩信息,包括ID、姓名、课程和分数,然后利用自定义的查找函数和排序函数对数据进行处理,最终按照成绩降序将结果写入TXT文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

using namespace std;

/*实际上四个find函数可合并为一个函数,只需增加两个形参strbegin和strend使用字符串匹配算法即可*/
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*); //配合sort函数指定vector排序规则

/*主函数*/
int main()
{
	ifstream ifs;//创建读文件对象
	ofstream ofs;//创建写文件独享
	ifs.open("C:/Users/XDFG/Desktop/xml处理/学生成绩.xml", ios::in);
	ofs.open("C:/Users/XDFG/Desktop/xml处理/学生成绩.txt", ios::out | ios::ate);
	vector<student*> stuptrs;//创建vector容器保存学生类对象指针

	string a;
	char temp[20];
	memset(temp, '\0', 20);//初始化temp字符串
	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++)
			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';
}

bool Sortstruct(const student* stu1, const student* stu2)
{
	return stu1->score > stu2->score;//降序为真
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值