! Reprezentarea in Format a nr. intregi
!
Program Main_Represent
use ifcore  ! cvf: use dflib

logical pressed

integer(1) I1, max_i1, min_i1;
integer(2) I2, max_i2, min_i2;
integer(4) I4, max_i4, min_i4;
integer(8) I8, max_i8, min_i8;
integer(8) xi; real(8) xi8

character(8) C, fname*100, kind_char*1
integer width, width8

print '(a,\)', ' INTEGER(kind). Introdu "kind" (1;2;4;8): '
read*, ki

if(ki /=1 .and. ki /=2 .and. ki /=4 .and. ki /=8) then
ki =4
write(*,*) ' "kind" invalid!'
write(*,*) ' S-a luat kind =4.' 
endif

print '(/,a)', ' Nume fisier de iesire = "Repr_Int-C.rez".'
print '(a,\)', ' Introdu "C" (<= 8 char): '
read '(a)', C

if(C /=' ') then
fname ='Repr_Int-'//trim(C)//'.rez'
else
fname ='Repr_Int.rez'
endif

open(1, file =fname, status ='unknown', form ='formatted')
write(1, '(a)') ' Reprezentarea numerelor intregi'
write(kind_char, '(I1)') ki

write(1, '(a)') ' Tipul: Integer('//kind_char//')'

i1 =1_1; i2 =1_2; i4 =1_4; i8 =1_8; 
max_i1 =huge(i1); min_i1 =-max_i1 -1;
max_i2 =huge(i2); min_i2 =-max_i2 -1;
max_i4 =huge(i4); min_i4 =-max_i4 -1;
max_i8 =huge(i8); min_i8 =-max_i8 -1;

select case(ki)
case(1)
write(*, '(/,a,I4)') ' Max: ', max_i1
write(*, '(a,I4,/)') ' Min: ', min_i1
write(1, '(a,I4)') ' Max: ', max_i1
write(1, '(a,I4,/)') ' Min: ', min_i1

case(2)
write(*, '(/,a,I7)') ' Max: ', max_i2
write(*, '(a,I7,/)') ' Min: ', min_i2
write(1, '(a,I7)') ' Max: ', max_i2
write(1, '(a,I7,/)') ' Min: ', min_i2

case(4)
write(*, '(/,a,I13, a,1pg9.3,a)') ' Max: ', max_i4, '  (', real(max_i4), ')'
write(*, '(a,I13,/)') ' Min: ', min_i4
write(1, '(a,I13, a,1pg9.3,a)') ' Max: ', max_i4, '  (', real(max_i4), ')'
write(1, '(a,I13,/)') ' Min: ', min_i4

case(8)
write(*, '(/,a,I20, a,1pg9.3,a)') ' Max: ', max_i8, '  (', real(max_i8), ')'
write(*, '(a,I20,/)') ' Min: ', min_i8
write(1, '(a,I20, a,1pg9.3,a)') ' Max: ', max_i8, '  (', real(max_i8), ')'
write(1, '(a,I20,/)') ' Min: ', min_i8
end select

write(1,'(a)') 'I = intreg'
write(1,'(a)') 'bx = reprezentare in format'
write(1,'(a)') 'S|int = bit de semn|intreg stocat'
write(1,'(a)') '---------------------------------'

100 print '(a,\)', ' Introdu INT (intreg): '

select case(ki)
case(1)
read *, xi
if( xi >max_i1 .OR. xi <min_i1 ) then
write(*,*) ' "INT" invalid!'

iwxi =width8(xi)
write(1,'(a, I<iwxi>)') 'I: ', xi;
write(1, '(a,/)') ' "INT" invalid!'
goto 100
endif

i1 =int1(xi)

case(2)
read *, xi
if( xi >max_i2 .OR. xi <min_i2 ) then
write(*,*) ' "INT" invalid!'

iwxi =width8(xi)
write(1,'(a, I<iwxi>)') 'I: ', xi;
write(1, '(a,/)') ' "INT" invalid!'
goto 100
endif

i2 =int2(xi)

case(4)
read *, xi
if( xi >max_i4 .OR. xi <min_i4 ) then
write(*,*) ' "INT" invalid!'

iwxi =width8(xi)
write(1,'(a, I<iwxi>)') 'I: ', xi;
write(1, '(a,/)') ' "INT" invalid!'
goto 100
endif

i4 =int4(xi)

case(8)

read *, xi8
if( xi8 >max_i8 .OR. xi8 <min_i8 ) then
write(*,*) ' "INT" invalid!'

write(1, *) 'I: ', xi8;
write(1, '(a,/)') ' "INT" invalid!'
goto 100
endif

i8 =int8(xi8)

end select

call Represent(ki, i1, i2, i4, i8)
write(1,*)

200 print '(//,a,\)', ' Alt "INT" (Y,y/char): '
read '(a1)', char1

if (char1 =='Y' .or. char1 =='y') then
goto 100
endif

write(*, '(a)') ' Apasa orice tasta'
DO WHILE (.NOT. pressed)   
pressed = PEEKCHARQQ ( )
END DO

ENDProgram Main_Represent
