-
Notifications
You must be signed in to change notification settings - Fork 0
/
IPPP.go
222 lines (206 loc) · 5.04 KB
/
IPPP.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
var MI [50]TModuleInterface //массив описаний интерфейсов модулей
var RP [50]TRPObject //массив элементов рабочей памяти пакета
var stack Stack
var checkingArray [10]int
// атрибут ПО (элемент рабочей памяти)
type TRPObject struct {
ident string // условный идентификатор атрибута в рамках МПО
name string // реальное название атрибута ПО
value float64 // значение атрибута ПО
isCalc bool // признак: вычислен или нет
}
type TModuleInterface struct {
moduleIdent string // условный идентификатор модуля
moduleName string // смысловое название модуля
moduleOutputParam string // номера выходных параметров
moduleInputParam string // номера входных параметров
}
func returnIndexInRP(s string) string {
//fmt.Printf("s:%s l:%d", s, len(s))
for i := range RP {
if RP[i].ident == s {
return strconv.Itoa(i)
}
}
return ""
}
func contains(e int) bool {
for _, a := range checkingArray {
if a == e {
return true
}
}
return false
}
func arrayPush(e int) {
for i := range checkingArray {
if checkingArray[i] == -1 {
checkingArray[i] = e
return
}
}
}
func clearArr() {
for i := range checkingArray {
checkingArray[i] = -1
}
}
func parseTxt(fileName string) {
file, _ := os.Open(fileName)
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Scan() //should skip "#1"
for scanner.Scan() {
if scanner.Text() != "#2" {
temp := strings.Split(scanner.Text(), ":")
i, _ := strconv.Atoi(strings.Trim(temp[0], " "))
RP[i] = TRPObject{ident: strings.Trim(temp[1], " "), name: strings.Trim(temp[2], " "), isCalc: false}
} else {
break
}
}
for scanner.Scan() {
tempObj := TModuleInterface{moduleIdent: "", moduleName: "", moduleOutputParam: "", moduleInputParam: ""}
temp := strings.Split(scanner.Text(), ":")
tempObj.moduleName = strings.Trim(temp[1], " ")
temp = strings.Split(temp[0], "=")
tempObj.moduleOutputParam = returnIndexInRP(temp[0])
temp = strings.Split(temp[1], "(")
tempObj.moduleIdent = strings.Trim(temp[0], " ")
temp = strings.Split(temp[1], ",")
temp[len(temp)-1] = temp[len(temp)-1][:len(temp[len(temp)-1])-2]
for i := range temp {
tempObj.moduleInputParam += returnIndexInRP(temp[i])
}
index, _ := strconv.Atoi(tempObj.moduleIdent[1:])
MI[index] = tempObj
}
}
func Solver(s int) {
cont := false
if RP[s].isCalc {
return
}
for i := range MI {
cont = false
outParam, _ := strconv.Atoi(MI[i].moduleOutputParam)
if outParam == s {
for j := 0; j < len(MI[i].moduleInputParam); j++ {
index, _ := strconv.Atoi(string(MI[i].moduleInputParam[j]))
if !RP[index].isCalc {
cont = true
break
}
}
if !cont {
RP[s].isCalc = true
stack.Push(MI[i].moduleIdent)
return
}
}
}
for i := range MI {
outParam, _ := strconv.Atoi(MI[i].moduleOutputParam)
arrayPush(s)
if outParam == s {
stack.Push(MI[i].moduleIdent)
fmt.Printf("%s\n", MI[i].moduleIdent)
for j := 0; j < len(MI[i].moduleInputParam); j++ {
index, _ := strconv.Atoi(string(MI[i].moduleInputParam[j]))
if contains(index) {
stack.Pop()
cont = true
break
}
if !RP[index].isCalc {
cont = false
Solver(index)
}
}
if cont {
continue
}
RP[s].isCalc = true
return
}
}
}
func parseRequest(fileName string) float64 {
file, _ := os.Open(fileName)
defer file.Close()
clearArr()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if scanner.Text() != "?" {
s := strings.Split(scanner.Text(), "=")
for i := range RP {
if RP[i].ident == s[0] {
value, _ := strconv.ParseFloat(s[1], 64)
RP[i].value = value
RP[i].isCalc = true
}
}
} else {
break
}
}
scanner.Scan()
rez, _ := strconv.Atoi(returnIndexInRP(scanner.Text()))
Solver(rez)
calcRes()
return RP[rez].value
}
func calcRes() {
for stack.Len() > 0 {
switch stack.Pop() {
case "F1":
fmt.Printf("F1: %s\n", MI[1].moduleName)
F1()
case "F2":
fmt.Printf("F2: %s\n", MI[2].moduleName)
F2()
case "F3":
fmt.Printf("F3: %s\n", MI[3].moduleName)
F3()
case "F4":
fmt.Printf("F4: %s\n", MI[4].moduleName)
F4()
case "F5":
fmt.Printf("F5: %s\n", MI[5].moduleName)
F5()
case "F6":
fmt.Printf("F6: %s\n", MI[6].moduleName)
F7()
case "F7":
fmt.Printf("F7: %s\n", MI[7].moduleName)
F7()
case "F8":
fmt.Printf("F8: %s\n", MI[8].moduleName)
F8()
case "F9":
fmt.Printf("F9: %s\n", MI[9].moduleName)
F9()
case "F10":
fmt.Printf("F10: %s\n", MI[10].moduleName)
F10()
case "F11":
fmt.Printf("F11: %s\n", MI[11].moduleName)
F11()
case "F12":
fmt.Printf("F12: %s\n", MI[12].moduleName)
F12()
}
}
}
func main() {
parseTxt("data/1.txt")
fmt.Printf("%f", parseRequest("data/input.txt"))
}