module VstupVystup

! procedury pro vstup a vystup

use std_type

implicit none

private


public :: cti_cislo, cti_cislo_I, cti_cislo_SP, cti_cislo_DP

public :: cti_matici, cti_matici_SP, cti_matici_DP, cti_matici_I, &
          pis_matici, pis_matici_SP, pis_matici_DP, pis_matici_I, &
          pis_t_matici, pis_t_matici_SP, pis_t_matici_DP, pis_t_matici_I
          
public :: cti_vektor,cti_vektor_I,cti_vektor_SP,cti_vektor_DP, &
          pis_vektor,pis_vektor_I,pis_vektor_SP,pis_vektor_DP

interface cti_cislo
    module procedure cti_cislo_I, cti_cislo_SP, cti_cislo_DP
end interface

interface cti_vektor
    module procedure cti_vektor_I, cti_vektor_SP, cti_vektor_DP
end interface

interface pis_vektor
    module procedure pis_vektor_I,pis_vektor_SP,pis_vektor_DP
end interface

interface cti_matici
    module procedure cti_matici_SP, cti_matici_DP, cti_matici_I
end interface

interface pis_matici
    module procedure pis_matici_SP, pis_matici_DP, pis_matici_I
end interface

interface pis_t_matici
    module procedure pis_t_matici_SP, pis_t_matici_DP, pis_t_matici_I
end interface

contains


! ================== vstup ===================================

subroutine cti_cislo_I(utext,c)
    character(Len=*),intent(in) :: utext
    integer,intent(out) :: c
    write(unit=*,fmt="(a)",advance="no")utext
    read *,c
end subroutine cti_cislo_I

subroutine cti_cislo_SP(utext,c)
    character(Len=*),intent(in) :: utext
    real(kind=SP),intent(out) :: c
    write(unit=*,fmt="(a)",advance="no")utext
    read *,c
end subroutine cti_cislo_SP

subroutine cti_cislo_DP(utext,c)
    character(Len=*),intent(in) :: utext
    real(kind=DP),intent(out) :: c
    write(unit=*,fmt="(a)",advance="no")utext
    read *,c
end subroutine cti_cislo_DP


subroutine cti_vektor_I(prvku,V)
    integer,intent(in) :: prvku
    integer,dimension(prvku),intent(out) :: V
    
    integer :: i
    
    read(unit=*,fmt=*)(V(i),i=1,prvku)
end subroutine cti_vektor_I

subroutine cti_vektor_SP(prvku,V)
    integer,intent(in) :: prvku
    real(kind=SP),dimension(prvku),intent(out) :: V
    
    integer :: i      
    
    read(unit=*,fmt=*)(V(i),i=1,prvku)
end subroutine cti_vektor_SP

subroutine cti_vektor_DP(prvku,V)
    integer,intent(in) :: prvku
    real(kind=DP),dimension(prvku),intent(out) :: V
    
    integer :: i    
    
    read(unit=*,fmt=*)(V(i),i=1,prvku)
end subroutine cti_vektor_DP


subroutine cti_matici_I(radku,sloupcu,A)
    integer,intent(in) :: radku, sloupcu
    integer,dimension(radku,sloupcu),intent(out) :: A
    
    integer :: i
    
    read(unit=*,fmt=*)(A(i,:),i=1,radku)
end subroutine cti_matici_I

subroutine cti_matici_SP(radku,sloupcu,A)
    integer,intent(in) :: radku, sloupcu
    real(kind=SP),dimension(radku,sloupcu),intent(out) :: A
    
    integer :: i
    
    read(unit=*,fmt=*)(A(i,:),i=1,radku)
end subroutine cti_matici_SP

subroutine cti_matici_DP(radku,sloupcu,A)
    integer,intent(in) :: radku, sloupcu
    real(kind=DP),dimension(radku,sloupcu),intent(out) :: A
    
    integer :: i
    
    read(unit=*,fmt=*)(A(i,:),i=1,radku)
end subroutine cti_matici_DP

!======================= vystup ==============================

subroutine pis_vektor_I(prvku,V,FmtRetez,transp)
    ! Je-li pritomno 'transp', vypisuje do radku
    integer,intent(in) :: prvku
    Integer,dimension(prvku),intent(IN) :: V
    character(LEN=*),intent(IN) :: FmtRetez
    logical,optional,Intent(in) :: transp
    
    integer :: i
    
    if(present(transp)) THEN  !vystup do radku
        do i = 1,prvku
            write(unit=*,fmt=FmtRetez,advance="no")V(i)
        end do
    else
        do i = 1,prvku
            write(unit=*,fmt=FmtRetez,advance="yes")V(i)
        end do
    end if
    print *    
end subroutine pis_vektor_I  

subroutine pis_vektor_SP(prvku,V,FmtRetez,transp)
    integer,intent(in) :: prvku
    real(kind=SP),dimension(prvku),intent(IN) :: V
    character(LEN=*),intent(IN) :: FmtRetez
    logical,optional,Intent(in) :: transp
    
    integer :: i
    
    if(present(transp)) THEN  !vystup do radku
        do i = 1,prvku
            write(unit=*,fmt=FmtRetez,advance="no")V(i)
        end do
    else
        do i = 1,prvku
            write(unit=*,fmt=FmtRetez,advance="yes")V(i)
        end do
    end if
    print *    
end subroutine pis_vektor_SP

