#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;//降序为真 }