-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path391.go
71 lines (65 loc) · 1.24 KB
/
391.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
// UVa 391 - Mark-up
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func solve(lines []string) []string {
bold, italics, fontSize, markUp := false, false, false, true
var newLines []string
var ch byte
var f float64
for _, line := range lines {
var newLine strings.Builder
for r := strings.NewReader(line); ; {
if _, err := fmt.Fscanf(r, "%c", &ch); err != nil {
break
}
if ch != '\\' {
newLine.WriteByte(ch)
continue
}
fmt.Fscanf(r, "%c", &ch)
if markUp {
switch ch {
case 'b':
bold = !bold
case 'i':
italics = !italics
case 's':
if fontSize = !fontSize; fontSize {
fmt.Fscanf(r, "%f", &f)
}
case '*':
markUp = false
default:
newLine.WriteByte(ch)
}
} else {
if ch == '*' {
markUp = true
} else {
newLine.WriteString("\\")
newLine.WriteByte(ch)
}
}
}
newLines = append(newLines, newLine.String())
}
return newLines
}
func main() {
in, _ := os.Open("391.in")
defer in.Close()
out, _ := os.Create("391.out")
defer out.Close()
s := bufio.NewScanner(in)
s.Split(bufio.ScanLines)
var lines []string
for s.Scan() {
lines = append(lines, s.Text())
}
fmt.Fprintln(out, strings.Join(solve(lines), "\n"))
}