      Program TOTO8_V091
      parameter (MAX=200)
      integer a(MAX,0:27),scelta,lung,soglia,tabel(130,3)
      integer lista(30,2),diff,dif_fc,data(2),cont
      character vb
      character *10 squad(128)
      real m(30),mp(30),peso,rette(MAX,2),medpes,freq,fat_som
      real fuori_c,seriea,fat_dif,fr(128)
      fat_som=1.5
      fat_dif=0.1
      diff=10
      dif_fc=5
      fuori_c=1
      seriea=0.5
      medpes=0.01
      freq=1
      open (unit=4,file='Squadre.nom',status='old',form='formatted')
      do 15 i=1,128
         read(4,'(A10)') squad(i)
15    continue
      close (4)
      open (unit=3,file='Classif.dat',status='old',form='formatted')
      do 16 i=1,130
         read(3,*) (tabel(i,j),j=1,3)
16    continue
      close (3)
      open (unit=4,file='Toto8.con',status='old',form='formatted')
      read(4,*) lung
      close(4)
      open (unit=3,file='Toto8.dat',status='old',form='formatted')
      do 60 i=1,lung
         read(3,*) (a(i,j),j=0,27)
60    continue
      close (3)
      write(*,*) ' '
      write(*,*) '                TOTO 8'
      write(*,*) '          by Paolo Ferraresi'
      write(*,*) '    paolo.ferraresi@carpi.nettuno.it'
      write(*,*) ' '
      write(*,*) 'Hai già inserito le partite di questa schedina
     * (s,n,x=non lo faccio)?'
      read(*,'(A1)') vb
      if (vb.eq.'s') then
         open(unit=3,file='Lista.dat',status='old',form='formatted')
         do 2 i=1,30
            read(3,*) (lista(i,j),j=1,2)
2        continue
         close(3)
      else
         if (vb.eq.'n') then
            call Inserire (lista,squad)
         endif
      endif
      soglia=lung-5
      if (soglia.lt.0) soglia=0
      peso=1.5
5     cont=0
      do 10 i=1,30
         m(i)=0
10    continue
      do 20 i=1,lung
         if (a(i,2).ne.0) then
            do 13 j=2,9
               m(a(i,j))=m(a(i,j))+1
13          continue
            cont=cont+1
         endif
20    continue
      do 30 i=1,30
         m(i)=m(i)/cont*100
30    continue
      call M_Pes(a,mp,soglia,peso,lung,MAX)
      call Frequenz(a,fr,lung,MAX)
      do 23 i=1,lung
         if (a(i,2).eq.0) then
            rette(i,1)=-1
            rette(i,2)=-1
         else           
            data(1)=0
            data(2)=0
            do 25 j=2,9
               data(1)=data(1)+a(i,j)
25          continue
            do 27 j=1,8
               data(2)=data(2)+a(i,j+1)*j
27          continue
            rette(i,1)=(data(2)-4.5*data(1))/42
            rette(i,2)=(data(1)-36*rette(i,1))/8
         endif
23    continue
40    write(*,*) ' '
      write(*,*) 'MENU'
      write(*,*) ' '
      write(*,*) '1_Cabala'
      write(*,*) '2_Pronostici'
      write(*,*) '3_Consultazione dati'
      write(*,*) '4_Aggiornamento dati'
      write(*,*) '5_Schedina corrente'
      write(*,*) '0_EXIT'
      write(*,*) ' '
      write(*,*) 'Scelta:'
      read(*,'(I1)') scelta
      write(*,*) ' '
      if (scelta.eq.1) then
50       write(*,*) ' '
         write(*,*) 'MENU CABALA'
         write(*,*) '1_Pronostico medio'
         write(*,*) '2_Pronostico pesato'
         write(*,*) '3_Percentuali medie'
         write(*,*) '4_Percentuali pesate'
         write(*,*) '5_Variazione pesi'
         write(*,*) '0_EXIT'
         write(*,*) ' '
         write(*,*) 'Scelta'
         read(*,'(I1)') scelta
         if (scelta.eq.1) then
            call Pronost(m,rette,lung,MAX)
         else
            if (scelta.eq.2) then
               call Pronost(mp,rette,lung,MAX)
            else
               if (scelta.eq.3) then
                  call Percent(m)
               else
                  if (scelta.eq.4) then
                     call Percent(mp)
                  else
                     if (scelta.eq.5) then
                        call Cambia(peso,soglia,lung)
                        call M_Pes(a,mp,soglia,peso,lung,MAX)
                     else
                        write(*,*) ' '
                        goto 40
                     endif
                  endif
               endif
            endif
         endif
         goto 50
      else
         if (scelta.eq.2) then
            if ((vb.eq.'s').or.(vb.eq.'n')) then
