1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
/*
//str2int convert a string to an integer
int str2int(string s){
int n=atoi(s.c_str());
return n;
}
(+*/
//To keep track of the input postion and the input data
class Buffer{
private:
string s;
int pos;
public:
//To advance pos to where the next tag begins (on the <)
int skipToTag(){
while(pos<s.size() and s[pos]!='<')
pos++;
return pos;
}
//Advance pos to immediately after the next tag
//precondition: pos is positioned outside of a tag
int skipPastTag(){
while(pos<s.size() &&s[pos]!='>') pos++;
pos++; //advance past the '>'
return pos;
}
char get(){
if(pos<s.length()){
return s[pos++];
}
return '\0'; //return null character of eob
}
string substr(int start, int finish){ //from [start,finish]
string result = "";
if(finish<s.size())
while (start<finish){
result = result + s[start];
start++;
}
return result;
}
bool eob(){
return pos >=s.length();
}
void read(istream &fin){
s = "";
char ch;
while(fin.get(ch)){
s = s + ch;
}
}
void write(ostream &fout){
fout << s;
}
}buffer; //Declare the buffer -- as a global variable!
string getTag (Buffer &buffer){
string result = "";
buffer.skipToTag(); //positioned at '<'
char ch;
buffer.get(); //skip the '<'
if(buffer.eob())
return result;
else {
ch = buffer.get();
while(!buffer.eob() and ch!='>'){
result = result + ch;
ch = buffer.get();
}
buffer.get(); //advance past the '>'
return result;
}
}
class Person{
protected:
string first;
string last;
public:
Person(){first= ""; last = "";}
Person(string first, string last){
this-> first = first; this-> last = last;
}
string getField() const {return last;}
// to read the xml
void read(){
int start = buffer.skipPastTag(); // to skip past the <>
int finish = buffer.skipToTag(); // skip to </>
first = (buffer.substr(start, finish));
buffer.skipPastTag(); //skip past </>
start = buffer.skipPastTag(); //skip past <
finish = buffer.skipToTag(); //skip to </>
last =(buffer.substr(start, finish));
//to throw the rest away
buffer.skipPastTag();
buffer.skipPastTag();
}
void write(ostream &fout){
fout << "<Person>" << endl;
fout << " <First>" << first << "</First>" << endl;
fout << " <Last>" << last << "</Last>" << endl;
fout << "</Person>" << endl;
}
};
class Student : public Person{
private:
Person t1;
string s;
string major;
string gpa;
public:
Student(Person t1, string s){
this -> t1 = t1; this -> s = s;
}
string getField() const {return last;}
void read(){
buffer.skipPastTag(); //<student>
t1.read();
int start = buffer.skipPastTag(); //skip past string
int finish = buffer.skipToTag(); //skip to /string
major = buffer.substr(start, finish); //read the string
buffer.skipPastTag();
start = buffer.skipPastTag();
finish = buffer.skipToTag();
gpa = buffer.substr(start, finish);
buffer.skipPastTag();
buffer.skipPastTag();
}
void write(ostream &fout){
fout << "<Student>" << endl;
t1.write(fout);
fout << " <Major>" << major << "</Major>" << endl;
fout << " <GPA>" << gpa << "<GPA>" << endl;
fout << "</Student>" << endl;
}
};
class Professor : public Person{
private:
Person t1;
string s;
string discipline;
string rank;
public:
Professor(Person t1, string s){
this -> t1 = t1; this -> s = s;
}
string getField() const {
return last;
}
void read(){
buffer.skipPastTag();
t1.read();
int start = buffer.skipPastTag();
int finish = buffer.skipToTag();
discipline = buffer.substr(start, finish);
buffer.skipPastTag();
start = buffer.skipPastTag();
finish = buffer.skipToTag();
rank = buffer.substr(start, finish);
buffer.skipPastTag();
buffer.skipPastTag();
}
void write(ostream &fout){
fout << "<Professor>" << endl;
t1.write(fout);
fout << " <Discipline>" << discipline << "</Discipline>" << endl;
fout << " <Rank>" << rank << "</Rank>" << endl;
}
};
bool compare1(const Student &s, const Student &t){
return s.getField()<t.getField();
}
bool compare2(const Professor &s, const Professor &t){
return s.getField()<t.getField();
}
void print1(const vector <Student> &v){
int i = 1;
cout << "Printing Person vector contents:" << endl;
for(auto it: v){
cout << "Record " << i << ":" << endl;
it.write(cout);
i++;
}
cout << endl;
}
void print2(const vector <Professor> &v){
int i = 1;
cout << "Printing Student vector contents: " << endl;
for(auto it: v) {
cout << "Record" << i << ":" << endl;
it.write(cout);
i++;
}
cout << endl;
}
int main(){
ifstream fin ("info.xml");
cout << "info.xml contains: " << endl << endl;
buffer.write(cout);
cout << "Reading info.xml, reconstructing the info and priting it back out: " << endl;
vector <Student> studentRecords;
vector <Professor> profRecords;
bool done = false;
string tag = getTag(buffer);
do{
cout << "\nFound recorded object " << tag << ":" << endl;
if(tag == "Student"){
student.read(); //read the rest of the record
studentRecords.push_back(Student);
}
else if(tag == "Professor"){
professor.read();
profRecords.push_back(Professor);
}
else
done = true; // no tag found
tag = getTag(buffer);
}while (!buffer.eob() && !done);
sort (studentRecords.begin(), studentRecords.end(), compare1);
sort (profRecords.begin(), profRecords.end(), compare2);
print1(studentRecords);
print2(profRecords);
return 0;
}
|