Страница 1 из 1

загрузка файла в дерево

Добавлено: 04 май 2010, 16:10
artemi
надо загрузить фаил в дерево и произвести продвижение по дереву на ходя минимальное значение ,также надо ввести инфо в фаил
помогите доделать. моя программа не коректно работает и не могу сделать продвижение по дереву
вот мой код:

Код: Выделить всё

#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <fstream.h>
struct Map{
        int num;
        char gorod[20];
        int rast;
        friend ostream &operator<<(ostream &stream, Map mp){
                stream<<" "<<mp.num<<" "<<mp.gorod<<" "<<mp.rast;
                return stream;
        }
        friend istream &operator>>(istream &stream, Map &mp){
                stream>>mp.num>>mp.gorod>>mp.rast;
                return stream;
        }
};
struct node{
        Map info;
        node *nextl,*nextr;
        node(){
                info.num=info.rast=0;
                nextl=nextr=0;
        }
        node(Map newinfo){
                info=newinfo;
                nextl=nextr=0;
        }
};
template<class T,class T1>class tree{
        public:
        T *root;
        tree() {root=0;}
        void push(T*&wer,T1 dat,int n){
                if (wer==0){
                       try{
                        wer=new T;
                         if(!wer) throw 1;
                        wer->nextl=0;wer->nextr=0;wer->info=dat;
  }
                          catch (int mthrow) {cout<<"No memory!"<<endl;return;}
                }
          else if(n==1)
               if(strcmp(dat.gorod,wer->info.gorod)<0)push(wer->nextl,dat,1);
                        else push(wer->nextr,dat,1);
                else
                        if(dat.rast>wer->info.rast)push(wer->nextl,dat,2);
                        else push(wer->nextr,dat,2);
        }
        void insert(T1 dat,int n){
                if(root==0)root=new T(dat); else push(root,dat,n);
        }
        void look(ostream &stream,T *&wer){
                if(wer!=0){
                        look(stream,wer->nextl);
                        stream<<" "<<wer->info<< endl;
                        look (stream,wer->nextr);
                }
        }
        friend ostream &operator<<(ostream &stream,tree ob)
                { ob.look (stream, ob.root);return stream; }
};
void main(){
  int r;
  do{
  cout<<"1.Sort with names\n";
  cout<<"2.Sort with balls\n";
  cout<<"3.Exit\n";
  cout<<"4.add\n";
  cout<<"5.file\n";
  cin>>r;
  switch (r){
  case 1: {
                 tree<node,Map>q;
                 node *n;
                 ifstream infile("Map.txt");
                 while(!infile.eof()){
                                Map m;
                                infile>>m;
                                q.insert(m,1);
                        }
                  infile.close();
                  cout<<q;
                  break;
                 }
        case 2: {
                 tree<node, Map> q;
                 node *n;
 ifstream infile("map.txt");
                 Map *m;
                 m = new Map;
                 int i = 1;
                 float s = 0;
                 while(!infile.eof()){
                                infile >> m[i];
                                s+=m[i].rast;
                                i++;
                         }
                  for(int j=1;j<=i;j++)
                        if (m[j].rast>s/i)
                                q.insert(m[j],2);
                  infile.close();
                  cprintf("Miide rastoynie is %1.3f",s/i);
                  cout<<'\n'<< q;
                  break;
                 }
        case 3: {return;}
        default: {cout<<"Error! Try again\n"; break;}
 
 
     case 5:
     {
         tree<node,Map>q;
         node *no;
         Map *m;
         int n,i;//рабочая переменная для подсчёта
         cout<<"Kolichestvo zapisey=";cin>>n;
         if(m!=NULL)delete[]m; //очистка рабочей области(массива)
         m=new Map[n];     //выделение динамической памяти
         if(m==NULL){cout<<"Net pamyati\n";getch();n=0;return;}
         //создание базы данных
         for(i=0;i<n;i++){
         cout<<"enter num\n";
         cin>>m[i].num;
         cout<<"enter gorod\n";
         cin>>m[i].gorod;
         cout<<"enter rastoynie\n";
         cin>>m[i].rast;  }
         cout<<"karta sozdana\n" ;
         ofstream outfile("Map.txt",ios: :o ut);
                 while(!outfile.eof()){
                 for(int i=0;i<n;i++){
                                outfile<<setw(5)<<m[i].num<<setw(12)<<m[i].gorod<<setw(10)<<m[i].rast<<endl;
                                }
 
                  outfile.close();
                  cout<<"save file\n";
                  getch();
                        }
                                 break;}
          }
        getch();
        clrscr();  }
  while(r!=6);
  return;}