33             write(*,*) ' '
               write(*,*) 'MENU PRONOSTICI'
               write(*,*) '1_Pronostico ponderato'
               write(*,*) '2_Preferenze'
               write(*,*) '0_EXIT'
               write(*,*) ' '
               write(*,*) 'Scelta:'
               read(*,'(I1)') scelta
               if (scelta.eq.1) then
                  call Vinco (tabel,lista,mp,fat_som,fat_dif,diff
     *,dif_fc,fuori_c,seriea,medpes,freq,fr)
                  goto 33
               else
                  if (scelta.eq.2) then
                     call Pref(fat_som,fat_dif,diff,dif_fc,fuori_c,
     *seriea,medpes,freq)
                     goto 33
                  else
                     write(*,*) ' '
                  endif
               endif
            else
               write(*,*) ' ' 
               write(*,*) 'Non puoi accedere a questo menu se non
     * inserisci le partite in schedina'
               write(*,*) ' '
            endif
         else
            if (scelta.eq.3) then
               call Consulta (a,lung,tabel,squad,MAX)
            else
               if (scelta.eq.4) then
                  write(*,*) ' '
                  write(*,*) 'MENU AGGIORNAMENTO DATI'
                  write(*,*) '1_Aggiornamento colonne vincenti'
                  write(*,*) '2_Aggiornamento parziale dati squadre'
                  write(*,*) '3_Aggiornamento totale dati squadre'
                  write(*,*) '4_Correzione dei dati di una squadra'
                  write(*,*) '5_Nuova schedina'
                  write(*,*) '0_EXIT'
                  write(*,*) ' '
                  write(*,*) 'Scelta:'
                  read(*,'(I1)') scelta
                  if (scelta.eq.1) then
                     call Aggiorna (a,rette,lung,MAX)
                     goto 5
                  else
                     if (scelta.eq.2) then
                        call Parziale (a,lung,tabel,squad,rette,130,
     *MAX)
                     else
                        if (scelta.eq.3) then
                           call Totale(squad,tabel,130)
                        else
                           if (scelta.eq.4) then
                              call Corregg(tabel)
                           else
                              if (scelta.eq.5) then
                                 call Inserire(lista,squad)
                              else
                                 write(*,*) ' '
                              endif
                           endif
                        endif
                     endif
                  endif
               else
                  if (scelta.eq.5) then
                     call Scheda(lista,squad)
                  else
                     stop
                  endif
               endif
            endif
         endif
      endif
      goto 40
      stop
      end

      Subroutine M_Pes(a,z,soglia,peso,lung,ldn)
      integer a(ldn,0:*),soglia,lung,cont
      real z(*),peso
      cont=0
      do 70 i=1,30
         z(i)=0
70    continue
      do 80 i=1,lung
         if (a(i,2).ne.0) then
            if (i.le.soglia) then
               do 90 j=2,9
                  z(a(i,j))=z(a(i,j))+1
90             continue
            else
               do 100 j=2,9
                  z(a(i,j))=z(a(i,j))+peso
100            continue
            endif
            cont=cont+1
         endif
80    continue
      do 110 i=1,30
         z(i)=z(i)/cont*100
110   continue
      return
      end

      Subroutine Percent(p)
      real p(*)
      write(*,*) ' '
      do 120 i=1,15
         write(*,'(I3,A1,I5,I10,A1,I5)') i,'=',int(p(i)),i+15,'=',
     *int(p(i+15))
120   continue
      return
      end

      Subroutine Pronost(p,rette,lung,ldn)
      external Sort
      real p(*),f(30),temp,rette(ldn,*),pende,trasla,distanza(15)
      integer c(8),pari(15),resta,cont,lung,aux
      do 130 i=1,30
         f(i)=p(i)
130   continue
      resta=8
150   continue
      temp=f(1)
      pari(1)=1
      cont=0
      do 140 j=1,30
         if ((f(j).eq.temp).and.(cont.lt.15)) then
            cont=cont+1
            pari(cont)=j
         else
            if (f(j).gt.temp) then
               cont=1
               pari(cont)=j
               temp=f(j)
            endif
         endif
140   continue
      if (resta.gt.cont) then
         do 145 k=1,cont
            c(8-resta+k)=pari(k)
            f(pari(k))=0
