1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
program mendeleev
implicit none
character(len=2), dimension(0:118), parameter :: ELEMENTS = (/ "? ", &
"Ac", "Ag", "Al", "Am", "Ar", "As", "At", "Au", "B ", "Ba", "Be", "Bh", &
"Bi", "Bk", "Br", "C ", "Ca", "Cd", "Ce", "Cf", "Cl", "Cm", "Cn", "Co", &
"Cr", "Cs", "Cu", "Db", "Ds", "Dy", "Er", "Es", "Eu", "F ", "Fe", "Fl", &
"Fm", "Fr", "Ga", "Gd", "Ge", "H ", "He", "Hf", "Hg", "Ho", "Hs", "I ", &
"In", "Ir", "K ", "Kr", "La", "Li", "Lr", "Lu", "Lv", "Mc", "Md", "Mg", &
"Mn", "Mo", "Mt", "N ", "Na", "Nb", "Nd", "Ne", "Nh", "Ni", "No", "Np", &
"O ", "Og", "Os", "P ", "Pa", "Pb", "Pd", "Pm", "Po", "Pr", "Pt", "Pu", &
"Ra", "Rb", "Re", "Rf", "Rg", "Rh", "Rn", "Ru", "S ", "Sb", "Sc", "Se", &
"Sg", "Si", "Sm", "Sn", "Sr", "Ta", "Tb", "Tc", "Te", "Th", "Ti", "Tl", &
"Tm", "Ts", "U ", "V ", "W ", "Xe", "Y ", "Yb", "Zn", "Zr" /)
type :: formula_t
integer :: tail = 1
integer :: n = 0
integer, dimension(:), allocatable :: elements
type(formula_t), pointer :: next => null()
end type formula_t
type(formula_t), pointer :: formula, f
character(len=:), allocatable :: word
integer :: length, argc, i, j
argc = command_argument_count()
do i = 1, argc
call get_command_argument(i, length=length)
allocate(character(len=length) :: word)
call get_command_argument(i, value=word)
write (*, "(A, ':')") word
formula => explode(word)
f => formula
do while (associated(f))
if (f%n > 0) then
do j = 1, f%n
write (*, "(' ', A)", advance="no") trim(ELEMENTS(f%elements(j)))
end do
write (*, "()")
end if
f => f%next
end do
call free_formula(formula)
deallocate(word)
end do
contains
pure subroutine free_formula(formula)
type(formula_t), pointer, intent(in out) :: formula
type(formula_t), pointer :: next
do while (associated(formula))
next => formula%next
deallocate(formula%elements)
deallocate(formula)
formula => next
end do
end subroutine free_formula
pure integer function tolower(c)
character(len=1), intent(in) :: c
tolower = ior(32, iachar(c))
end function tolower
pure subroutine search(start, length, sh, c)
integer, intent(in out) :: start, length
integer, intent(in) :: sh
character(len=1), intent(in) :: c
integer :: l, m, u, c_
c_ = tolower(c)
u = start + length
l = start
do while (l < u)
m = (u + l) / 2
if (tolower(ELEMENTS(m)(sh:sh)) < c_) then
l = m + 1
else
u = m
endif
end do
if (l == start + length) then
length = 0
return
end if
if (tolower(ELEMENTS(l)(sh:sh)) /= c_) then
length = 0
return
end if
u = start + length
start = l
do while (l < u)
m = (u + l) / 2
if (c_ < tolower(ELEMENTS(m)(sh:sh))) then
u = m
else
l = m + 1
endif
end do
length = u - start
end subroutine search
pure subroutine advance(word, formula)
character(len=*), intent(in) :: word
type(formula_t), pointer, intent(in out) :: formula
integer :: n, tail
integer :: start, length, sh, c
type(formula_t), pointer :: f, g
f => formula
tail = f%tail
n = f%n
sh = 0
start = 1
length = ubound(ELEMENTS, 1)
do
c = tail + sh
if (len(word) < c) exit
sh = sh + 1
call search(start, length, sh, word(c:c))
if (length == 0) exit
if (sh == len_trim(ELEMENTS(start))) then
if (n /= f%n) then
allocate(g)
allocate(g%elements(len(word)))
g%n = n
g%elements(1:n) = f%elements(1:n)
g%next => f%next
f%next => g
f => g
end if
f%n = f%n + 1
f%elements(f%n) = start
f%tail = c + 1
start = start + 1
length = length - 1
end if
end do
if (n == f%n) then
f%tail = f%tail + 1
f%n = f%n + 1
f%elements(f%n) = 0
end if
end subroutine advance
pure function explode(word) result(formula)
character(len=*), intent(in) :: word
type(formula_t), pointer :: formula
type(formula_t), pointer :: f
allocate(formula)
allocate(formula%elements(len(word)))
f => formula
do while (associated(f))
do while (f%tail <= len(word))
call advance(word, f)
end do
f => f%next
end do
end function explode
end program mendeleev
|