Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PROG-material-public
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Walter, Wolfgang
PROG-material-public
Commits
94fe0478
Commit
94fe0478
authored
1 year ago
by
Jonas Riedel
Browse files
Options
Downloads
Patches
Plain Diff
Add new file
parent
b7dbd0e3
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Uebung/SoSe2023/chatgpt_tuerme.f95
+75
-0
75 additions, 0 deletions
Uebung/SoSe2023/chatgpt_tuerme.f95
with
75 additions
and
0 deletions
Uebung/SoSe2023/chatgpt_tuerme.f95
0 → 100644
+
75
−
0
View file @
94fe0478
PROGRAM
TowersOfHanoi
USE
STACKMOD
IMPLICIT
NONE
INTEGER
,
PARAMETER
::
num_discs
=
3
TYPE
(
disc
)
::
discs
(
num_discs
)
INTEGER
::
i
! Initialize the stacks
TYPE
(
STACK
)
::
source_stack
,
auxiliary_stack
,
destination_stack
CALL
INIT
(
source_stack
)
CALL
INIT
(
auxiliary_stack
)
CALL
INIT
(
destination_stack
)
! Create discs and push them onto the source stack
DO
i
=
num_discs
,
1
,
-1
discs
(
i
)
%
num
=
i
discs
(
i
)
%
color
=
i
discs
(
i
)
%
diam
=
2
*
i
CALL
PUSH
(
source_stack
,
discs
(
i
))
END
DO
! Call the recursive subroutine to solve the Towers of Hanoi puzzle
WRITE
(
*
,
*
)
"Initial State:"
WRITE
(
source_stack
)
WRITE
(
auxiliary_stack
)
WRITE
(
destination_stack
)
WRITE
(
*
,
*
)
CALL
SolveTowersOfHanoi
(
num_discs
,
source_stack
,
auxiliary_stack
,
destination_stack
)
! Clean up memory
CALL
DELETE
(
source_stack
)
CALL
DELETE
(
auxiliary_stack
)
CALL
DELETE
(
destination_stack
)
END
PROGRAM
TowersOfHanoi
SUBROUTINE
SolveTowersOfHanoi
(
n
,
source
,
auxiliary
,
destination
)
USE
STACKMOD
IMPLICIT
NONE
INTEGER
,
INTENT
(
IN
)
::
n
TYPE
(
STACK
),
INTENT
(
INOUT
)
::
source
,
auxiliary
,
destination
TYPE
(
disc
)
::
disc
IF
(
n
==
1
)
THEN
! Move the top disc from the source to the destination peg
CALL
POP
(
source
,
disc
)
CALL
PUSH
(
destination
,
disc
)
WRITE
(
*
,
*
)
"Move disc "
,
disc
%
num
,
" from peg "
,
CHAR
(
source
),
" to peg "
,
CHAR
(
destination
)
WRITE
(
source
)
WRITE
(
auxiliary
)
WRITE
(
destination
)
WRITE
(
*
,
*
)
ELSE
! Recursive case:
! 1. Move (n-1) discs from source to auxiliary peg using destination peg as the auxiliary
CALL
SolveTowersOfHanoi
(
n
-
1
,
source
,
destination
,
auxiliary
)
! 2. Move the nth disc from source to destination peg
CALL
POP
(
source
,
disc
)
CALL
PUSH
(
destination
,
disc
)
WRITE
(
*
,
*
)
"Move disc "
,
disc
%
num
,
" from peg "
,
CHAR
(
source
),
" to peg "
,
CHAR
(
destination
)
WRITE
(
source
)
WRITE
(
auxiliary
)
WRITE
(
destination
)
WRITE
(
*
,
*
)
! 3. Move (n-1) discs from auxiliary peg to destination peg using source peg as the auxiliary
CALL
SolveTowersOfHanoi
(
n
-
1
,
auxiliary
,
source
,
destination
)
END
IF
END
SUBROUTINE
SolveTowersOfHanoi
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment