You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
lavos/cap3/work6.go

96 lines
2.0 KiB

package cap3
import (
"fmt"
"strings"
)
//
//func Work6(input string) string {
//
//}
var before bool = false
func printString(str string) {
temp := ""
for i := 0; i < len(str); i++ {
if i == 0 {
before = false
} else {
if str[i-1] == '*' || str[i-1] == '/' {
before = true
} else {
before = false
}
}
if str[i] == '(' {
temp = getString(str[i:])
if i == 0 {
str = temp
} else {
str = str[:i] + temp
}
}
}
fmt.Println(str)
}
func getString(str string) string {
result := str
temp := ""
i := 0
length := 0
add := false
for i = 1; i < len(str); {
if str[i] == '+' || str[i] == '-' {
add = true
}
if str[i] == '(' {
temp = getString(str[i:])
length = len(temp) + len(str[:i])
str = str[:i] + temp
if length != len(result) {
if i > 1 {
i--
}
} else {
i = strings.Index(str, ")") + 1
}
result = str
continue
}
if str[i] == ')' {
break
}
i++
}
result = str
if before {
return result
}
if !add {
if i+1 < len(str) {
str = str[1:]
temp = str
result = str[:i-1] + temp[i:]
} else if i+1 == len(str) {
str = str[1:]
temp = str
result = str[:i-1] + temp[i:]
}
} else {
if i+1 < len(str) {
if str[i+1] != '*' && str[i+1] != '/' {
str = str[1:]
temp = str
result = str[:i-1] + temp[i:]
}
} else if i+1 == len(str) {
str = str[1:]
temp = str
result = str[:i-1] + temp[i:]
}
}
return result
}