Joshua Rutschmann vor 8 Jahren
Ursprung
Commit
587c86b872
5 geänderte Dateien mit 93 neuen und 19 gelöschten Zeilen
  1. 15
    7
      hw5/task1/src/child/mod.rs
  2. 5
    5
      hw5/task1/src/main.rs
  3. 13
    7
      hw5/task1/src/unit_tests.rs
  4. 6
    0
      hw5/task2/Cargo.toml
  5. 54
    0
      hw5/task2/src/main.rs

+ 15
- 7
hw5/task1/src/child/mod.rs Datei anzeigen

@@ -16,20 +16,22 @@ pub fn run_childs(start_pid: i32, arg: &str) -> Result<(), String> {
16 16
             if value > 0 {
17 17
                 fork_children(0, value - 1, start_pid);
18 18
             } else {
19
-                return Err("Number of forks must not be zero.".to_string())
19
+                return Err("Number of forks must not be zero.".to_string());
20 20
             }
21 21
 
22 22
             Ok(())
23
-        },
23
+        }
24 24
         Err(_) => {
25
-            Err("Failed to parse arguments. PIDs must be decimal.".to_string())
26
-        },
25
+            Err(
26
+                "Failed to parse arguments. PIDs must be decimal.".to_string(),
27
+            )
28
+        }
27 29
     }
28 30
 }
29 31
 
30 32
 
31 33
 /// Private function, which forks specified amount of processes (*count*) through recursion
32
-fn fork_children(count: u8, to:u8, start_pid: i32) {
34
+fn fork_children(count: u8, to: u8, start_pid: i32) {
33 35
     let pid = fork();
34 36
     match pid {
35 37
         Ok(Child) => {
@@ -45,11 +47,17 @@ fn fork_children(count: u8, to:u8, start_pid: i32) {
45 47
         Ok(Parent { child }) => {
46 48
             if let Ok(ws) = wait() {
47 49
                 if let WaitStatus::Exited(child_pid, exit_code) = ws {
48
-                    println!("I am {} and my child is {}. After I waited for {}, it sent me status {:?}", getpid(), child, child_pid, exit_code);
50
+                    println!(
51
+                        "I am {} and my child is {}. After I waited for {}, it sent me status {:?}",
52
+                        getpid(),
53
+                        child,
54
+                        child_pid,
55
+                        exit_code
56
+                    );
49 57
                 }
50 58
             }
51 59
         }
52 60
 
53 61
         Err(_) => {}
54 62
     }
55
-}
63
+}

+ 5
- 5
hw5/task1/src/main.rs Datei anzeigen

@@ -11,21 +11,21 @@ mod child;
11 11
 
12 12
 
13 13
 fn main() {
14
-    let arguments:Vec<String> = args().collect();
14
+    let arguments: Vec<String> = args().collect();
15 15
 
16 16
     if arguments.len() == 2 {
17 17
 
18
-        let result = child::run_childs(getpid(), &arguments[1]);
18
+        let result = child::run_childs(i32::from(getpid()), &arguments[1]);
19 19
         match result {
20
-            Ok(_) => {},
20
+            Ok(_) => {}
21 21
             Err(e) => {
22 22
                 println!("{}", e);
23 23
                 process::exit(1)
24
-            },
24
+            }
25 25
         }
26 26
 
27 27
     } else {
28 28
         zombie::run_zombie();
29 29
     }
30 30
 
31
-}
31
+}

+ 13
- 7
hw5/task1/src/unit_tests.rs Datei anzeigen

@@ -5,17 +5,23 @@ mod tests {
5 5
     use child::*;
6 6
 
7 7
     #[test]
8
-    fn test_zero_forks(){
9
-        assert_eq!(run_childs(123,"0"), Err("Number of forks must not be zero.".to_string()))
8
+    fn test_zero_forks() {
9
+        assert_eq!(
10
+            run_childs(123, "0"),
11
+            Err("Number of forks must not be zero.".to_string())
12
+        )
10 13
     }
11 14
 
12 15
     #[test]
13
-    fn test_one_fork(){
14
-        assert_eq!(run_childs(procinfo::pid::stat_self().unwrap().pid,"1"), Ok(()))
16
+    fn test_one_fork() {
17
+        assert_eq!(
18
+            run_childs(procinfo::pid::stat_self().unwrap().pid, "1"),
19
+            Ok(())
20
+        )
15 21
     }
16 22
 
17 23
     #[test]
18
-    fn test_wrong_pid(){
19
-        assert_eq!(run_childs(2,"1"), Ok(()))
24
+    fn test_wrong_pid() {
25
+        assert_eq!(run_childs(2, "1"), Ok(()))
20 26
     }
21
-}
27
+}

+ 6
- 0
hw5/task2/Cargo.toml Datei anzeigen

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

+ 54
- 0
hw5/task2/src/main.rs Datei anzeigen

@@ -0,0 +1,54 @@
1
+use std::fmt;
2
+
3
+struct Roman(String);
4
+
5
+impl fmt::Display for Roman {
6
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7
+        write!(f, "{}", self.0)
8
+    }
9
+}
10
+
11
+impl From<i32> for Roman {
12
+    fn from(arabic_number: i32) -> Self {
13
+        let stages = vec![1000, 500, 100, 50, 10, 7, 6, 5, 4, 3, 2, 1];
14
+        let mut an = arabic_number;
15
+        let mut value = String::new();
16
+        for s in stages {
17
+            while an >= s{
18
+                value.push_str(&Roman::to_roman(s));
19
+                an -= s;
20
+            }
21
+        }
22
+        Roman::new(value)
23
+    }
24
+}
25
+
26
+impl Roman {
27
+    fn to_roman(number:i32) -> String {
28
+        match number {
29
+            1 => "I",
30
+            2 => "II",
31
+            3 => "III",
32
+            4 => "IV",
33
+            5 => "V",
34
+            6 => "VI",
35
+            7 => "VII",
36
+            10 => "X",
37
+            50 => "L",
38
+            100 => "C",
39
+            500 => "D",
40
+            1000 => "M",
41
+            _ => "",
42
+        }.to_string()
43
+    }
44
+
45
+    fn new(string: String) -> Self {
46
+        Roman(string)
47
+    }
48
+}
49
+
50
+
51
+fn main() {
52
+    println!("Hello, world!");
53
+    println!("{} into {}", 2012, Roman::from(2012));
54
+}

Laden…
Abbrechen
Speichern