浏览代码

Merge branch 'hw2' of github.com:themultiplexer/bsys-ws17-grp11 into hw2

Lorenz Bung 8 年前
父节点
当前提交
ec27e8c3fc
共有 5 个文件被更改,包括 161 次插入3 次删除
  1. 10
    0
      hw2/task2/Cargo.toml
  2. 86
    0
      hw2/task2/src/lib.rs
  3. 28
    0
      hw2/task2/src/main.rs
  4. 30
    3
      hw2/task3/src/lib.rs
  5. 7
    0
      hw2/task3/src/main.rs

+ 10
- 0
hw2/task2/Cargo.toml 查看文件

@@ -0,0 +1,10 @@
1
+[package]
2
+name = "task2"
3
+version = "0.1.0"
4
+authors = ["Joshua Rutschmann <joshua.rutschmann@gmx.de>"]
5
+
6
+[dependencies]
7
+
8
+[[bin]]
9
+doc = false
10
+name = "task2"

+ 86
- 0
hw2/task2/src/lib.rs 查看文件

@@ -0,0 +1,86 @@
1
+/// a struct to hold all of our configuration
2
+#[derive(Debug, PartialEq)]
3
+pub struct Config {
4
+    pub search: char,
5
+    pub line: String,
6
+}
7
+
8
+pub fn run(conf: &Config) -> i32 {
9
+    let mut count = 0;
10
+    for c in conf.line.chars() {
11
+        if c == conf.search {
12
+            count = count + 1;
13
+        }
14
+    }
15
+    count
16
+}
17
+
18
+/// Parses relevant arguments, returning the filled Config in Result
19
+///
20
+///
21
+/// This function will parse the relevant arguments from the
22
+/// given <Strings>.
23
+/// Returns Config or Error Message in Result
24
+#[allow(dead_code)]
25
+fn parse_arguments(args: &Vec<String>) -> Result<Config, String> {
26
+
27
+    if args.len() < 3 {
28
+        return Err("not ennugh parameters".to_string());
29
+    }
30
+
31
+    match args[1].chars().nth(0) {
32
+        Some(value) => {
33
+            Ok(Config {
34
+                search: value,
35
+                line: args[2].clone(),
36
+            })
37
+        }
38
+        None => Err("char mismatch".to_string()),
39
+    }
40
+}
41
+
42
+/// Parses relevant arguments, returning the filled Config
43
+///
44
+///
45
+/// This function will parse the relevant arguments from the
46
+/// given <Strings>.
47
+/// Returns Config
48
+#[allow(dead_code)]
49
+fn parse_arguments_simple(args: &Vec<String>) -> Config {
50
+    Config {
51
+        search: args[1].chars().nth(0).unwrap(),
52
+        line: args[2].clone(),
53
+    }
54
+}
55
+
56
+/// Prints elements of Vec
57
+///
58
+///
59
+/// This function will print all elements of Vec with "args found: <elem>" in
60
+/// each line
61
+///
62
+/// Returns nothing
63
+#[allow(dead_code)]
64
+fn print_arguments(args: &Vec<String>) {
65
+    for s in args {
66
+        println!("args found: {}", s);
67
+    }
68
+}
69
+
70
+impl Config {
71
+    pub fn new(args: &Vec<String>) -> Result<Config, &str> {
72
+        if args.len() < 3 {
73
+            return Err("not enough arguments");
74
+        }
75
+
76
+        match args[1].chars().nth(0) {
77
+            Some(value) => {
78
+                Ok(Config {
79
+                    search: value,
80
+                    line: args[2].clone(),
81
+                })
82
+            }
83
+            None => Err("char mismatch"),
84
+        }
85
+    }
86
+}

+ 28
- 0
hw2/task2/src/main.rs 查看文件

@@ -0,0 +1,28 @@
1
+use std::env;
2
+use std::process;
3
+use task2::Config;
4
+extern crate task2;
5
+
6
+fn main() {
7
+    let args = env::args().collect();
8
+
9
+    let res = Config::new(&args);
10
+    match res {
11
+        Ok(conf) => {
12
+            println!(
13
+                "You asked me to count all '{}' in '{}'",
14
+                conf.search,
15
+                conf.line
16
+            );
17
+            let occ = task2::run(&conf);
18
+            println!("Found {} '{}' in '{}'", occ, conf.search, conf.line);
19
+        }
20
+        Err(message) => {
21
+            println!("{}", message);
22
+            process::exit(1)
23
+        }
24
+    }
25
+
26
+
27
+
28
+}

+ 30
- 3
hw2/task3/src/lib.rs 查看文件

@@ -1,3 +1,4 @@
1
+
1 2
 #[cfg(test)]
2 3
 mod tests {
3 4
     #[test]
@@ -5,14 +6,40 @@ mod tests {
5 6
         assert_eq!(2 + 2, 4);
6 7
     }
7 8
 }
9
+
8 10
 pub struct PascalsTriangle {
9 11
     height: u32,
10 12
 }
11 13
 
12 14
 impl PascalsTriangle {
13
-    pub fn new(i:u32) {
14
-        height = i;
15
+    pub fn new(i: u32) -> Self {
16
+        PascalsTriangle { height: i }
15 17
     }
16
-    pub fn rows() -> Vec<Vec<u32>> {
18
+    pub fn rows(&self) -> Vec<Vec<u32>> {
19
+        let rows = self.height as usize;
20
+        let mut matrix = Vec::with_capacity(rows);
21
+
22
+        for line in 0..rows {
23
+            let current = line as usize;
24
+            matrix.push(Vec::with_capacity(current + 1));
25
+            matrix[current].push(1);
26
+
27
+            if current > 1 {
28
+                let previous = current - 1;
29
+                for index in 1..current {
30
+                    let add = matrix[previous][index - 1] + matrix[previous][index];
31
+                    matrix[current].push(add);
32
+                }
33
+            }
34
+
35
+            if current > 0 {
36
+                matrix[current].push(1);
37
+            }
38
+
39
+        }
40
+
41
+        println!("{:?}", matrix);
42
+
43
+        matrix
17 44
     }
18 45
 }

+ 7
- 0
hw2/task3/src/main.rs 查看文件

@@ -0,0 +1,7 @@
1
+extern crate task3;
2
+use task3::PascalsTriangle;
3
+
4
+fn main() {
5
+    let p = PascalsTriangle::new(10);
6
+    p.rows();
7
+}

正在加载...
取消
保存