145      continue
         resta=resta-cont
         goto 150
      endif
      if (resta.eq.cont) then
         do 155 k=1,cont
            c(8-resta+k)=pari(k)
155      continue
         call Sort (c)
         write(*,*)  ' '
         write(*,*) 'Pronostico eseguito facendo la media su tutte le
     * colonne vincenti'
         write(*,*) (c(i),i=1,8)
      endif
      if (resta.lt.cont) then
         do 135 i=1,resta
            c(8-resta+i)=31+i
135      continue
         call Sort(c)
         write(*,*) ' '
         write(*,*) 'Pronostico eseguito facendo la media su tutte le
     * colonne vincenti'
         write(*,*) (c(i),i=1,8-resta)
         write(*,*) 'Numeri che possono completare la colonna vincente:'
         write(*,*) '(',(pari(i),i=1,cont),')'
         write(*,*) ' '
         write(*,*) 'La serie di 8 numeri più probabile è però:'
         do 160 i=1,lung
            if (rette(i,1).ne.-1) then
               pende=pende+rette(i,1)
               trasla=trasla+rette(i,2)
            endif
160      continue
         pende=pende/lung
         trasla=trasla/lung
         do 165 i=1,cont
            pos=1
152         if (pari(i).gt.c(pos)) then
               pos=pos+1
               goto 152
            endif
            distanza(i)=abs((pende*pos+trasla)-pari(i))
165      continue
         do 157 i=1,resta
            aux=1
            temp=distanza(1)
            do 162 j=2,cont
               if (distanza(j).lt.temp) then
                  temp=distanza(j)
                  aux=j
               endif
162         continue
            c(8-resta+i)=pari(aux)
            distanza(aux)=100
157      continue
         call Sort(c)
         write(*,*) (c(i),i=1,8)
      endif
      return
      end

      Subroutine Cambia (peso,soglia,lung)
      integer soglia,lung
      real peso
      write(*,*) ' '
      write(*,*) 'Numero di giornate da valutare con un peso diverso
     * (default=5):'
      read(*,*) soglia
      soglia=lung-soglia
      if (soglia.lt.0) soglia=0
      write(*,*) 'Peso (default=1.5):'
      read(*,*) peso
      return
      end

      Subroutine Consulta(a,lung,classif,squad,ldn)
      external Mostra
      integer sc,a(ldn,0:*),lung,num,ann,classif(130,3)
      character tasto
      character *10 squad(*)
170   write(*,*) ' '
      write(*,*) 'MENU CONSULTAZIONE'
      write(*,*) '1_Vedi classifiche'
      write(*,*) '2_Vedi tutte le colonne vincenti'
      write(*,*) '3_Vedi una colonna vincente'
      write(*,*) '0_EXIT'
      write(*,*) ' '
      write(*,*) 'Scelta:'
      read(*,'(I1)') sc
      if (sc.eq.1) then
         call Mostra(classif,squad)
         goto 170
      else
         if (sc.eq.2) then
            write(*,*) ' '
            write(*,*) 'Sched. | Anno |        Numeri vincenti'
            do 180 i=1,lung
               if (a(i,2).ne.0) then
               write(*,'(I6,A2,I5,A2,8I4)') a(i,0),' |',a(i,1),' |',
     *(a(i,j),j=2,9)
               endif
               if (mod(i,25).eq.0) then
                  write(*,*) 'Premi invio per continuare'
                  read(*,'(A1)') tasto
               endif
180         continue
            goto 170
         else
            if (sc.eq.3) then
               write(*,*) ' '
               write(*,*) 'Schedina numero:'
               read(*,*) num
               write(*,*) 'Anno:'
               read(*,*) ann
               do 190 i=1,lung
                  if (a(i,0).eq.num) then
                     if (a(i,1).eq.ann) then
                        write(*,*) ' '
                        write(*,*) 'Sched. | Anno |        Numeri
     * vincenti'
                        write(*,'(I6,A2,I5,A2,8I4)') a(i,0),' |',
     *a(i,1),' |',(a(i,j),j=2,9)
                        goto 170
                     endif
                  endif
