-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindice_libro3.go
118 lines (107 loc) · 1.93 KB
/
indice_libro3.go
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
package main
import (
. "fmt"
. "strconv"
)
type ele_ind struct {
titulo string
nivel int
}
//devuelve 10,100,1000.......
func ceros(n int) (dec int) {
var s string = "1"
for i := 1; i < n; i++ {
s = s + "0"
}
dec, _ = Atoi(s)
return
}
//pone puntos en numeros de indice
func pp(st string) (s string) {
for _, v := range st {
s = s + Sprint(string(v), ".")
}
return
}
//crea array con numeros de indice
func cind(a []ele_ind, ai *[]int) {
for _, v := range a {
*ai = append(*ai, v.nivel)
}
}
//crea array con arbol de numeros de indice
func ctind(ai []int, ind *[]string) {
s := ""
ant := make(map[int]string)
ante := 0
i := 1
for _, v := range ai {
if ante == 0 {
ante = v
s = Itoa(v)
*ind = append(*ind, s)
ant[v] = s
} else if v > ante {
i = 1
ante = v
s = ant[v-1] + Itoa(i)
*ind = append(*ind, s)
ant[v] = s
i++
} else if v == ante {
s = ant[v-1] + Itoa(i)
*ind = append(*ind, s)
ant[v] = s
i++
} else if v < ante {
i, _ = Atoi(ant[v])
if v != 1 {
ante = v
i = (i % ceros(v)) + 1
s = ant[v-1] + Itoa(i)
*ind = append(*ind, s)
ant[v] = s
i++
} else {
ante = v + 1
i = i + 1
s = Itoa(i)
*ind = append(*ind, s)
ant[v] = s
i = 1
}
}
}
}
//crea el numero de espacios segun el nivel
//devolviendo una cadena de espacios
func tab(n int) string {
esp := " "
for i := 0; i < n; i++ {
esp = esp + " "
}
return esp
}
//imprime tabla contenido
func tcont(a []ele_ind, ind []string) {
for i, v := range a {
Println(tab(v.nivel), pp(ind[i]), v.titulo)
}
}
func main() {
ai := []int{}
ind := []string{}
cont := []ele_ind{{"Introducción", 1},
{"Motivación", 2},
{"Reseña histórica", 2},
{"Origen", 3},
{"Trabajos preliminares", 3},
{"Soluciones actuales", 2},
{"Objetivo de este libro", 2},
{"Requisitos", 1},
{"Hardware", 2},
{"Software", 2}}
cind(cont, &ai)
ctind(ai, &ind)
tcont(cont, ind)
}