a-conjecture-of-mine

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

Commit
0e844981908c1d1db20730edbd6c1aa2a33718c1
Parent
fbd0555b91f701ea72f6027d4d7d7a1c36540ffd
Author
Gark Garcia <37553739+GarkGarcia@users.noreply.github.com>
Date

Further implemented console functionalities.

Diffstat

2 files changed, 84 insertions, 30 deletions

Status File Name N° Changes Insertions Deletions
Added fdsfs 29 29 0
Modified src/main.rs 85 55 30
diff --git a/fdsfs b/fdsfs
@@ -0,0 +1,29 @@
+commit fbd0555b91f701ea72f6027d4d7d7a1c36540ffd (HEAD -> master)
+Author: Gark Garcia <37553739+GarkGarcia@users.noreply.github.com>
+Date:   Sat Nov 3 16:29:00 2018 -0300
+
+    Implemented multi-threading functionality.
+
+commit adfd52d53c6f4a6b1e0a0dd1cbf5b06f0afdf470
+Author: Gark Garcia <37553739+GarkGarcia@users.noreply.github.com>
+Date:   Fri Nov 2 19:19:53 2018 -0300
+
+    Cleaned unecessary type conversions.
+
+commit 40b7cae777a49cbc6d369b6a003c4324a0b36cd0
+Author: Gark Garcia <37553739+GarkGarcia@users.noreply.github.com>
+Date:   Fri Nov 2 18:42:43 2018 -0300
+
+    Optimized the progress annotation functionality.
+
+commit 836609a62dd778474a80512994c36840b5b4ac23
+Author: Gark Garcia <37553739+GarkGarcia@users.noreply.github.com>
+Date:   Fri Nov 2 17:49:25 2018 -0300
+
+    Implemented console functionalities.
+
+commit 2b5b6fe1ae108a9c867ae307fe66d61c6fcf8c0a
+Author: Gark Garcia <37553739+GarkGarcia@users.noreply.github.com>
+Date:   Wed Oct 31 15:09:47 2018 -0300
+
+    Initial commit.
diff --git a/src/main.rs b/src/main.rs
@@ -5,62 +5,71 @@
 
 extern crate crossterm;
 
-use std::io::stdin;
+use std::io::{stdin, stdout, prelude::*};
 use std::sync::mpsc::{Sender, Receiver};
 use std::sync::mpsc;
 use std::thread;
 use std::ops::Range;
+use std::env;
+use std::process::Command;
 use crossterm::terminal::{terminal,ClearType};
 use crossterm::Screen;
 
 fn main() {
+    let args: Vec<String> = env::args().collect();
     let screen = Screen::default();
     let prompt = terminal(&screen);
 
-    let mut user_input = String::new();
+    // Assign the correct number of threads to run the application with
+    // The default is 10
+    let n_threads = if args.len() == 0 { 10 } else {
+         match args[0].trim().parse::<i32>() {
+             Ok(n) => n,
+             Err(_) => 10
+         }
+    };
 
     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?");
+For all A and B in N, S(A + B) = S(A) + S(B) - 9k, where k is an interger.");
 
     // 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() {
-        println!("\nLOADING...");
-
-        let max = input_parsing_result.unwrap().abs();
-        let counterexpls = get_all_countrexpls(max);
-
-        // Print the results
-        prompt.clear(ClearType::CurrentLine);
-        if counterexpls.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 counterexpls {
-                println!("{} and {}", pair[0], pair[1]);
+    let user_input = ask("\nWhat value would you like to test the conjecture for? ".to_owned());
+
+    match user_input.trim().parse::<i32>() {
+        Ok(max) => {
+            println!("\nLOADING...");
+            let counterexpls = get_all_countrexpls(max, n_threads);
+
+            // Print the results
+            prompt.clear(ClearType::All);
+            println!("LOADED... 100%\n");
+            if counterexpls.len() == 0 {
+                println!("The conjecture is proved for all natural numbers smaller or equals to {}!\n", max);
+            } else {
+                println!("The conjecture is disproved! Here are the counter examples:");
+
+                for pair in counterexpls {
+                    println!("{} and {}", pair[0], pair[1]);
+                }
+
+                println!("");
             }
-        }
-    } else {
-        println!("'{}' is not an interger!", user_input.trim());
+
+            pause_prompt();
+        }, 
+        Err(_) => println!("'{}' is not an interger!", user_input.trim())
     }
 }
 
-fn get_all_countrexpls(max: i32) -> Vec<[i32; 2]> {
+fn get_all_countrexpls(max: i32, n_threads: i32) -> Vec<[i32; 2]> {
 
     if max > 1000 {
-        
+
         // Thread related variables
         let (coutexpl_sender, coutexpl_reciever): (Sender<Vec<[i32; 2]>>, Receiver<Vec<[i32; 2]>>) = mpsc::channel();
         let mut child_threads = Vec::new();
-        let n_threads = 10;
         let range_lenght = ((max as f32) / n_threads as f32).ceil() as i32;
 
         // Conjecture related variables
@@ -129,3 +138,18 @@ fn sum_digits(n: i32) -> i32 {
 
     return sum;
 }
+
+fn ask(message: String) -> String {
+    // Print the question
+    write!(stdout(), "{}", message).unwrap();
+    stdout().flush().unwrap();
+
+    // Return the responce
+    let mut response = String::new();
+    stdin().read_line(&mut response).unwrap();
+    return response;
+}
+
+fn pause_prompt() {
+    let _ = Command::new("cmd.exe").arg("/c").arg("pause").status();
+}+
\ No newline at end of file