190            continue
               write(*,*) ' '
               write(*,*) 'La schedina richiesta non è in archivio'
               goto 170
            else
               write(*,*) ' '
            endif
         endif
      endif
      return
      end

      Subroutine Mostra(classif,squad)
      external Sort2,Scrivi
      integer classif(130,3),temp(20,2),c
      character *10 squad(*)
      c=0
      write(*,*) '**** Serie A ****'
      do 192 k=1,128
         if ((k.ge.19).and.(k.le.38)) c=18
         if ((k.ge.39).and.(k.le.56)) c=38
         if ((k.ge.57).and.(k.le.74)) c=56
         if ((k.ge.75).and.(k.le.92)) c=74
         if ((k.ge.93).and.(k.le.110)) c=92
         if ((k.ge.111).and.(k.le.128)) c=110
         temp(k-c,1)=k
         temp(k-c,2)=classif(k,1)
         if (k.eq.18) then
            if (classif(129,1).ne.0) call Sort2(temp,18)
            call Scrivi(classif,temp,squad,18)
            write(*,*) '**** Serie B ****'
         endif
         if (k.eq.38) then
            if (classif(129,2).ne.0) call Sort2(temp,20)
            call Scrivi(classif,temp,squad,20)
            write(*,*) '**** Serie C1 A ****'
         endif
         if (k.eq.56) then
            if (classif(129,3).ne.0) call Sort2(temp,18)
            call Scrivi(classif,temp,squad,18)
            write(*,*) '**** Serie C1 B ****'
         endif
         if (k.eq.74) then
            if (classif(129,3).ne.0) call Sort2(temp,18)
            call Scrivi(classif,temp,squad,18)
            write(*,*) '**** Serie C2 A ****'
         endif
         if (k.eq.92) then
            if (classif(130,1).ne.0) call Sort2(temp,18)
            call Scrivi(classif,temp,squad,18)
            write(*,*) '**** Serie C2 B ****'
         endif
         if (k.eq.110) then
            if (classif(130,1).ne.0) call Sort2(temp,18)
            call Scrivi(classif,temp,squad,18)
            write(*,*) '**** Serie C2 C ****'
         endif
192   continue
      if (classif(130,1).ne.0) call Sort2(temp,18)
      call Scrivi(classif,temp,squad,18)
      return
      end

      Subroutine Scrivi(classif,temp,squad,num)
      integer classif(130,3),num,temp(20,2)
      character *10 squad(*)
      character tasto
      write(*,*) '                  punti  | gol fatti | gol subiti'
      do 196 k=1,num
      write(*,'(A11,I11,I10,I12)') squad(temp(k,1)),classif(temp(k,1)
     *,1),classif(temp(k,1),2),classif(temp(k,1),3)
196   continue
      write(*,*) ' '
      write(*,*) 'Premi invio per continuare'
      read(*,'(A1)') tasto
      return
      end

      Subroutine Sort2(matr,num)
      integer matr(20,2),num,temp(2),scambio      
243   continue
      scambio=0
      do 246 i=1,num-1
         if (matr(i,2).lt.matr(i+1,2)) then
            temp(1)=matr(i,1)
            temp(2)=matr(i,2)
            matr(i,1)=matr(i+1,1)
            matr(i,2)=matr(i+1,2)
            matr(i+1,1)=temp(1)
            matr(i+1,2)=temp(2)
            scambio=scambio+1
         endif
246   continue
      if (scambio.ge.1) goto 243
      return
      end
  
      Subroutine Aggiorna (a,rette,lung,ldn)
      integer lung,a(ldn,0:*),vinc(0:11)
      character ok
      real rette(ldn,*)
      write(*,*) ' '
      write(*,*) 'AGGIORNAMENTO COLONNE VINCENTI'
200   write(*,*) 'Inserire il numero della schedina (0_EXIT):'
      read(*,*) vinc(0)
      if (vinc(0).eq.0) return
      write(*,*) 'Inserire l''anno (aa):'
      read(*,*) vinc(1)
      write(*,*) 'Numeri vincenti:'
      read(*,*) (vinc(j),j=2,9)
      write(*,*) 'E'' tutto corretto (s,n)?'
      read(*,'(A1)') ok
      if (ok.eq.'n') goto 200
      vinc(10)=0
      vinc(11)=0
      call Sort(vinc(2))
      if ((a(lung,2).eq.0).and.(a(lung,0).eq.vinc(0))) then
         do 230 i=1,11
            a(lung,i)=vinc(i)
230      continue
         call MinQuad(vinc,rette(lung,1))
      else
         do 220 i=0,11
            a(lung+1,i)=vinc(i)
220      continue
         do 225 i=12,27
            a(lung+1,i)=0
225      continue
         lung=lung+1
         call MinQuad (vinc,rette(lung,1))
         open (unit=4,file='Toto8.con',status='new',form='formatted')
         write (4,*) lung
         close(4)
      endif
      open (unit=3,file='Toto8.dat',status='new',form='formatted')
      do 210 i=1,lung
         write (3,*) (a(i,j),j=0,27)
