themultiplexer 8 gadus atpakaļ
vecāks
revīzija
d3ac29bb0e
3 mainītis faili ar 66 papildinājumiem un 5 dzēšanām
  1. 19
    5
      hw6/task1/src/main.rs
  2. 0
    0
      hw6/task2/src/command.rs
  3. 47
    0
      hw6/task2/src/shell.rs

+ 19
- 5
hw6/task1/src/main.rs Parādīt failu

@@ -70,16 +70,25 @@ fn concatenate_strings<'a>(s1: &'a str, s2: &'a str) -> String {
70 70
 }
71 71
 
72 72
 /// Splits the given String reference and returns it as Vector.
73
-/*fn split_into_strings<'2>(s1:&'a str) -> Vec<&'a str> {
74
-    s1.to_string().split_whitespace().collect()
75
-}*/
73
+fn split_into_strings<'a>(s1: &'a str) -> Vec<String> {
74
+    s1.to_string()
75
+        .split_whitespace()
76
+        .map(|s| s.to_string())
77
+        .collect()
78
+}
79
+
76 80
 /// Calculates the sum of the given Strings and returns them as a Result.
77 81
 fn sum_strings(v: Vec<&str>) -> Result<i32, &str> {
78 82
     let mut sum = 0;
79 83
     for x in v {
80 84
         match x.parse::<i32>() {
81 85
             Ok(val) => {
82
-                sum += val;
86
+                match i32::checked_add(sum, val) {
87
+                    Some(y) => {
88
+                        sum = y;
89
+                    }
90
+                    None => return Err("Overflow would happen in sum_strings()"),
91
+                }
83 92
             }
84 93
             Err(_) => return Err("Given String is not a int"),
85 94
         }
@@ -93,7 +102,12 @@ fn mul_strings(v: Vec<&str>) -> Result<i32, &str> {
93 102
     for x in v {
94 103
         match x.parse::<i32>() {
95 104
             Ok(val) => {
96
-                prod *= val;
105
+                match i32::checked_mul(prod, val) {
106
+                    Some(y) => {
107
+                        prod = y;
108
+                    }
109
+                    None => return Err("Overflow would happen in mul_strings()"),
110
+                }
97 111
             }
98 112
             Err(_) => return Err("Given String is not a int"),
99 113
         }

+ 0
- 0
hw6/task2/src/command.rs Parādīt failu


+ 47
- 0
hw6/task2/src/shell.rs Parādīt failu

@@ -0,0 +1,47 @@
1
+struct Shell<R, W> {
2
+    pub reader: R,
3
+    pub writer: W,
4
+    pub should_exit: bool,
5
+    pub name: String,
6
+}
7
+
8
+impl Shell<R, W> {
9
+    /// Creates a new Shell.
10
+    pub fn new(input: R, output: W, name: String) -> Self {
11
+        Shell {
12
+            reader = input,
13
+            writer = output,
14
+            should_exit = false,
15
+            name = name,
16
+        }
17
+    }
18
+    /// Initializes the Shell Loop
19
+    pub fn start() -> Result<Self, &str> {
20
+    }
21
+    /// Waits for user inputs.
22
+    fn shell_loop() -> Result {
23
+    }
24
+    /// Prints the shell prompt.
25
+    fn prompt() -> Result<Option, &str> {
26
+    }
27
+    /// Runs a command.
28
+    fn run(&str) {
29
+    }
30
+}
31
+
32
+impl BufRead for Shell<R, W> {
33
+    fn fill_buf(&mut self) -> Result<&[u8]> {}
34
+    fn consume(&mut self, amt: usize) {}
35
+}
36
+
37
+impl Write for Shell<R, W> {
38
+    fn write(&mut self, buf: &[u8]) -> Result<usize> {}
39
+    fn flush(&mut self) -> Result<()> {}
40
+}
41
+
42
+
43
+let mut s = Shell::new();
44
+    match s.start() {
45
+        Ok(_) => process::exit(0),
46
+        Err(_) => process::exit(1),
47
+    }

Notiek ielāde…
Atcelt
Saglabāt