diff --git a/a1_guessing_game/src/main.rs b/a1_guessing_game/src/main.rs index 8ee4544d04f301662a50316f44d28cad826c2042..09545035848ba742c97d78e9f0f946420f4ac5b2 100644 --- a/a1_guessing_game/src/main.rs +++ b/a1_guessing_game/src/main.rs @@ -20,7 +20,7 @@ fn main() { loop{ let mut guess = String::new(); print!("\nYour guess is...: "); - io::stdout().flush(); + io::stdout().flush().unwrap(); //Throws away the result, and grabs the OK if it exists. //returns io::Result, this has .expect() method io::stdin().read_line(&mut guess) diff --git a/a2_guessing_game/src/lib.rs b/a2_guessing_game/src/lib.rs new file mode 100644 index 0000000000000000000000000000000000000000..d8a1cc53659d690823fac7cb5a2bd3507cdb2b3a --- /dev/null +++ b/a2_guessing_game/src/lib.rs @@ -0,0 +1 @@ +pub mod results; diff --git a/a2_guessing_game/src/main.rs b/a2_guessing_game/src/main.rs index ba4a668ca900d79264a14cd81f8a3445f32a32fa..509044e7378cfd12ea575c4b61b0f65083da15b2 100644 --- a/a2_guessing_game/src/main.rs +++ b/a2_guessing_game/src/main.rs @@ -1,21 +1,23 @@ extern crate rand; extern crate colored; +extern crate guessing_game; use std::io; //standard in-out use std::cmp::Ordering; -use std::cmp::max; use std::collections::HashMap; +use std::io::Write; //seemingly needed to use io::stdout().flush() use rand::Rng; -use std::io::Write; //seemingly needed to use io::stdout().flush() use colored::*; + fn get_input() -> Result<u32, String> { //returns io::Result, this has .expect() method let mut guess = String::new(); print!("\nYour guess is...: "); io::stdout().flush().unwrap(); //Throws away the result, and grabs the OK if it exists. io::stdin().read_line(&mut guess); + //Do I use something here or does that mess up the match below? //parse makes the string into the number type (in this case u32) //parse() can crash too with strange input. So .expect is here too! @@ -25,50 +27,6 @@ fn get_input() -> Result<u32, String> { } } -fn print_guesses(v: &Vec<(u32, String)>) { - let iterations = 2; - for i in 0..iterations { - println!("\nResults: Printing All\nAttempt | Guess"); - for g in v { - println!(" {} | {}", g.0, g.1); - } - } -} - -fn print_the_last_three_guesses_in_backwards_order_really_nicely(v: &Vec<(u32, String)>) { - let mut count = 0; - println!("\nResults: Printing Last Three\nAttempt | Guess"); - for g in v.iter().rev() { - println!(" {} | {}", g.0, g.1); - count += 1; - if count == 3 { break; } - } -} - -fn print_hashmap(hm: &HashMap<u32, String>) { - println!("\nResults: Printing HashMap\nAttempt | Guess"); - for m in hm{ - println!(" {:?} | {:?}", m.0, m.1); - } -} - -fn print_hashmap_last_three(hm: &HashMap<u32, String>) { - println!("\nResults: Printing Last Three in Hashmap\nAttempt | Guess"); - let mut entries = 0; - for m in hm.iter() { - entries += 1; - } - //let ln: u32 = hm.len(); //Give error because hm.len() is usize, not u32 - - for i in (max(entries-2, 1)..entries+1).rev() { - let mut item = hm.get(&i); - match item { - Some(num) => println!(" {} | {}", i, num), - None => println!("Nothing!"), - }; - } -} - fn main() { println!("{}", "\n\nGuess the number or perish. :)".bold()); @@ -77,22 +35,15 @@ fn main() { println!("The secret number is: {}, but don't tell a soul.", secret_number); - //Vector to hold all guesses, first to last. - let mut guesses: Vec<(u32, String)>= Vec::new(); - let mut guess_pair: (u32, String); - - //Hashmap - let mut guess_hashmap = HashMap::new(); - - let mut guess: u32; - let mut attempts = 0; + let mut guesses: Vec<(u32, String)>= Vec::new(); //Vector to hold all guesses, first to last. + let mut guess_pair: (u32, String); //Tuple + let mut guess_hashmap = HashMap::new(); //Hashmap + let mut guess: u32; //Guess + let mut attempts = 0; //Attempts loop{ match get_input() { - Ok(num) => { - println!("Number {}!", num); - guess = num; - } + Ok(num) => guess = num, Err(error) => { println!("Error {}!", error); continue; @@ -101,12 +52,10 @@ fn main() { attempts += 1; - //Vector - guess_pair = (attempts, guess.to_string()); + guess_pair = (attempts, guess.to_string()); //Vector guesses.push(guess_pair); - //Hashmap - guess_hashmap.insert(attempts, guess.to_string()); + guess_hashmap.insert(attempts, guess.to_string()); //Hashmap println!("\nYou guessed: {}!", guess); print!("That's "); @@ -128,9 +77,14 @@ fn main() { println!("\nYou've guessed {} time(s) already!", attempts.to_string().yellow()) } - print_guesses(&guesses); - print_the_last_three_guesses_in_backwards_order_really_nicely(&guesses); - print_hashmap(&guess_hashmap); - print_hashmap_last_three(&guess_hashmap); + //print_guesses(&guesses); + //print_the_last_three_guesses_in_backwards_order_really_nicely(&guesses); + //print_hashmap(&guess_hashmap); + //print_hashmap_last_three(&guess_hashmap); + guessing_game::results::print_guesses(&guesses); + guessing_game::results::print_the_last_three_guesses_in_backwards_order_really_nicely(&guesses); + guessing_game::results::print_hashmap(&guess_hashmap); + guessing_game::results::print_hashmap_last_three(&guess_hashmap); } + diff --git a/a2_guessing_game/src/results.rs b/a2_guessing_game/src/results.rs new file mode 100644 index 0000000000000000000000000000000000000000..0cbf61bfbaa5ae88ba304e177a993a6d4ca25741 --- /dev/null +++ b/a2_guessing_game/src/results.rs @@ -0,0 +1,48 @@ + +use std::cmp::max; +use std::collections::HashMap; + +pub fn print_guesses(v: &Vec<(u32, String)>) { + let iterations = 2; + for _i in 0..iterations { + println!("\nResults: Printing All\nAttempt | Guess"); + for g in v { + println!(" {} | {}", g.0, g.1); + } + } +} + +pub fn print_the_last_three_guesses_in_backwards_order_really_nicely(v: &Vec<(u32, String)>) { + let mut count = 0; + println!("\nResults: Printing Last Three\nAttempt | Guess"); + for g in v.iter().rev() { + println!(" {} | {}", g.0, g.1); + count += 1; + if count == 3 { break; } + } +} + +pub fn print_hashmap(hm: &HashMap<u32, String>) { + println!("\nResults: Printing HashMap\nAttempt | Guess"); + for m in hm{ + println!(" {:?} | {:?}", m.0, m.1); + } +} + +pub fn print_hashmap_last_three(hm: &HashMap<u32, String>) { + println!("\nResults: Printing Last Three in Hashmap\nAttempt | Guess"); + let mut entries = 0; + for _m in hm.iter() { + entries += 1; + } + //let ln: u32 = hm.len(); //Give error because hm.len() is usize, not u32 + + for i in (max(entries-2, 1)..entries+1).rev() { + let mut item = hm.get(&i); + match item { + Some(num) => println!(" {} | {}", i, num), + None => println!("Nothing!"), + }; + } +} + diff --git a/a3_decrypter/grupp_01.lab1_underlag.s b/a3_decrypter/grupp_01.lab1_underlag.s new file mode 100644 index 0000000000000000000000000000000000000000..65665ee7e687a0ec10b0c7a6b5258a054ee368c6 --- /dev/null +++ b/a3_decrypter/grupp_01.lab1_underlag.s @@ -0,0 +1,246 @@ +# ---------------------------------------------------------- +# Group 1's "underlag" for Lab 1 +# Pseudo-instructions may be used for Lab 1. +# ---------------------------------------------------------- + + + +# Group 1's Codeword Generator Subroutine (pseudocode) +# (remember: "seed" is a global variable, UNSIGNED INTEGER; +# you may implement local variables in registers or on the stack; +# result returned in v0; preserve all except t regs) +# +# FUNCTION codgen(): UNSIGNED INTEGER; +# LOCAL SIGNED INTEGER n; +# LOCAL UNSIGNED INTEGER x, y; +# BEGIN +# n := [count the number of 0's in word "seed"]; +# x := [rotate "seed" left by 30 bits]; +# y := [shift "seed" right-ARITHMETIC by 6 bits]; +# seed := x XOR y XOR n; +# RETURN( seed XOR 0x464b713e ); +# END; +# +# hint: if "seed" is initialized to 0x3e944b9f, +# the first five calls will generate these values: +# 0x891432f9, 0x4aa1dccc, 0xc54270fa, 0x9885155f, 0xce83d1b8, ... +# your code is to be written farther down (see comment below). + + +# Group 1's Recursive Decoding Subroutine (pseudocode) +# (for "decode", all four local variables must be implemented ON THE +# STACK, and NOT in registers; implement the code literally,. +# no optimizations. We're trying to teach you something. +# remember: result returned in v0; preserve all except t regs) +# +# FUNCTION decode( wordarr, bytearr ): UNSIGNED INTEGER; +# (wordarr, bytearr passed by reference) +# LOCAL UNSIGNED INTEGER m, r, x, y; +# BEGIN +# x := ONE'S-COMPLEMENT of codgen(); +# IF ([contents of word at "wordarr"] = 0) THEN +# [byte pointed to by "bytearr"] := 0; +# r := x; +# ELSE +# y := decode( wordarr+, bytearr+ ); # "k+" means "successor in k" +# m := ( x - y ) - [contents of word at "wordarr"]; +# [byte pointed to by "bytearr"] := [the eight bits at "m"<20:13>]; +# r := TWO'S-COMPLEMENT OF codgen(); +# r := x + y + m + r + 5; +# ENDIF; +# RETURN( r ); +# END; + + +# ---------------------------------------------------------- +# The following are the ONLY lines that may appear in the +# ".data" section of the code. You may add NO other lines. +# NO additional global variables. +# ---------------------------------------------------------- + + + .data +plain: .space 132 # room for 132 characters + + .align 4 +seed: .word 0 # 32-bit UNSIGNED INTEGER. + +abc: .word 0x9fdd9158 # string "abc", encoded + .word 0x85715808 + .word 0xac73323a + .word 0 +coded: .word 0x015e7a47 # the real encoded data + .word 0x2ef84ebb + .word 0x177a8db4 + .word 0x1b722ff9 + .word 0x5dc7cff0 + .word 0x5dc9dea6 + .word 0x1da0c15a + .word 0xe4c236a2 + .word 0x3d16b0d0 + .word 0x1f397842 + .word 0xaae0d2ba + .word 0x11246674 + .word 0x0845317f + .word 0xd5512dad + .word 0xb6184977 + .word 0xd293a53e + .word 0x7d9c2716 + .word 0xd917eae6 + .word 0xd8852384 + .word 0x286e46f9 + .word 0xce566029 + .word 0xcefe7daf + .word 0x62d726d4 + .word 0x0dbaeb2d + .word 0x95f57c60 + .word 0xed515141 + .word 0x29b77d0f + .word 0x9f7b8d0c + .word 0x45a8395a + .word 0xfead2b72 + .word 0x883d434c + .word 0xed8ddf60 + .word 0xe51e65e4 + .word 0x19bf6bb1 + .word 0xfeb505ec + .word 0x662aa23c + .word 0xf6827cf8 + .word 0xd1dc7a5c + .word 0x4fa5b066 + .word 0x7ddd25a4 + .word 0xa8ba8e8a + .word 0x72846227 + .word 0xf8f636fb + .word 0x2b389a9c + .word 0xe4038bf6 + .word 0x6e169877 + .word 0xad028132 + .word 0x84dbfe8c + .word 0x243762ff + .word 0x59c8f80c + .word 0xb6e0db4b + .word 0xedb8cab7 + .word 0xcd4b39f6 + .word 0xaf263741 + .word 0x18d9965f + .word 0x1ab1f037 + .word 0x5b458792 + .word 0xc94d960d + .word 0xd45cedea + .word 0x2160aca3 + .word 0x93c77766 + .word 0x2d66e105 + .word 0x9ff74d4f + .word 0x6dc22f21 + .word 0x6b03d689 + .word 0x5fc48de0 + .word 0x1138f000 + .word 0xccb58e57 + .word 0xf9c8e200 + .word 0x7ab26e3c + .word 0xc61dcb3e + .word 0x6aefccb0 + .word 0x7a452f05 + .word 0xa5cf0731 + .word 0xa249383f + .word 0x628fe534 + .word 0xcad81710 + .word 0x7f616276 + .word 0x3ce18308 + .word 0xed4857ff + .word 0xd1e5b1d1 + .word 0xc2e84dc2 + .word 0xaa003742 + .word 0xaf637488 + .word 0x831afc48 + .word 0x287a69a0 + .word 0x6e04546e + .word 0x13dffa07 + .word 0x3232fb10 + .word 0xd69e2e09 + .word 0x355d8dc7 + .word 0xef902301 + .word 0x9a89ac15 + .word 0x967dc900 + .word 0x08dc2b1c + .word 0x6b5be690 + .word 0x894b0e02 + .word 0xe26af9af + .word 0xa6fd3b23 + .word 0xfcf213e5 + .word 0x85217608 + .word 0x7fd3be8b + .word 0xa2e757fb + .word 0x3717a341 + .word 0x85ee426d + .word 0x394bb856 + .word 0x12ac98c3 + .word 0xec7d4ab5 + .word 0x721b6989 + .word 0x30e36360 + .word 0xaa018403 + .word 0x9ee61196 + .word 0xa8697adc + .word 0x51e9d65a + .word 0x11023594 + .word 0xc4c4b36b + .word 0xda80bf7a + .word 0xbd5a645e + .word 0x18cea918 + .word 0xa723dda8 + .word 0x0126c05e + .word 0x2962d48a + .word 0xd5f7d312 + .word 0xb8947041 + .word 0x7c1e2e9a + .word 0x945eeac3 + .word 0x7110fb1c + .word 0xa7bc72cc + .word 0xdf47dfbb + .word 0x09a1c6c8 + .word 0xc2e41061 + .word 0 + + +# ---------------------------------------------------------- +# The following is the main program. You may not change this. +# You may only add your subroutines AFTER the "infinite end loop" instruction here. +# You MUST have two subroutines named "codgen" and "decode". +# BOTH must adhere to our calling conventions; +# both MUST preserve all registers except v-regs and t-regs; +# we are going to TEST for this when we run your code. +# ---------------------------------------------------------- + + + .text + .set noreorder +main: li $s0, 0x0e0657c1 # initialize "seed" + la $s1, seed # initialize "seed" + sw $s0, 0($s1) + la $a0, coded # address of start of coded words + la $a1, plain # address of start of decoded bytes + bal decode # outer call to recursive "decode" +end: + b end # infinite loop; plaintext now in "plain". + + +# ---------------------------------------------------------- +# Group 1's assembly code for Function CodGen : +# ---------------------------------------------------------- + + # your activation record diagram here. + +codgen: # your code here. + + +# ---------------------------------------------------------- +# Group 1's assembly code for Function DeCode : +# ---------------------------------------------------------- + + # your activation record diagram here. + +decode: # your code here. + + +# end of file.