210   continue
      close(3)
      return
      end

      Subroutine MinQuad(vinc,retta)
      integer vinc(0:9),dati(2)
      real retta(2)
      dati(1)=0
      dati(2)=0
      do 290 i=2,9
         dati(1)=dati(1)+vinc(i)
290   continue
      do 300 i=1,8
         dati(2)=dati(2)+vinc(i+1)*i
300   continue
      retta(1)=(dati(2)-4.5*dati(1))/42
      retta(2)=(dati(1)-36*retta(1))/8
      return
      end

      Subroutine Sort (v)
      integer v(8),temp,scambio
240   continue
      scambio=0
      do 250 i=1,7
         if (v(i).gt.v(i+1)) then
            temp=v(i)
            v(i)=v(i+1)
            v(i+1)=temp
            scambio=scambio+1
         endif
250   continue
      if (scambio.ge.1) goto 240
      return
      end

      Subroutine Totale (squad,tabel,ldn)
      integer tabel(ldn,*)
      character ok
      character *10 squad(*)
      write(*,*) 'AGGIORNAMENTO TOTALE DATI SQUADRE'
      write(*,*) 'Continuo (s,n)?'
      read(*,'(A1)') ok
      if (ok.eq.'n') return
      write(*,*) 'Inserire per ogni squadra: punti, gol fatti, gol
     * subiti'
      write(*,*) '* SERIE A *'
      do 260 i=1,18
         write(*,*) squad(i)
         read(*,*) (tabel(i,j),j=1,3)
260   continue
      write(*,*) '* SERIE B *'
      do 262 i=19,38
         write(*,*) squad(i)
         read(*,*) (tabel(i,j),j=1,3)
262   continue
      write(*,*) '* SERIE C1 girone A *'
      do 264 i=39,56
         write(*,*) squad(i)
         read(*,*) (tabel(i,j),j=1,3)
264   continue
      write(*,*) '* SERIE C1 girone B *'
      do 266 i=57,74
         write(*,*) squad(i)
         read(*,*) (tabel(i,j),j=1,3)
266   continue
      write(*,*) '* SERIE C2 girone A *'
      do 268 i=75,92
         write(*,*) squad(i)
         read(*,*) (tabel(i,j),j=1,3)
268   continue
      write(*,*) '* SERIE C2 girone B *'
      do 270 i=93,110
         write(*,*) squad(i)
         read(*,*) (tabel(i,j),j=1,3)
270   continue
      write(*,*) '* SERIE C2 girone C *'
      do 272 i=111,128
         write(*,*) squad(i)
         read(*,*) (tabel(i,j),j=1,3)
272   continue
      write(*,*) 'Numero di partite giocate in serie A:'
      read(*,*) tabel(129,1)
      write(*,*) 'Numero di partite giocate in serie B:'
      read(*,*) tabel(129,2)
      write(*,*) 'Numero di pertite giocate in serie C1:'
      read(*,*) tabel(129,3)
      write(*,*) 'Numero di partite giocate in serie C2:'
      read(*,*) tabel(130,1)
      tabel(130,2)=0
      tabel(130,3)=0
      open (unit=4,file='Classif.dat',status='new',form='formatted')
      do 280 i=1,130
         write(4,*) (tabel(i,j),j=1,3)
280   continue
      close (4)
      return
      end

      Subroutine Parziale(a,lung,tabel,squad,rette,ldn,ldna)
      external Comp
      integer tabel(ldn,*),risult(128,3),casa,cod,a(ldna,0:27)
      integer numero,temp,toto(8),lung
      character ok
      real rette(ldna,*)
      character *10 squad(*)
      write(*,*) 'AGGIORNAMENTO PARZIALE DATI SQUADRE'
      write(*,*) 'Puoi fare l''aggiornamento parziale solo se hai fatto
     * almeno una volta quello totale'
      write(*,*) 'Continuo (s,n)?'
      read(*,'(A1)') ok
      if (ok.eq.'n') return
344   continue
      do 340 i=1,128
         risult(i,1)=-1
340   continue
      write(*,*) 'Numero schedina della domenica di questi risultati'
      read(*,*) numero
      do 350 i=1,128
         if (i.eq.1) then
            write(*,*) '* SERIE A *'
         else
            if (i.eq.19) then
               write(*,*) '* SERIE B *'
            else
               if (i.eq.39) then
                  write(*,*) '* SERIE C1 girone A *'
               else
                  if (i.eq.57) then
                     write(*,*) '* SERIE C1 girone B *'
                  else
                     if (i.eq.75) then
                        write(*,*) '* SERIE C2 girone A *'
                     else
                        if (i.eq.93) then
                           write(*,*) '* SERIE C2 girone B *'
                        else
                           if (i.eq.111) then
                              write(*,*) '* SERIE C2 girone C *'
                           endif
                        endif
                     endif
                  endif
               endif
            endif
         endif
