Using the GNU compiler with Linux in a terminal in the same directory as the source code
save the module as modpers_struct.f and the program as phonebook.f then
gfortran modpers_struct.f phonebook.f
./a.out to run the program and mv a.out newprogramname to rename it.
With Windows using the MinGW shell (GNU C and gfortran)
save the module as modpers_struct.f and the program as phonebook.f
The path to save the fortran files in MinGW is MinGW / msys / 1.0 / home / computer
From the shell from Windows menu type the following to compile
gfortran -static modpers_struct.f phonebook.f
./a.exe to run the program and mv a.exe newprogramname.exe to rename it.
The program can be copied to the desktop and run by right clicking on the icon / program name.
Notice in the listings, there must be seven spaces from the beginning of the line to the start of the code on that line.
module modpers_struct
save
c implicit none
type voter
character *15 :: contact
character *15 :: phoneno
end type voter
end module modpers_struct
program phonebook
use modpers_struct
type(voter), dimension(1000) :: member
integer kounter
integer :: choice
kounter = 0
choice = 0
do
print*," "
print*,"1. Create a new file."
10 print*,"2. Add a record."
print*,"3. Search for a record."
print*,"4. Save records."
print*,"5. List records."
print*,"6. Load file."
print*,"7. Sort records."
print*,"8. Delete a record."
print*,"9. Exit program."
print*," Enter a choice."
print*," "
read*,choice
if(choice.EQ.1) then
call newfile
end if
if(choice.EQ.2) then
call addrecords(kounter, member)
end if
if(choice.EQ.3) then
call retrieve(kounter, member)
end if
if(choice.EQ.4) then
call saverecords(kounter, member)
endif
if(choice.EQ.5) then
call listrecords(kounter, member)
end if
if(choice.EQ.6) then
call loadrecords(kounter, member)
end if
if(choice.EQ.7)then
call sort(kounter, member)
end if
if(choice.EQ.8) then
call deleterecord(kounter, member)
end if
if(choice.EQ.9) exit
end do
end program phonebook
subroutine newfile
open(unit = 20, file = 'Fortran1.txt',status='new')
rewind = 20
common numofrecs = 0
write(20,*),numofrecs
endfile 20
rewind 20
close(20)
print*," "
print *,"New file created."
end
subroutine retrieve(kounter, member)
use modpers_struct
type(voter), dimension (1000) :: member
character *15 :: person
integer :: j
integer :: flag
flag = 0
print*,"Enter a name."
read(*,'(A15)')person
j=kounter
do i=1,j
if(member(i)%contact.eq.person)then
flag = 1
print*," "
print*,"Record found."
print*,member(i)%contact, member(i)%phoneno
print*," "
end if
end do
if(flag == 0) then
print*," "
print*,"Record not found."
end if
end
subroutine saverecords(kounter, member)
use modpers_struct
type(voter), dimension (1000) :: member
integer i, j
open(unit =20, file = 'Fortran1.txt', access = 'sequential')
rewind 20
write(20,*)kounter
j = kounter
do i = 1,j
2 format(A15,A15)
write(20,2)member(i)%contact,member(i)%phoneno
end do
endfile 20
rewind 20
close(20)
print*," "
print*,"File saved."
end
subroutine addrecords(kounter, member)
use modpers_struct
type(voter), dimension(1000) ,intent(inout)::member
character(1) :: yn
c integer,intent(inout) :: Kount
yn=''
print*," "
do
Print*,"Do you wish to input a record (y)es or (n)o?"
read(*,'(a1)') yn
if (yn=='y' .or. yn=='Y') then
kounter = kounter + 1
print*,"Enter name."
read(*,'(a15)') member(kounter)%contact
print*,"Enter phone number."
read(*,53) member(kounter)%phoneno
53 format(a15)
else
exit
end if
end do
end
subroutine listrecords(kounter, member)
use modpers_struct
type(voter), dimension(1000) :: member
character(1) :: yn
Integer i, j
j=kounter
print*," "
print*,'The number of records is ',j
print*," "
do i = 1,j
print*,"Record number ",i
print*, member(i)%contact, member(i)%phoneno
end do
end
subroutine loadrecords(kounter, member)
use modpers_struct
type(voter), dimension (1000) :: member
integer i, j
open(unit =20, file = 'Fortran1.txt', access = 'sequential')
rewind 20
read(20,*)kounter
j=kounter
do i = 1,j
1 format(A15,A15)
read(20,1)member(i)%contact,member(i)%phoneno
end do
endfile 20
rewind 20
close(20)
print*," "
print*,"File loaded."
print*,"Number of records ", Kounter
end
Subroutine sort(kounter, member)
use modpers_struct
type(voter), dimension (1000) :: member
type(voter) :: temp
integer i, j, k
k = kounter
do i =1,k-1
do j =i+1, k
if(member(i)%contact.gt. member(j)%contact) then
temp = member(i)
member(i) = member(j)
member(j) = temp
end if
end do
end do
print*," "
print*,"Records sorted."
end
subroutine deleterecord(kounter, member)
use modpers_struct
type(voter), dimension (1000) :: member
integer i, j, k, n
integer flag
character *15 :: person
character(1) yn
yn=''
print*," "
print*,"Do you wish to delete a record (y) or (n)?"
read(*,'(a1)') yn
if (yn=='y' .or. yn=='Y') then
print*,"Enter a record number to delete. "
read(*, 40) n
40 format(I5)
k = kounter
do j = n,k-1
member(j)=member(j+1)
end do
print*,"Record deleted."
kounter = kounter - 1
end if
end