       program tellen
C    Doel van dit programma : kolommen inlezen uit verschillende files en er elementaire bewerkingen
C    mee uitvoeren, zoals optellen, aftrekken, delen en vermenigvuldigen.
        implicit none
        integer maxnfiles,maxntasks,maxnlines,maxcolsperfile
        parameter(maxnfiles=1000,maxnlines=10000,maxcolsperfile=10)
        parameter(maxntasks=maxnfiles-1)
        integer i,j,k,max,skip,nofcols(maxnfiles),lines(maxnfiles)
        integer noftasks,col(0:maxntasks)
        integer noffiles
        character*4 copy
        character car(maxnfiles)
        character*60 bestand,uitfile
        real*8 cols(maxcolsperfile,maxnlines,maxnfiles),uit(maxnlines)
        real*8 fac(0:maxntasks)
        

        open(1,file='in.tel',form='formatted',status='old')
        read(1,*) noffiles, noftasks
        do i=1,noffiles
        read(1,4) bestand
4       format(A60)
        read(1,*) skip,nofcols(i)
        open(20+i,file=bestand,form='formatted',status='old')
        if (skip.gt.0) then
          do j=1,skip; read(20+i,*) ; enddo
        endif
        j=0
1       j=j+1
        read(20+i,*,end=2) (cols(k,j,i),k=1,nofcols(i))
        goto 1
2       close(20+i)
        lines(i)=j-1

        write(*,*) i,bestand,skip,nofcols(i),lines(i),cols(3,200,i)

        enddo     !i

        read(1,*) col(0),fac(0)
        do i=1,noftasks
        read(1,*) col(i),car(i),fac(i)
        enddo
        read(1,3) copy
3       format(A4)
        read(1,4) uitfile
        close(1)
        
        do i=200,210; write(*,*) cols(3,i,1),cols(3,i,2); enddo

        max = 1000000
        do i=1,noffiles; if (lines(i).lt.max) max=lines(i); enddo

        do i=1,max; uit(i)=fac(0)*cols(col(0),i,1); enddo
        do i=1,noftasks
          if (car(i).eq.'A') then; do j=1,max;
               uit(j)=uit(j)+fac(i)*cols(col(i),j,i+1); enddo
          endif
          if (car(i).eq.'S') then; do j=1,max;
               uit(j)=uit(j)-fac(i)*cols(col(i),j,i+1); enddo
          endif
          if (car(i).eq.'M') then; do j=1,max;
               uit(j)=uit(j)*fac(i)*cols(col(i),j,i+1); enddo
          endif
          if (car(i).eq.'D') then; do j=1,max;
               uit(j)=uit(j)/(fac(i)*cols(col(i),j,i+1)); enddo
          endif
        enddo

        open(11,file=uitfile,form='formatted')
        do i=1,max
        if ((copy.eq.'COPY').or.(copy.eq.'copy')) then
         write(11,*) cols(1,i,1),uit(i)
        else
         write(11,*) uit(i)
        endif
        enddo
        close(11)

        write(*,*) 'tellen has finished without problems.'
        write(*,*) 'results have been saved in ',uitfile

        stop
        end