348      if (risult(i,1).eq.-1) then
            write(*,*) squad(i),' gioca in casa (1=sì,0=no,2=non
     * gioca,3=correz. partita prec.)?'
            read(*,*) casa
            if (casa.eq.1) then
               write(*,*) 'Contro (codice):'
               read(*,*) cod
               write(*,*) squad(cod)
               call Comp(risult,i,cod)
            else
               if (casa.eq.0) then
                  write(*,*) 'Contro (codice):'
                  read(*,*) cod
                  write(*,*) squad(cod)
                  call Comp(risult,cod,i)
               else
                  if (casa.eq.3) then
                     risult(cod,1)=-1
                     write(*,*) squad(i-1),' gioca in casa (1=sì,0=no
     *,2=non gioca)?'
                     read(*,*) casa
                     if (casa.eq.1) then
                        write(*,*) 'Contro (codice):'
                        read(*,*) cod
                        write(*,*) squad(cod)
                        call Comp(risult,i-1,cod)
                     else
                        if (casa.eq.0) then
                           write(*,*) 'Contro (codice):'
                           read(*,*) cod
                           write(*,*) squad(cod)
                           call Comp(risult,cod,i-1)
                        else
                           risult(i-1,1)=0
                        endif
                     endif
                     goto 348
                  else
                     risult(i,1)=0
                  endif
               endif
            endif
         endif
350   continue
      write(*,*) 'E'' tutto corretto (s,n)?'
      read(*,'(A1)') ok
      if (ok.eq.'n') goto 344
      do 360 i=1,128
         if (risult(i,1).ne.0) then
            tabel(i,2)=tabel(i,2)+risult(i,2)
            tabel(i,3)=tabel(i,3)+risult(i,3)
            tabel(risult(i,1),2)=tabel(risult(i,1),2)+risult(i,3)
            tabel(risult(i,1),3)=tabel(risult(i,1),3)+risult(i,2)
            if (risult(i,2).eq.risult(i,3)) then
               tabel(i,1)=tabel(i,1)+1
               tabel(risult(i,1),1)=tabel(risult(i,1),1)+1
            else
               if (risult(i,2).gt.risult(i,3)) then
                  tabel(i,1)=tabel(i,1)+3
               else
                  tabel(risult(i,1),1)=tabel(risult(i,1),1)+3
               endif
            endif
         endif
360   continue
      k=1
355   continue
      if (risult(k,1).ne.0) then
         if (k.le.18) then
            tabel(129,1)=tabel(129,1)+1
            k=19
         else
            if ((k.le.38).and.(k.gt.18)) then
               tabel(129,2)=tabel(129,2)+1
               k=39
            else
               if ((k.le.74).and.(k.gt.38)) then
                  tabel(129,3)=tabel(129,3)+1
                  k=75
               else
                  if ((k.le.128).and.(k.gt.74)) then
                     tabel(130,1)=tabel(130,1)+1
                     k=129
                  endif
               endif
            endif
         endif
      else
         k=k+1
      endif
      if (k.le.128) goto 355
      open (unit=3,file='Classif.dat',status='new',form='formatted')
      do 370 i=1,130
         write(3,*) (tabel(i,j),j=1,3)
370   continue
      close(3)
      elem=0
      cont=0
365   continue
      cont=cont+1
      if (risult(cont,1).ne.0) then
         toto(elem+1)=cont
         elem=elem+1
      endif
      if (elem.ne.8) goto 365
      do 380 i=cont+1,128
         if (risult(i,1).ne.0) then
            somma=risult(i,2)+risult(i,3)
            temp=8
            do 390 k=7,1,-1
               if (risult(toto(k),2)+risult(toto(k),3).lt.risult(toto
     *(temp),2)+risult(toto(temp),3)) then
                  temp=k
               else
                  if ((risult(toto(k),2)+risult(toto(k),3).eq.risult
     *(toto(temp),2)+risult(toto(temp),3)).and.(risult(toto(k),3).le.
     *risult(toto(temp),3))) then
                     temp=k
                  endif
               endif
390         continue
            if (somma.gt.risult(toto(temp),2)+risult(toto(temp),3))
     *then
               toto(temp)=i
            else
               if ((somma.eq.risult(toto(temp),2)+risult(toto(temp),
     *3)).and.(risult(i,3).gt.risult(toto(temp),3))) then
                  toto(temp)=i
               endif
            endif
         endif
380   continue
      if (numero.eq.a(lung,0)) then
         do 400 i=1,8
            a(lung,i*2+10)=toto(i)
            a(lung,i*2+11)=risult(toto(i),1)
400      continue
      else
         lung=lung+1
         a(lung,0)=numero
         do 405 i=1,11
            a(lung,i)=0
405      continue
         do 410 i=1,8
            a(lung,i*2+10)=toto(i)
            a(lung,i*2+11)=risult(toto(i),1)
410      continue
         open (unit=3,file='Toto8.con',status='new',form='formatted')
         write(3,*) lung
         close(3)
         rette(lung,1)=-1
         rette(lung,2)=-1
      endif
      open (unit=3,file='Toto8.dat',status='new',form='formatted')
      do 420 i=1,lung
         write(3,*) (a(i,j),j=0,27)
420   continue
      close(3)
      return
      end

      Subroutine Comp (risult,i,cod)
      integer risult(128,3),cod,i
      risult(i,1)=cod
      risult(cod,1)=0
      write(*,*) 'Risultato partita:'
      read(*,*) risult(i,2),risult(i,3)
      return
      end

      Subroutine Inserire(lista,squad)
      integer lista(30,2),parti
      character ok
      character *10 squad(128)
      write(*,*) 'INSERIMENTO PARTITE NUOVA SCHEDINA'
      write(*,*) 'Continuo (s,n)?'
      read(*,'(A1)') ok
      if (ok.eq.'n') return
      write(*,*) 'Inserire i codici delle 2 squadre che giocano la
     * pertita indicata'
      do 430 i=1,30
         write(*,*) 'Partita',i,':'
         read(*,*) lista(i,1),lista(i,2)
         write(*,*) squad(lista(i,1)),'  ',squad(lista(i,2))
430   continue
422   write(*,*) 'E'' corretto (s,n)?'
      read(*,'(A1)') ok
      if (ok.eq.'n') then
         write(*,*) 'Quale partita è errata?'
         read(*,*) parti
         write(*,*) 'Introdurre i codici corretti:'
         read(*,*) lista(parti,1),lista(parti,2)
         write(*,*) squad(lista(parti,1)),'  ',squad(lista(parti,2))
         goto 422
      endif
      open (unit=3,file='Lista.dat',status='new',form='formatted')
      do 440 i=1,30
         write(3,*) (lista(i,j),j=1,2)
440   continue
      close(3)
      return
      end

      Subroutine Corregg (tabel)
      integer tabel(130,3),cod
      character ok
      write(*,*) 'CORREZIONE DATI DI UNA SQUADRA'
      write(*,*) 'Continuo (s,n)?'
      read(*,'(A1)') ok
      if (ok.eq.'n') return
      write(*,*) 'Quale squadra (codice)?'
      read(*,*) cod
      write(*,*) 'Nuovi dati (punti, gol fatti, gol subiti)'
      read(*,*) (tabel(cod,j),j=1,3)
      open (unit=3,file='Classif.dat',status='new',form='formatted')
      do 450 i=1,130
         write(3,*) (tabel(i,j),j=1,3)
450   continue
      close(3)
      return
      end

      Subroutine Vinco (tabel,lista,mp,fat_som,fat_dif,diff,dif_fc
     *,fuori_c,seriea,medpes,freq,fr)
      external Sort
      integer tabel(130,*),lista(30,*),qui
      integer diff,dif_fc,temp,numint(8)
      real mp(*),fat_som,fat_dif,medpes,freq,num(8,2),fuori_c,seriea
      real punti,fr(*),num2(4,2)
      do 455 i=1,8
         num(i,2)=0
455   continue
      do 456 i=1,4
         num2(i,2)=0
456   continue
      do 460 i=1,30
         if (lista(i,1).le.18) then
            punti=(real(tabel(lista(i,1),2)+tabel(lista(i,1),3)+
     *tabel(lista(i,2),2)+tabel(lista(i,2),3))/tabel(129,1))*fat_som
         endif
         if ((lista(i,1).gt.18).and.(lista(i,1).le.38)) then
            punti=(real(tabel(lista(i,1),2)+tabel(lista(i,1),3)+
     *tabel(lista(i,2),2)+tabel(lista(i,2),3))/tabel(129,2))*fat_som
         endif
         if ((lista(i,1).gt.38).and.(lista(i,1).le.74)) then
            punti=(real(tabel(lista(i,1),2)+tabel(lista(i,1),3)+
     *tabel(lista(i,2),2)+tabel(lista(i,2),3))/tabel(129,3))*fat_som
         endif
         if (lista(i,1).gt.74) then
            punti=(real(tabel(lista(i,1),2)+tabel(lista(i,1),3)+
     *tabel(lista(i,2),2)+tabel(lista(i,2),3))/tabel(130,1))*fat_som
         endif
         if (abs(tabel(lista(i,1),1)-tabel(lista(i,2),1)).ge.diff) then
           punti=punti+(abs(tabel(lista(i,1),1)-tabel(lista(i,2)
     *,1)))*fat_dif
         endif
         if (tabel(lista(i,1),1).le.tabel(lista(i,2),1)-dif_fc) then
            punti=punti+fuori_c
         endif
         if (lista(i,1).le.18) then
            punti=punti+seriea
         endif
         punti=punti+mp(i)*medpes
         punti=punti+(fr(lista(i,1))+fr(lista(i,2)))*freq
         temp=1
         do 470 k=2,8
            if (num(k,2).lt.num(temp,2)) then
               temp=k
            endif
470      continue
         qui=1
         do 462 k=2,4
            if (num2(k,2).lt.num2(qui,2)) then
               qui=k
            endif
462      continue
         if (punti.gt.num(temp,2)) then
            num2(qui,1)=num(temp,1)
            num2(qui,2)=num(temp,2)
            num(temp,1)=i
            num(temp,2)=punti
         else
            if (punti.gt.num2(qui,2)) then
               num2(qui,1)=i
               num2(qui,2)=punti
            endif
         endif
460   continue
      do 466 i=1,8
         numint(i)=int(num(i,1))
466   continue
      call Sort (numint)
      write(*,*) 'PRONOSTICO PONDERATO'
      write(*,*) ' '
      write(*,*) 'I numeri vincenti sono:'
      write(*,*) ' '
      write(*,*) (numint(i),i=1,8),'   (',int(num2(1,1)),',',
     *int(num2(2,1)),',',int(num2(3,1)),',',int(num2(4,1)),')'
      return
      end

      Subroutine Frequenz (a,fr,lung,ldn)
      integer a(ldn,0:*),cont
      real fr(*)
      cont=0
      do 475 i=1,128
         fr(i)=0
475   continue
      do 480 i=1,lung
         if (a(i,12).ne.0) then
            cont=cont+1
            do 490 j=12,27
               fr(a(i,j))=fr(a(i,j))+1
490         continue
         endif
480   continue
      do 485 i=1,128
         fr(i)=fr(i)/cont
485   continue
      return
      end

      Subroutine Pref(fat_som,fat_dif,diff,dif_fc,fuori_c,seriea,
     *medpes,freq)
      integer diff,dif_fc
      real fat_som,fat_dif,medpes,freq,fuori_c,seriea
      write(*,*) 'PREFERENZE'
      write(*,*) 'Fare riferimento al manuale per il significato di
     * ogni voce'
      write(*,*) ' '
      write(*,*) 'Fattore correttivo per il totale dei gol (def.=1.5):'
      read(*,*) fat_som
      write(*,*) 'Differenza di punti minima per considerare la diff.
     * di punti (def.=10):'
      read(*,*) diff
      write(*,*) 'Fattore correttivo per la differenza di punti
     * (def.=0.1):'
      read(*,*) fat_dif
      write(*,*) 'Differenza di punti minima per considerare il
     * fattore campo (def.=5):'
      read(*,*) dif_fc
      write(*,*) 'Quota assegnata se la squadra più forte è fuori
     * casa (def.=1):'
      read(*,*) fuori_c
      write(*,*) 'Quota assegnata se è una partita di serie A (def.
     *=0.5):'
      read(*,*) seriea
      write(*,*) 'Fattore correttivo per la media pesata (def.=0.01):'
      read(*,*) medpes
      write(*,*) 'Fattore correttivo per le presenze in schedina
     * (def.=1):'
      read(*,*) freq
      return
      end

      Subroutine Scheda(lista,squad)
      integer lista(30,*)
      character *10 squad(*)
      write(*,*) ' '
      write(*,*) 'Le partite in schedina sono:'
      do 500 i=1,15
         write(*,*) i,')',squad(lista(i,1)),' ',squad(lista(i,2)),
     *'           ',i+15,')',squad(lista(i+15,1)),' ',squad(lista
     *(i+15,2))
500   continue
      return
      end
