This repository was archived by the owner on May 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.rs
More file actions
78 lines (67 loc) · 1.96 KB
/
main.rs
File metadata and controls
78 lines (67 loc) · 1.96 KB
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
pub mod min;
pub mod fa;
use std::io::{stdin, stdout, BufRead, Write};
use std::str::FromStr;
use fa::DFA;
fn main()
{
let dfa = read_dfa();
let min_dfa = dfa.minimize();
print_dfa(&min_dfa);
}
fn read_dfa() -> DFA
{
let console = stdin(); // Can't inline this because "borrowed value does not live long enough"
let mut stdin = console.lock();
let mut buffer = String::new();
// Read Header
let state_count;
let transition_count;
let start_state;
let final_state_count;
stdin.read_line(&mut buffer).unwrap();
{
let header: Vec<&str> = buffer.trim_right().split(' ').collect();
state_count = usize::from_str(header[0]).unwrap();
transition_count = usize::from_str(header[1]).unwrap();
start_state = usize::from_str(header[2]).unwrap();
final_state_count = usize::from_str(header[3]).unwrap();
}
// Create DFA
let mut dfa = DFA::new(state_count, start_state);
// Read Transitions
for _ in 0..transition_count
{
buffer.clear();
stdin.read_line(&mut buffer).unwrap();
let transition: Vec<&str> = buffer.trim_right().split(' ').collect();
let from_state = usize::from_str(transition[0]).unwrap();
let input = i32::from_str(transition[1]).unwrap();
let to_state = usize::from_str(transition[2]).unwrap();
dfa.add_transition(from_state, input, to_state);
}
// Read Final States
for _ in 0..final_state_count
{
buffer.clear();
stdin.read_line(&mut buffer).unwrap();
let final_state = usize::from_str(buffer.trim_right()).unwrap();
dfa.add_final_state(final_state);
}
return dfa;
}
fn print_dfa(dfa: &DFA)
{
let console = stdout();
let mut stdout = console.lock();
// Header
writeln!(&mut stdout, "{0} {1} {2} {3}", dfa.state_count(), dfa.transitions().len(), dfa.start_state(), dfa.final_states().len()).unwrap();
for transition in dfa.transitions()
{
writeln!(&mut stdout, "{0} {1} {2}", transition.from, transition.on_input, transition.to).unwrap();
}
for state in dfa.final_states()
{
writeln!(&mut stdout, "{}", state).unwrap();
}
}