Skip to content
Snippets Groups Projects
Commit 4ba80bed authored by dali662d's avatar dali662d
Browse files

Upload New File

parent b1e8ebe2
No related branches found
No related tags found
No related merge requests found
module einkauf_mod
implicit none
private
public :: produkt, liste, ausgabe, delete
type produkt
character (len=15) :: name
integer :: menge
type(produkt), pointer :: next => NULL()
end type
type liste
integer :: laenge
type(produkt), pointer :: head => NULL()
end type
contains
subroutine ausgabe (shoppinglist)
type(liste), intent(in) :: shoppinglist
type(produkt), pointer :: current
current => shoppinglist%head
do while (associated(current))
write(*,'(A,X,I3)') current%name, current%menge
current => current%next
end do
end subroutine ausgabe
subroutine delete(shoppinglist)
type(liste), intent(inout) :: shoppinglist
type(produkt), pointer :: current, help
current => shoppinglist%head
shoppinglist%head => NULL()
do
help => current%next
deallocate(current)
current => help
if (.not. associated(current)) exit
end do
end subroutine delete
end module einkauf_mod
program einkaufsliste
use einkauf_mod
implicit none
type(liste) :: shoppinglist
!call build_mit_laenge(shoppinglist)
call build_ohne_laenge(shoppinglist)
call ausgabe(shoppinglist)
call delete(shoppinglist)
contains
subroutine build_mit_laenge(shoppinglist)
type(liste), intent(out) :: shoppinglist
type(produkt), pointer :: current
integer :: i
write(*,*) "Wie viele Elemente sollen auf die Liste kommen?"
read(*,*) shoppinglist%laenge
allocate(shoppinglist%head)
current => shoppinglist%head
write(*,*) "Gebe das erste Element ein und die gewuenschte Menge:"
read(*,*) current%name, current%menge
do i = 2, shoppinglist%laenge
allocate(current%next)
current => current%next
write(*,'(A,X,I3,X,A)') "Element", i, " und Menge:"
read(*,*) current%name, current%menge
end do
end subroutine build_mit_laenge
subroutine build_ohne_laenge(shoppinglist)
type(liste), intent(out) :: shoppinglist
type(produkt), pointer :: current
character :: choice
allocate(shoppinglist%head)
current => shoppinglist%head
write(*,*) "Gebe das erste Element ein und die gewuenschte Menge:"
read(*,*) current%name, current%menge
shoppinglist%laenge = 1
do
write(*,*) "Ein weiteres Element? (j/n)"
read(*,*) choice
if (choice == "n") exit
allocate(current%next)
current => current%next
write(*,*) "Name und Menge :"
read(*,*) current%name, current%menge
shoppinglist%laenge = shoppinglist%laenge + 1
end do
write(*,'(A,I3,A)') "Es wurden ", shoppinglist%laenge, " Elemente eingetragen."
end subroutine build_ohne_laenge
end program einkaufsliste
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment