<?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>
XML檔案。
源代碼。
#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:/1/test.xml", ios::in);
ofs.open("c:/1/test.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;//降序為真
}
大家看看,誰能編譯成功,在DEV C++要怎么修改才行,可以貼出修改的源代碼,謝謝各位大神。
uj5u.com熱心網友回復:
現成的xml組件為何不用?轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/280756.html
標籤:C++ 語言
上一篇:C++怎么添加exe資源?
