001
15.10.2009, 15:35 Uhr
Windalf
Der wo fast so viele Posts wie FloSoft... (Operator)
|
Hallo,
mein Programmiererzeit ist schon ein bisschen her aber vom Prinzip her hatte ich mal angefangen sowas zu basteln. Die aktuell Version habe ich auf irgend einer gebrannten DVD liegen. Aus nem alten Programm habe ich sowas mal gemacht. Damit konnte man sich relativ schnell weiter sowas was du planst zusammenschustern
C++: |
#include <iostream> #include <vector> #include <fstream> #include <string>
template <int n> class Record{ public: Record(); int record_size(); void operator=(Record r); std::string& operator[](int index); friend std::ostream& operator<<(std::ostream& , Record&); friend std::istream& operator>> (std::istream&, Record&); private: std::vector<std::string> record; };
//------------------------------------------------------------------------------------------- template <int n> Record<n>::Record(){record.resize(n);}
//------------------------------------------------------------------------------------------- template <int n> int Record<n>::record_size(){return record.size();}
//------------------------------------------------------------------------------------------- template <int n> std::string& Record<n>::operator[](int index){return this->record[index];}
//------------------------------------------------------------------------------------------- template <int n> void Record<n>::operator=(Record<n> r){ for(int i=0;i<n;++i)record[i]=r.record[i]; }
//------------------------------------------------------------------------------------------- template <int n> std::ostream& operator<<(std::ostream& os,Record<n>& r){ for(int i=0;i<r.record.size()-1;++i) os<<r.record[i]<<"\t"; os<<r.record[i]; return os; }
//------------------------------------------------------------------------------------------- template <int n> std::istream& operator>>(std::istream& is, Record<n>& r){ for(int i=0;i<r.record.size();++i) is>>r.record[i]; return is; }
//------------------------------------------------------------------------------------------- template <class Typ> class txtDB{
public: txtDB(const char* filenname); // ~txtDB(){}; bool save(const char* filename=NULL);
bool insert_record(Typ &record); bool update_record(Typ &record); int delete_record(std::string, int); bool exist_record(std::string, int); std::vector<int> select_record(std::string, int);
int db_size(); txtDB<Typ>& operator++(); txtDB<Typ>& operator--(); Typ& operator[](int); friend std::ostream& operator<<(std::ostream&,txtDB<Typ>&); friend std::istream& operator<<(std::istream&,txtDB<Typ>&);
private: std::string filename; std::vector<Typ> db; // std::vector<int> prim_keys;
};
//------------------------------------------------------------------------------------------- template <class Typ> txtDB<Typ>::txtDB(const char* filenname){
this->filename=filename; std::ifstream is(filenname); if(is.is_open()){ is>>*this; is.close(); } }
//------------------------------------------------------------------------------------------- template <class Typ> bool txtDB<Typ>::save(const char* filename){
if(!filename)filename=this->filename.c_str(); std::ofstream os(filename); if(os.is_open()){ os<<*this; os.close(); return true; } return false; }
//------------------------------------------------------------------------------------------- template <class Typ> bool txtDB<Typ>::insert_record(Typ &record){
int i,j; for(i=0;i<this->db.size()&&db[i][0]!=record[0];++i); // for(j=0;j<prim_keys.size();++j)
if(i==this->db.size()){ db.push_back(record); return true; } return false; }
//------------------------------------------------------------------------------------------- template <class Typ> int txtDB<Typ>::delete_record(std::string equal_conditon, int index){
int i,rv; std::vector<Typ>::iterator p;
for(i=rv=0;i<db_size();) if(db[i][index]==equal_conditon){ p=db.begin()+i; db.erase(p); ++rv; }else ++i; return rv; }
//------------------------------------------------------------------------------------------- template <class Typ> bool txtDB<Typ>::update_record(Typ &record){
int i; for(i=0;i<this->db.size()&&db[i][0]!=record[0];++i); if(i==this->db.size())return false;
db[i]=record; return true;
}
//------------------------------------------------------------------------------------------- template <class Typ> std::vector<int> txtDB<Typ>::select_record(std::string equal_conditon, int index){
int i; std::vector<int>rv; for(i=0;i<this->db_size();++i) if(db[i][index]==equal_conditon)rv.push_back(i); return rv;
}
//------------------------------------------------------------------------------------------- template <class Typ> bool txtDB<Typ>::exist_record(std::string equal_conditon, int index){
return select_record(equal_conditon,index).size()>0;
return true; }
//------------------------------------------------------------------------------------------- template <class Typ> int txtDB<Typ>::db_size(){return this->db.size();}
//------------------------------------------------------------------------------------------- template <class Typ> txtDB<Typ>& txtDB<Typ>::operator++(){Typ t;this->db.push_back(t);return *this;}
//------------------------------------------------------------------------------------------- template <class Typ> txtDB<Typ>& txtDB<Typ>::operator--(){if(!db.size())this->db.pop_back();return *this;}
//------------------------------------------------------------------------------------------- template <class Typ> Typ& txtDB<Typ>::operator[](int index){return this->db[index];}
//------------------------------------------------------------------------------------------- template <class Typ> std::ostream& operator<<(std::ostream& os, txtDB<Typ>& tdb){ for(int i=0;i<tdb.db_size()-1;++i) os<<tdb[i]<<std::endl; os<<tdb[i]; return os; }
//------------------------------------------------------------------------------------------- template <class Typ> std::istream& operator>>(std::istream& is, txtDB<Typ>& tdb){ for(int i=0;!is.eof();++i){ ++tdb; is>>tdb[i]; } return is; }
int main(){
//Record<3> mr; //mr[0]="41775";mr[1]="100123";mr[2]="Tekken";
txtDB<Record<3> > db("test.txt");
//db.insert_record(mr); //mr[0]="41772"; //db.insert_record(mr); //db.insert_record(mr);
std::cout<<db.db_size()<<std::endl; std::cout<<db<<std::endl; std::cout<<std::endl<<std::endl;
std::vector<int> abc=db.select_record("5",0); for(int i=0;i<abc.size();++i)std::cout<<db[abc[i]]<<std::endl; std::cout<<std::endl<<std::endl;
if(db.exist_record("Tekken",2))std::cout<<"Ja"<<std::endl; else std::cout<<"Nein"<<std::endl;
int x=db.delete_record("Tekken",2); db--; db--; std::cout<<db.db_size()<<std::endl; std::cout<<db<<std::endl; std::cout<<"datensätze gelöscht: "<<x<<std::endl<<std::endl;
//db.save("test.txt"); //db.save();
return 0; }
/* std::string line; bool input_okay = false; double zahl; while(!input_okay) { std::cout<<"Eingabe: "<<std::flush; std::getline(std::cin, line); std::stringstream strm(line); strm>>zahl; if (!strm) { }else { input_okay = true; } } */
/* std::istream &operator>> (std::istream &is, Record &r) { is >> r.a; if(is.peek() != '\t') { is.setstate(std::ios_base::badbit); } else { is.get(); // is >> r.str; } return is; } */
|
-- ...fleißig wie zwei Weißbrote |