Advertisement
RiccardoMontagnin

Esame 31 Marzo 2013

Jun 21st, 2015
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<iostream>
  2. #include<fstream>
  3. using namespace std;
  4.  
  5. /////////////////////////////////////////////////////////////////////////
  6. ///////////////////////////  FUNZIONI DATE  /////////////////////////////
  7. /////////////////////////////////////////////////////////////////////////
  8.  
  9. struct nodo{
  10.    
  11.     int info1, info2;
  12.     nodo* next;
  13.    
  14.     nodo(int a=0, int c=0, nodo* b=0){
  15.        
  16.         info1=a;
  17.         info2=c;
  18.         next=b;
  19.        
  20.     }
  21.    
  22. };
  23.  
  24. nodo* copia(nodo* X){
  25.  
  26.     if(X){
  27.        
  28.         return new nodo(X->info1, X->info2, copia(X->next));
  29.        
  30.     }
  31.    
  32.     return 0;
  33.  
  34. }
  35.  
  36. void crea(nodo* &L, int dim, ifstream & INP, int n){
  37.  
  38.     int x;
  39.    
  40.     if(n<dim){
  41.      
  42.         INP>>x;
  43.         L=new nodo(x,n,0);
  44.         crea(L->next, dim,INP,n+1);
  45.     }
  46.    
  47.     else{
  48.        
  49.         L=0;
  50.        
  51.     }
  52.    
  53. }
  54.  
  55. void crea1(nodo*&L, int dim, ifstream & INP, int n){
  56.    
  57.     int x,y;
  58.  
  59.     if(n<dim){
  60.      
  61.         INP>>x>>y;
  62.         cout<<"x="<<x<<" y="<<y<<endl;
  63.         L=new nodo(x,y,0);
  64.         crea1(L->next, dim,INP,n+1);
  65.      
  66.     }
  67.    
  68.     else{
  69.        
  70.         L=0;
  71.        
  72.     }
  73. }
  74.  
  75. void stampa(nodo* x, ofstream & OUT){
  76.    
  77.     if(x){
  78.      
  79.         OUT<<'('<< x->info1<<','<<x->info2<<')';
  80.         if(x->next)
  81.         OUT<<"->";
  82.         cout<< x->info1<<' '<<x->info2<<' ';
  83.         stampa(x->next, OUT);
  84.        
  85.     }
  86.    
  87.     else{
  88.        
  89.         OUT<<endl;
  90.         cout<<endl;
  91.        
  92.     }
  93.    
  94. }
  95.  
  96.  
  97. /////////////////////////////////////////////////////////////////////////
  98. /////////////////////////  FUNZIONI CREATE  /////////////////////////////
  99. /////////////////////////////////////////////////////////////////////////
  100.  
  101.  
  102. //Controlla a partire da T(x, indiceT) in che nodo di T inizia un match di P[indiceP] && Restituisce -1 se non vi è match
  103. int startmatch(nodo* T, int*P, int indiceP, int indiceT){
  104.    
  105.     nodo *punt = T;
  106.    
  107.     for(int i=0; i<indiceT; i++){   //Vado all'indice di T che mi serve per iniziare il match
  108.        
  109.         if(punt->next){                 //Se esiste il successivo...
  110.             punt = punt->next;              //...mi muovo
  111.         }
  112.        
  113.         else{                           //Altrimentri...
  114.             return -1;                      //...ritorno -1
  115.         }
  116.        
  117.     }
  118.    
  119.     for(int i=0; true; ){           //Inizio il controllo
  120.        
  121.         if(punt->info1==P[indiceP]){    //Se il campo info1 è uguale a P[indiceP]
  122.             return punt->info2;             //la ricerca si ferma
  123.         }
  124.      
  125.         else{                           //Altrimenti
  126.            
  127.             if(punt->next){                 //se esiste un prossimo
  128.                 punt=punt->next;                //mi sposto sul prossimo
  129.                 i++;                            //e continuo la ricerca
  130.             }
  131.            
  132.             else{                           //se non esiste
  133.                 return -1;                      //non ho trovato il match
  134.             }
  135.            
  136.         }
  137.        
  138.     }
  139.    
  140.     return -1;
  141.    
  142. }
  143.  
  144. //Controlla quanto è lungo un match di P[indiceP...dimP-1] a partire da T(x, indiceT) && Restituisce 0 se non vi è match
  145. int checklung(nodo* T, int*P, int indiceP, int indiceT, int dimP){
  146.    
  147.     if(indiceT == -1){
  148.        
  149.         return 0;
  150.        
  151.     }
  152.    
  153.     nodo *punt = T;
  154.    
  155.     for(int i=0; i<indiceT; i++){   //Vado all'indice di T che mi serve per iniziare il match
  156.        
  157.         if(punt->next){                 //Se esiste il successivo...
  158.             punt = punt->next;              //...mi muovo
  159.         }
  160.        
  161.         else{                           //Altrimentri...
  162.             return 0;                      //...ritorno -1
  163.         }
  164.        
  165.     }
  166.    
  167.     for(int i=0; true; ){     //Controllo quanto è lungo il match
  168.        
  169.         if(P[indiceP+i]==punt->info1){  //Se trovo un match tra il campo info1 di T e P[i]
  170.             i++;                            //cerco il prossimo
  171.            
  172.             if(i==dimP){                        //se ho trovato tutto il match
  173.                 return i;                           //ritorno la lunghezza
  174.             }
  175.            
  176.             if(punt->next){
  177.                 punt = punt->next;
  178.             }
  179.            
  180.             else{
  181.                 return i;
  182.             }
  183.            
  184.         }
  185.        
  186.         else{                           //Se non trovo match
  187.             return i;                       //ritorno la lunghezza fin'ora trovata
  188.         }
  189.        
  190.     }
  191.    
  192.     return 0;
  193.    
  194. }
  195.  
  196. //PRE=(T lista corretta, dimP>=0)
  197. nodo* M1(nodo* T, int*P, int dimP){     //ITERATIVA
  198.  
  199.     nodo *lista;
  200.    
  201.     //Crea il primo nodo della lista
  202.     lista->info1 = startmatch(T, P, 0, 0);
  203.     lista->info2 = checklung(T, P, 0, lista->info1, dimP);
  204.    
  205.    
  206.     int ultimoT = lista->info1 + lista->info2, ultimoP = lista->info2;
  207.     nodo *pros = lista;
  208.    
  209.     while(true){
  210.        
  211.         int start = startmatch(T, P, ultimoP, ultimoT);
  212.         int lung = checklung(T, P, ultimoP, start, dimP);
  213.  
  214.         if(start!= -1 && lung!= 0){
  215.            
  216.             pros->next = new nodo(start-ultimoT, lung, 0);
  217.             pros = pros->next;
  218.             ultimoT = start + lung;
  219.             ultimoP += lung;
  220.  
  221.         }
  222.        
  223.         else{
  224.  
  225.             return lista;
  226.            
  227.         }
  228.        
  229.        
  230.     }
  231.  
  232.    
  233.     return lista;
  234.    
  235. }
  236. //POST=(M1 restituisce col return la lista del match di P in T)
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252. //PRE_TB=(T e X sono liste corrette, I campi info1 e info2 dei nodi di X (se ci sono nodi) sono tutti maggiori o uguali a 0, T=vT)
  253. void TB(nodo*&T, nodo*X){
  254.     return;
  255. }
  256. //POST_TB=( T=(vT-X), i nodi di (X di vT) sono deallocati).
  257.  
  258.  
  259.  
  260. //PRE_TC=(T e X sono liste corrette, i campi info1 e info2 dei nodi di X (se ci sono nodi) sono tutti maggiori o uguali a 0, T=vT)
  261. nodo* TC(nodo*&T, nodo*X){
  262.     return T;
  263.  
  264. }
  265. //POST_TC=( T=(vT-X), e restituisce (X di vT) con il return)
  266.  
  267.  
  268. main(){
  269.    
  270.     try{
  271.        
  272.         ifstream INP("input");
  273.         ofstream OUT("output");
  274.        
  275.         if(!INP){
  276.            
  277.             throw(0);
  278.            
  279.         }
  280.        
  281.         if(!OUT){
  282.            
  283.             throw(1);
  284.            
  285.         }
  286.        
  287.         int P[100], dim, dimP, dimX;
  288.         INP>>dim>>dimP>>dimX;
  289.  
  290.         nodo*L=0;
  291.         crea(L,dim,INP,0);
  292.         stampa(L, OUT);
  293.  
  294.         for(int i=0;i<dimP ; i++){
  295.            
  296.             INP>>P[i];
  297.            
  298.         }
  299.        
  300.         nodo *z = M1(L, P,dimP);
  301.      
  302.         if(!z){
  303.            
  304.             OUT<<"nessun match"<<endl;
  305.            
  306.         }
  307.        
  308.         else{
  309.            
  310.             stampa(z,OUT);
  311.            
  312.         }
  313.  
  314.  
  315.         OUT<<endl;
  316.        
  317.        
  318.         nodo* X=0;
  319.        
  320.         crea1(X,dimX,INP,0);
  321.        
  322.         nodo*L1=copia(L);
  323.        
  324.         TB(L1,X);               //da fare
  325.        
  326.         stampa(L1,OUT);
  327.        
  328.         cout<<endl;
  329.        
  330.         nodo* z1=TC(L,X);       //da fare
  331.         stampa(L,OUT);
  332.        
  333.         cout<<endl;
  334.        
  335.         stampa(z1,OUT);
  336.        
  337.         cout<<endl;
  338.        
  339.     }
  340.    
  341.     catch(int x){
  342.        
  343.         switch(x){
  344.            
  345.             case 0: cout<<"problemi con input"<<endl; break;
  346.             case 1: cout<<"problemi con output"<<endl; break;
  347.            
  348.         }
  349.        
  350.     }
  351.    
  352. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement