Advertisement
dany27

Untitled

May 6th, 2012
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.09 KB | None | 0 0
  1. #include <iostream>
  2. #include<sstream>
  3. #include<fstream>
  4. using namespace std;
  5. template<class R> class fa;
  6. template<class T>
  7. class elem{
  8.     friend class fa<T>;
  9.     T adat;
  10.     int hany;
  11.     elem *left;
  12.     elem *right;
  13. public:
  14.     elem(){}
  15.     void preord(){
  16.     if(this!=NULL)
  17.         {
  18.         adat.kiir();
  19.         this->left->preord();
  20.         this->right->preord();
  21.         }
  22.     }
  23.     void preord_save(ostream &os) {
  24.     if(this!=NULL)
  25.         {
  26.         os<<adat;
  27.         this->left->preord_save(os);
  28.         this->right->preord_save(os);
  29.         }
  30.     }
  31.     void feltoltes(istream &is)
  32.     {
  33.         string line;
  34.         while(!is.eof())
  35.         {
  36.             getline(is,line,'\n');
  37.             fa<T>::fa.beszur(adat.convert(line));
  38.         }
  39.     }
  40.     void torol(){
  41.     if(this!=NULL)
  42.     {
  43.         if(this->left!=NULL)
  44.             this->left->torol();
  45.         if(this->right!=NULL)
  46.             this->right->torol();
  47.         delete this;
  48.     }
  49.  
  50.     }
  51.  
  52. };
  53. template<class R>
  54. class fa{
  55.     elem<R> first;
  56.     public:
  57.     fa(){
  58.     first.left=NULL;
  59.     first.right=NULL;
  60.     first.hany=0;
  61.     }
  62.     void beszur(R& x)
  63.     {
  64.         int bale=2;
  65.         elem<R> *q=&first;
  66.         elem<R> *p=&first;
  67.  
  68.         if(q->hany!=0)
  69.         {
  70.             if(q->adat.compare(x)==1)
  71.             {
  72.                 p=q->right;
  73.                 bale=0;
  74.             }
  75.             else if(q->adat.compare(x)==2)
  76.             {
  77.                 p=q->left;
  78.                 bale=1;
  79.             }
  80.             if(bale==2)
  81.                 ++(q->hany);
  82.             else
  83.             {
  84.                 while(p!=NULL)
  85.                     if(p->adat.compare(x)==1)
  86.                     {
  87.                         q=p;
  88.                         p=p->right;
  89.                         bale=0;
  90.                     }
  91.                     else if(p->adat.compare(x)==2)
  92.                     {
  93.                         q=p;
  94.                         p=p->left;
  95.                         bale=1;
  96.                     }
  97.                     else
  98.                         break;
  99.  
  100.                 if(p==NULL)
  101.                 {
  102.                     if(bale)
  103.                     {
  104.                         q->left=new elem<R>;
  105.                         p=q->left;
  106.                     }
  107.                     else
  108.                     {
  109.                         q->right=new elem<R>;
  110.                         p=q->right;
  111.                     }
  112.                     p->adat=x;
  113.                     p->hany=1;
  114.                     p->left=NULL;
  115.                     p->right=NULL;
  116.                 }
  117.                 else
  118.                     ++(p->hany);
  119.             }
  120.         }
  121.         else
  122.         {
  123.             first.adat=x;
  124.             first.hany=1;
  125.         }
  126.     }
  127.     void preorder()
  128.     {
  129.         elem<R> *p=&first;
  130.         p->preord();
  131.     }
  132.     void preorder_save()
  133.     {
  134.         ofstream f1;
  135.         f1.open("mentes.txt");
  136.         elem<R> *p=&first;
  137.         p->preord_save(f1);
  138.         f1.close();
  139.     }
  140.     void feltolt()
  141.     {
  142.         ifstream f1;
  143.         f1.open("mentes.txt");
  144.         elem<R>::elem.feltoltes(f1);
  145.         f1.close();
  146.     }
  147. };
  148.  
  149.  
  150. class student{
  151.     string nev;
  152.     string neptun;
  153.     public:
  154.     student(){}
  155.     student(string nev, string neptun) :nev(nev), neptun(neptun){
  156.     }
  157.     int compare(student kivel)
  158.     {
  159.         if(neptun<kivel.neptun)
  160.             return 1;
  161.         else if(neptun>kivel.neptun)
  162.             return 2;
  163.         else return 0;
  164.     }
  165.  
  166.     student operator=(student ki)
  167.     {
  168.         nev=ki.nev;
  169.         neptun=ki.neptun;
  170.  
  171.         return ki;
  172.     }
  173.     friend std::ostream& operator<<(std::ostream& os, student i) {
  174.         return os <<i.nev<<"  "<<i.neptun<<endl;
  175.     }
  176.     student& convert(string line)
  177.     {
  178.         string adat1, adat2;
  179.         stringstream linestream(line);
  180.         getline(linestream,adat1,'\t');
  181.         getline(linestream,adat2,'\n');
  182.         student vki(adat1,adat2);
  183.         return vki;
  184.     }
  185.     void kiir()
  186.     {
  187.         cout<<"Nev: "<<nev<<" Neptun: "<<neptun<<endl;
  188.     }
  189.     ~student(){}
  190. };
  191.  
  192. class szamok{
  193.     int szam;
  194.  
  195.     public:
  196.     szamok(){}
  197.     szamok(int be) :szam(be){}
  198.     int compare(szamok mivel){
  199.     if(szam<mivel.szam)
  200.         return 1;
  201.     else if(szam>mivel.szam)
  202.         return 2;
  203.     else return 0;
  204.     }
  205.     szamok operator=(szamok mi)
  206.     {
  207.         szam=mi.szam;
  208.  
  209.         return mi;
  210.     }
  211.     friend std::ostream& operator<<(std::ostream& os, szamok i) {
  212.         return os <<i.szam<<endl;
  213.     }
  214.     void kiir()
  215.     {
  216.         cout<<"Szam: "<<szam<<endl;
  217.     }
  218.     ~szamok(){}
  219.  
  220. };
  221.  
  222.  
  223. int main()
  224. {
  225.     fa<student> y;
  226.     fa<student> x;
  227.     ofstream f1;
  228.     //f1.open("mentes.txt");
  229.  
  230.    y.beszur(*(new student("dsfdsf","KSDFGH")));
  231.    y.beszur(*(new student("dsfdsf","XFDFDF")));
  232.    y.beszur(*(new student("dsfdsf","CVDFDF")));
  233.    /*x.beszur(*(new szamok(32)));
  234.    x.beszur(*(new szamok(25)));
  235.    x.beszur(*(new szamok(45)));
  236.    x.beszur(*(new szamok(12)));*/
  237.     y.preorder();
  238.     //x.preorder();
  239.  
  240.     y.preorder_save();
  241.     x.feltolt();
  242.     x.preorder();
  243.     //f1.close();
  244.     return 0;
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement