-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbalance_checking.rs
More file actions
51 lines (49 loc) · 1.72 KB
/
balance_checking.rs
File metadata and controls
51 lines (49 loc) · 1.72 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
use regex::Regex;
fn balance(s: &str) -> String {
let re = Regex::new(r"[^\d\w\. ]").unwrap();
let a = s.split("\n").map(|e| e.to_string()).collect::<Vec<_>>();
let mut b = 0.0;
let mut x = 0.0;
let mut v = vec![];
for e in a {
let t = re.replace_all(&e,"").into_owned().trim().to_string();
if t.is_empty() {continue}
if v.len()==0 {
b = t.parse::<f64>().unwrap();
v.push(format!("Original Balance: {:.2}",b));
}
else {
let c = t.split(" ").last().unwrap().parse::<f64>().unwrap();
let d = t.split(" ").collect::<Vec<_>>();
x += c;
v.push(format!("{} {} {:.2} Balance {:.2}",d[0],d[1],c,b-x));
}use regex::Regex;
fn balance(s: &str) -> String {
let re = Regex::new(r"[^\d\w\. ]").unwrap();
let a = s.split("\n").map(|e| e.to_string()).collect::<Vec<_>>();
let mut b = 0.0;
let mut x = 0.0;
let mut v = vec![];
for e in a {
let t = re.replace_all(&e,"").into_owned().trim().to_string();
if t.is_empty() {continue}
if v.len()==0 {
b = t.parse::<f64>().unwrap();
v.push(format!("Original Balance: {:.2}",b));
}
else {
let c = t.split(" ").last().unwrap().parse::<f64>().unwrap();
let d = t.split(" ").collect::<Vec<_>>();
x += c;
v.push(format!("{} {} {:.2} Balance {:.2}",d[0],d[1],c,b-x));
}
}
v.push(format!("Total expense {:.2}",x));
v.push(format!("Average expense {:.2}",x/(v.len() as f64-2.0)));
v.join("\n")
}
}
v.push(format!("Total expense {:.2}",x));
v.push(format!("Average expense {:.2}",x/(v.len() as f64-2.0)));
v.join("\n")
}