subroutine pis_vektor_DP(prvku,V,FmtRetez,transp)
    integer,intent(in) :: prvku
    real(kind=DP),dimension(prvku),intent(IN) :: V
    character(LEN=*),intent(IN) :: FmtRetez
    logical,optional,Intent(in) :: transp
    
    integer :: i
    
    if(present(transp)) THEN  !vystup do radku
        do i = 1,prvku
            write(unit=*,fmt=FmtRetez,advance="no")V(i)
        end do
    else
        do i = 1,prvku
            write(unit=*,fmt=FmtRetez,advance="yes")V(i)
        end do
    end if
    print *    
end subroutine pis_vektor_DP  

subroutine pis_matici_I(radku,sloupcu,A,FmtRetez)
    integer,intent(in) :: radku, sloupcu
    Integer,dimension(radku,sloupcu),intent(IN) :: A
    character(LEN=*),intent(IN) :: FmtRetez
        
    integer :: i,j
    
    do i = 1,radku
        do j = 1,sloupcu
            write(unit=*,fmt=FmtRetez,advance="no")A(i,j)
        end do
        print *
    end do
end subroutine pis_matici_I  

subroutine pis_matici_SP(radku,sloupcu,A,FmtRetez)
    integer,intent(in) :: radku, sloupcu
    real(kind=SP),dimension(radku,sloupcu),intent(IN) :: A
    character(LEN=*),intent(IN) :: FmtRetez
        
    integer :: i,j
    
    do i = 1,radku
        do j = 1,sloupcu
            write(unit=*,fmt=FmtRetez,advance="no")A(i,j)
        end do
        print *
    end do
end subroutine pis_matici_SP   

subroutine pis_matici_DP(radku,sloupcu,A,FmtRetez)
    integer,intent(in) :: radku, sloupcu
    real(kind=DP),dimension(radku,sloupcu),intent(IN) :: A
    character(LEN=*),intent(IN) :: FmtRetez
        
    integer :: i,j
    
    do i = 1,radku
        do j = 1,sloupcu
            write(unit=*,fmt=FmtRetez,advance="no")A(i,j)
        end do
        print *
    end do
end subroutine pis_matici_DP    

! ===== vypis trojuhelnikove matice =====

subroutine pis_t_matici_I(dimA,A1,FmtRetez,jak)
    integer,intent(in) :: dimA
    integer,dimension(dimA*(dimA+1)/2),intent(IN) :: A1
    character(LEN=*),intent(IN) :: FmtRetez
    character(len=1),optional,intent(in) :: jak     ! "s" spodni, "h" horni
        
    integer :: i,j,q
    character(len=1) :: hd
    
    if (.not.present(jak)) then
        hd = "S"
    else
        hd = jak
    end if
    if (hd == "h" .or. hd == "H") hd = "H"    ! jen v tomto pripade horni
    
    q = 1
    if (hd == "H") Then
        do i = 1,dimA
            do j = 1,dimA-i+1
                write(unit=*,fmt=FmtRetez,advance="no")A1(q)
                q = q+1
            end do
            print *
        end do
    else
        do i = 1,dimA
            do j = 1,i
                write(unit=*,fmt=FmtRetez,advance="no")A1(q)
                q = q+1
            end do
            print *
        end do
    end if
end subroutine pis_t_matici_I


subroutine pis_t_matici_SP(dimA,A1,FmtRetez,jak)
    integer,intent(in) :: dimA
    real(kind=SP),dimension(dimA*(dimA+1)/2),intent(IN) :: A1
    character(LEN=*),intent(IN) :: FmtRetez
    character(len=1),optional,intent(in) :: jak     ! "s" spodni, "h" horni
        
    integer :: i,j,q
    character(len=1) :: hd
    
    if (.not.present(jak)) then
        hd = "S"
    else
        hd = jak
    end if
    if (hd == "h" .or. hd == "H") hd = "H"    ! jen v tomto pripade horni
    
    q = 1
    if (hd == "H") Then
        do i = 1,dimA
            do j = 1,dimA-i+1
                write(unit=*,fmt=FmtRetez,advance="no")A1(q)
                q = q+1
            end do
            print *
        end do
    else
        do i = 1,dimA
            do j = 1,i
                write(unit=*,fmt=FmtRetez,advance="no")A1(q)
                q = q+1
            end do
            print *
        end do
    end if
end subroutine pis_t_matici_SP



subroutine pis_t_matici_DP(dimA,A1,FmtRetez,jak)
    integer,intent(in) :: dimA
    real(kind=DP),dimension(dimA*(dimA+1)/2),intent(IN) :: A1
    character(LEN=*),intent(IN) :: FmtRetez
    character(len=1),optional,intent(in) :: jak     ! "s" spodni, "h" horni
        
    integer :: i,j,q
    character(len=1) :: hd
    
    if (.not.present(jak)) then
        hd = "S"
    else
        hd = jak
    end if
    if (hd == "h" .or. hd == "H") hd = "H"    ! jen v tomto pripade horni
    
    q = 1
    if (hd == "H") Then
        do i = 1,dimA
            do j = 1,dimA-i+1
                write(unit=*,fmt=FmtRetez,advance="no")A1(q)
                q = q+1
            end do
            print *
        end do
    else
        do i = 1,dimA
            do j = 1,i
                write(unit=*,fmt=FmtRetez,advance="no")A1(q)
                q = q+1
            end do
            print *
        end do
    end if
end subroutine pis_t_matici_DP
            
    
    
   
 
end module VstupVystup
