Advertisement
akanoce

Ex.2 5/6/17

Jan 13th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct nodo{char info; nodo* left,*right; nodo(int a=0, nodo*b=0,nodo*c=0){info=a; left=b; right=c;}};
  6.  
  7. //PRE=(albero(r)corretto,k>0 e salto>0).
  8. int stampaConSalti(nodo* r, int k, int salto){
  9.    int resto1,resto2;
  10.    
  11.     if(r){
  12.            
  13.             if(!r->left && !r->right){
  14.             resto1 = k-1;
  15.             if(!resto1){
  16.                 cout<<r->info<<" ";
  17.                 resto1 = salto;
  18.             }//!resto1
  19.             return resto1-1;
  20.            
  21.             }//!r->left && !r->right
  22.            
  23.             if(!r->left )
  24.                 resto1 = k - 1;
  25.             else    
  26.            resto1 = stampaConSalti(r->left, k , salto);
  27.          
  28.                 if(!resto1){
  29.             cout<<r->info<<" ";
  30.             k = salto;
  31.             resto1 = k;
  32.         }  
  33.             if(r->right){
  34.            resto2 = stampaConSalti(r->right,resto1,salto);
  35.          
  36.             if(!resto2){
  37.             cout<<r->info<<" ";
  38.             k = salto;
  39.             resto2 =  k;
  40.            
  41.         }
  42.          
  43.           return resto2;
  44.             }
  45.             else
  46.             return resto1-=1;
  47.     }//r
  48.    
  49. }//stampaConSalti
  50.  
  51. /*POST=(se albero(r)contiene m nodi, allora, la funzione percorrei primi (rispetto all’ordine infisso) salto-1 nodi senza stamparli,
  52. poi stampail nodo seguente, poi percorrei prossimi k-1 senza stamparli, stampa il seguente e quando ha percorso tutti gli m nodi,
  53. restituisce l’intero che indica quanti nodi dovrebbe percorrere per fare la prossima stampa)*/
  54.  
  55. nodo* parse(char*& A)
  56. {
  57.   if(*A>='a' && *A<='z')
  58.     {
  59.       char p=*A;
  60.       if(*(A+1) !='(') throw (1);
  61.       A=A+2;
  62.       nodo*l=parse(A);
  63.       if(*A != ',') throw (2);
  64.       A=A+1;
  65.       nodo*r=parse(A);
  66.       if(*A !=')') throw(3);
  67.      
  68.       A=A+1;
  69.       return new nodo(p,l,r);
  70.     }
  71.   else
  72.     if(*A=='_') {A=A+1;return 0;}
  73.     else
  74.       throw (4);
  75.  
  76.  
  77. }
  78.  
  79. void infix_stampa(nodo* n){
  80.    
  81.    
  82.     if(n){
  83.         infix_stampa(n->left);
  84.         cout<<n->info<<" ";
  85.         infix_stampa(n->right);
  86.     }
  87.    
  88. }//infix_stampa
  89.  
  90. main()
  91. try{
  92.   char A[100],y,*q=A;
  93.   int k;
  94.   cin>>k;
  95.   while(y!='$')
  96.     {
  97.       cin>>y;
  98.       *q=y;
  99.       q++;
  100.     }
  101.   q=A;
  102.   nodo* R=parse(q);
  103.   infix_stampa(R);
  104.   cout<<endl;
  105.   cout<<"start"<<endl;
  106.   int a= stampaConSalti(R,k,k);//da fare
  107.   cout<<endl<<"restituisce "<<a<<endl;
  108.   cout<<"end"<<endl;
  109.  }
  110.  catch(int x)
  111.    {
  112.      switch(x)
  113.        {
  114.        case 1: cout<<"manca ("<<endl;   break;
  115.        case 2: cout<<"manca ," <<endl; break;
  116.        case 3: cout<<"manca )"<<endl;   break;
  117.        case 4: cout<<"non albero"<<endl;   break;
  118.        }
  119.  
  120.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement