a-conjecture-of-mine

An exercise on polyglossy: the same problem solved on multiple languages

Commit
836609a62dd778474a80512994c36840b5b4ac23
Parent
2b5b6fe1ae108a9c867ae307fe66d61c6fcf8c0a
Author
Gark Garcia <37553739+GarkGarcia@users.noreply.github.com>
Date

Implemented console functionalities.

Diffstat

3 files changed, 146 insertions, 13 deletions

Status File Name N° Changes Insertions Deletions
Modified Cargo.lock 27 27 0
Modified Cargo.toml 3 3 0
Modified src/main.rs 129 116 13
diff --git a/Cargo.lock b/Cargo.lock
@@ -1,4 +1,31 @@
 [[package]]
 name = "conjecture_tester"
 version = "0.1.0"
+dependencies = [
+ "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
+]
 
+[[package]]
+name = "kernel32-sys"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "winapi"
+version = "0.2.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "winapi-build"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[metadata]
+"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
+"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
+"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"
diff --git a/Cargo.toml b/Cargo.toml
@@ -4,3 +4,5 @@ version = "0.1.0"
 authors = ["Gark Garcia <37553739+GarkGarcia@users.noreply.github.com>"]
 
 [dependencies]
+winapi = "0.2.8"
+kernel32-sys = "0.2.1"+
\ No newline at end of file
diff --git a/src/main.rs b/src/main.rs
@@ -1,26 +1,58 @@
-// The following script is a simple test for the following conjecture:
+// The following program is a simple test for the following conjecture:
 
 // Let S: N -> N be the sum of the digits of a positive integer.
 // For all A and B in N, S(A + B) = S(A) + S(B) - 9k, where k is an interger.
 
-fn main() {
-    let max : i32 = 1000;
-    let counter_examples : Vec<[i32; 2]> = test_conjecture(max);
+extern crate kernel32;
+extern crate winapi;
 
-    // Print the results
-    if counter_examples.len() == 0 {
-        println!("The conjecture holds up to {}!", max);
-    } else {
-        println!("The conjecture doesn't hold! Here are the counter examples:");
+use winapi::HANDLE;
+use winapi::wincon::CONSOLE_SCREEN_BUFFER_INFO;
+use winapi::wincon::COORD;
+use winapi::wincon::SMALL_RECT;
+use winapi::WORD;
+use winapi::DWORD;
+use std::io::stdin;
 
-        for pair in counter_examples {
-            println!("{} and {};", pair[0], pair[1]);
+static mut CONSOLE_HANDLE: Option<HANDLE> = None;
+
+fn main() {
+    let mut user_input = String::new();
+
+    println!("  This program is a simple test for the following conjecture:
+    
+  Let S: N -> N be the sum of the digits of a positive integer.
+  For all A and B in N, S(A + B) = S(A) + S(B) - 9k, where k is an interger.
+    
+  What value would you like to test the conjecture for?");
+
+    // Listen for user input
+    stdin().read_line(&mut user_input).expect("Did not enter a correct string");
+    let input_parsing_result = user_input.trim().parse::<i32>();
+
+    // If the user input is a valid int
+    if !input_parsing_result.is_err() {
+        let max = input_parsing_result.unwrap().abs();
+        let counter_examples = get_counter_expl(max);
+
+        // Print the results
+        clear_console();
+        if counter_examples.len() == 0 {
+            println!("The conjecture is proved for all natural numbers smaller or equals to {}!", max);
+        } else {
+            println!("The conjecture is disproved! Here are the counter examples:");
+
+            for pair in counter_examples {
+                println!("{} and {};", pair[0], pair[1]);
+            }
         }
+    } else {
+        println!("'{}' is not an interger!", user_input.trim());
     }
 }
 
 // Test the conjecture for all values up to max and return the counterexamples
-fn test_conjecture(max : i32) -> Vec<[i32; 2]> {
+fn get_counter_expl(max : i32) -> Vec<[i32; 2]> {
     let mut counter_examples : Vec<[i32; 2]> = Vec::new();
     let mut load_bar = 0f32;
 
@@ -37,7 +69,7 @@ fn test_conjecture(max : i32) -> Vec<[i32; 2]> {
 
             if new_load_bar != load_bar {
                 load_bar = new_load_bar;
-                print!("{}[2J", 27 as char);
+                clear_console();
                 println!("LOADING: {}%", new_load_bar);
             }
         }
@@ -66,3 +98,74 @@ fn sum_digits(n : i32) -> i32 {
 
     return sum;
 }
+
+
+// Console releted code:
+
+fn get_output_handle() -> HANDLE {
+    unsafe {
+        if let Some(handle) = CONSOLE_HANDLE {
+            return handle;
+        } else {
+            let handle = kernel32::GetStdHandle(winapi::STD_OUTPUT_HANDLE);
+            CONSOLE_HANDLE = Some(handle);
+            return handle;
+        }
+    }
+}
+
+fn get_buffer_info() -> winapi::CONSOLE_SCREEN_BUFFER_INFO {
+    let handle = get_output_handle();
+    if handle == winapi::INVALID_HANDLE_VALUE {
+        panic!("NoConsole")
+    }
+    let mut buffer = CONSOLE_SCREEN_BUFFER_INFO {
+        dwSize: COORD { X: 0, Y: 0 },
+        dwCursorPosition: COORD { X: 0, Y: 0 },
+        wAttributes: 0 as WORD,
+        srWindow: SMALL_RECT {
+            Left: 0,
+            Top: 0,
+            Right: 0,
+            Bottom: 0,
+        },
+        dwMaximumWindowSize: COORD { X: 0, Y: 0 },
+    };
+    unsafe {
+        kernel32::GetConsoleScreenBufferInfo(handle, &mut buffer);
+    }
+    buffer
+}
+
+fn clear_console() {
+    let handle = get_output_handle();
+    if handle == winapi::INVALID_HANDLE_VALUE {
+        panic!("NoConsole")
+    }
+
+    let screen_buffer = get_buffer_info();
+    let console_size: DWORD = screen_buffer.dwSize.X as u32 * screen_buffer.dwSize.Y as u32;
+    let coord_screen = COORD { X: 0, Y: 0 };
+
+    let mut amount_chart_written: DWORD = 0;
+    unsafe {
+        kernel32::FillConsoleOutputCharacterW(
+            handle,
+            32 as winapi::WCHAR,
+            console_size,
+            coord_screen,
+            &mut amount_chart_written,
+        );
+    }
+    set_cursor_possition(0, 0);
+}
+
+fn set_cursor_possition(y: i16, x: i16) {
+    let handle = get_output_handle();
+    if handle == winapi::INVALID_HANDLE_VALUE {
+        panic!("NoConsole")
+    }
+    unsafe {
+        kernel32::SetConsoleCursorPosition(handle, COORD { X: x, Y: y });
+    }
+}