A
Hallo zusammen,
Lang ist's her, doch nun habe ich bei diesem Thema wieder Zeit gefunden weiterzuarbeiten
Habe folgenden Code:
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <fstream>
using namespace std;
class myItem {
public:
myItem(int T, int S, int I, const string& L): Time(T), Section(S), Id(I), Line(L) {}
int Time, Section, Id;
string Line;
bool operator < (const myItem& o) const {
if (Section != o.Section) return Section < o.Section;
if (Id != o.Id) return Id < o.Id;
return Time < o.Time;
}
};
int main() {
string line;
string header1 = "FORCES FROM REACTIONS AT POINT TIES:"
string header2 = "FORCES FROM REACTIONS AT RESTRAINED FACES :"
vector <string> Sections, TableHeads;
vector <myItem> AllData;
int curTime, curSection, curId;
// INPUT
while (getline(cin,line).good()) {
cerr << line << endl;
char dummy;
if (sscanf(line.c_str()," SOLUTION AT RAMP POINT %*d TIME %d",&curTime) == 1) {
// A new ramp point and time, restart sections
curSection = -1;
} else if (line.find(':') != string::npos) {
// A new heading
string tablehead;
getline(cin,tablehead);
curSection++;
if (Sections.size() <= curSection) {
Sections.push_back(line);
TableHeads.push_back(tablehead);
}
} else if (line.find_first_not_of("- \r\n\t")==string::npos) {
// ignore line
} else {
// data line
int curId = line[0] == '*'? (~0u)>>1 : atoi(line.c_str());
AllData.push_back(myItem(curTime,curSection,curId,line));
}
}
// SORT
sort(AllData.begin(), AllData.end());
// OUTPUT
std::ofstream output ("//gt//home//h113027//GT26//forces_moments//final1//output.txt");
curSection = -1; curId = -1;
for (vector<myItem>::iterator i=AllData.begin(); i!= AllData.end(); i++) {
if (i->Section != curSection) {
curSection = i->Section;
curId = -1;
output << Sections[curSection].c_str() << std::endl;
}
if (i->Id != curId) {
curId = i->Id;
output << "\nTime " << TableHeads[curSection].c_str() << std::endl;
}
output << i->Time << std::endl << i->Line.c_str() << std::endl;
}
output.close();
}
Was der Code macht ist folgendes: Habe das gleiche input file wie früher:
http://cid-daa1966be9348935.skydrive.live.com/embedicon.aspx/.Public
Der Code sucht nun nach dem ":" was ja am Ende der Überschrift jedes Datenpakets steht (z.B. "Forces from...Loads:"),
Danach liest es die darunterliegenden Daten, sortiert sie und gibt sie ins output file aus. Soweit so gut
Doch nun möchte ich nicht unbedingt alle Daten sortieren und ausgeben, sondern nur beispielsweise "Forces from Reactions at Point Ties:"
Dazu habe ich ein String mit dem Titel definiert. Jetzt brauche ich noch den entsprechenden Befehl in der Zeile 35.
Kenn mich leider nicht so gut aus, aber denke mit einer Bool-Abfrage - wenn der vordefinierte Titel gelesen wurde, dann Daten lesen, sonst überspringen - kann man das lösen.
Bin ich da komplett falsch oder gibt es eine bessere Möglichkeit?
Vielen Dank schon mal für euer Feedback...
Grüsse