-
Notifications
You must be signed in to change notification settings - Fork 1
/
2.rs
140 lines (110 loc) · 3.12 KB
/
2.rs
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
use std::io;
const EOF: char = '#'; // Simulating C EOF char
const SIZE : usize = 100; // Max size for array of numbers
const BUFSIZE : usize = 100; // Max size for ungetch buffer
static mut BUFFER : Vec<char> = Vec::new(); // Buffer to control characters
static mut INPUT : String = String::new(); // Holds characters from stdin (getchar simulation)
fn main()
{
let mut array: Vec<f64> = Vec::with_capacity(SIZE);
let mut n = 0;
unsafe {
while n < SIZE {
// Altough the capacity is set, the length is 0
array.push(0.0);
if getfloat(&mut array[n]) == 0 {
break;
}
println!("Read: {}", array[n]);
n += 1;
}
}
}
/// It is unsafe because we manipulate a static mut variable inside it.
unsafe fn getfloat(pn: &mut f64) -> i32
{
let mut c: char = ' ';
// skip white spaces
while c.is_whitespace() {
c = getch();
}
// skip invalid characters before signal character
while
!c.is_ascii_digit()
&& c != EOF
&& c != '+'
&& c != '-'
{
c = getch();
}
let sign = if c == '-' { -1.0 } else { 1.0 };
if c == '+' || c == '-' {
c = getch();
}
// skip invalid characters after signal character
while
!c.is_ascii_digit()
&& c != EOF
&& c != '+'
&& c != '-'
{
c = getch();
}
*pn = 0.0;
while c.is_ascii_digit() {
*pn = 10.0 * *pn + (c.to_digit(10).unwrap_or(0) as f64 - '0'.to_digit(10).unwrap_or(0) as f64);
c = getch();
}
// read decimal part
if c == '.' {
c = getch();
let mut power: f64 = 10.0;
while c.is_ascii_digit() {
*pn += (c.to_digit(10).unwrap_or(0) as f64 - '0'.to_digit(10).unwrap_or(0) as f64) / power;
c = getch();
power *= 10.0;
}
}
*pn *= sign;
if c != EOF {
ungetch(c);
}
c as i32
}
/// get a (possibly pushed-back) character
/// It is unsafe because we manipulate a static mut variable inside it.
unsafe fn getch() -> char
{
return if BUFFER.len() > 0 { BUFFER.pop().unwrap_or(EOF) } else { getchar() };
}
/// push character back on input
/// It is unsafe because we manipulate a static mut variable inside it.
unsafe fn ungetch(c: char)
{
if BUFFER.len() >= BUFSIZE {
print!("ungetch: too many characters\n");
} else {
BUFFER.push(c);
}
}
/// A simulation of C getchar function.
/// It is unsafe because we manipulate a static mut variable inside it.
unsafe fn getchar() -> char
{
let mut c = INPUT.pop();
if c == None {
// clear input so lines don't get appended
INPUT.clear();
match io::stdin().read_line(&mut INPUT) {
Ok (_bytes) => {
if _bytes == 0 { return EOF }
INPUT = INPUT.chars().rev().collect::<String>();
c = INPUT.pop();
},
Err(_) => {
panic!("Unexpected error.");
}
}
}
c.unwrap_or(EOF)
}