Code:
// file: nove_klase.h
#ifndef NOVE_KLASE_H_INCLUDED
#define NOVE_KLASE_H_INCLUDED
#include <vector>
#include <string>
// Pojednostavljene verzije klasa ....
class Point {
//private: // u cilju pojednostavljenja
public:
double x; // moses ih kasnije staviti u private
double y; // ako napravis inspector i mutator member functions
Point(const double& xx=0.0, const double& yy=0.0):x(xx), y(yy){}
Point(const Point& p);
~Point(){}
};
class PolyLine {
//private: // opet pojednostavljenje
public:
int id;
std::vector<Point> mPoint;
PolyLine(const int& i):id(i){}
~PolyLine();
void AddPoint(const double& x, const double& y);
};
// Default delimiteri su "=" i " "
int MyTokenizer(const std::string& str, std::vector<std::string>& tokens, const std::string& delims="= ");
#endif // NOVE_KLASE_H_INCLUDED
// end of file: nove_klase.h
Code:
// file: nove_klase.cpp
#include <iostream>
#include "nove_klase.h"
Point::Point(const Point& p){
x = p.x;
y = p.y;
}
void PolyLine::AddPoint(const double& x, const double& y){
mPoint.push_back( Point(x,y) );
}
PolyLine::~PolyLine(){
if (!mPoint.empty()) mPoint.clear();
}
int MyTokenizer(const std::string& str, std::vector<std::string>& tokens, const std::string& delims) {
/*
modifikovana verzija orginala:
http://www.rosettacode.org/wiki/Tokenizing_A_String#C.2B.2B
*/
using namespace std;
string::size_type lastPos = str.find_first_not_of(delims, 0);
string::size_type pos = str.find_first_of(delims, lastPos);
size_t count(0);
while ( (string::npos != pos || string::npos != lastPos) && count<3 ) {
tokens[count] = str.substr(lastPos, pos - lastPos);
lastPos = str.find_first_not_of(delims, pos);
pos = str.find_first_of(delims, lastPos);
count++;
}
if ( tokens[0].compare("PtCnt") == 0 ) return 0; // pocetak nove PoluLine
else if (tokens[0].substr(0,2).compare("Pt")==0) return 1; // nova tacka u PoluLine
else return -1;
}
// end of file: nove_klase.cpp
Nacin uptrebe:
Code:
// file: program.cpp
#include <iostream>
#include <fstream>
#include <vector>
#include "nove_klase.h"
using namespace std;
int main() {
int odgovor;
vector<PolyLine> mPolyLine;
vector<string> token(3,"");
string line, delims = "= ";
ifstream in_file ("test.txt");
if (in_file.is_open()) {
while (! in_file.eof() ) {
getline (in_file,line);
odgovor = MyTokenizer(line, token, delims);
if (odgovor==0)
mPolyLine.push_back( PolyLine( atoi(token[1].c_str()) ) );
else if (odgovor==1)
mPolyLine[mPolyLine.size()-1].AddPoint( atof(token[1].c_str()), atof(token[2].c_str()) );
// Ocisti tokene
for (size_t t=0;t<token.size(); t++) token[t].clear();
}
in_file.close();
}
// Sada kad imas Konture kao objekte - mozes sa njima raditi sta hoces!
for (size_t p=0; p<mPolyLine.size(); p++) {
cout << "ID Konture = " << mPolyLine[p].id << endl;
for (size_t t=0; t<mPolyLine[p].mPoint.size(); t++) {
cout << " (" << mPolyLine[p].mPoint[t].x << ", " << mPolyLine[p].mPoint[t].y << ")\n";
}
}
return 0;
}
// end of file: program.cpp
Test podaci (fajla "test.txt") - nije orginal, ali po tvojoj prici izgleda priblizno ovako:
Code:
[Version]
VersionLevel=4
[Konturs]
KonturCnt=1
[Kontur0]
PtCnt=450
Pt0=672.000 2661.000
Pt1=725.000 2651.000
Pt2=775.000 2636.000
Pt3=811.000 2632.000
Pt4=817.000 2624.000
[Version]
VersionLevel=4
[Konturs]
KonturCnt=1
[Kontur0]
PtCnt=494
Pt0=505.000 2716.000
Pt1=554.000 2681.000
Pt2=589.000 2664.000
Pt3=648.000 2617.000
Pt4=648.000 2557.000
idb