From 9f6c55ebcab2e1d3073e8bb6c8c910d0116efee4 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 8 Jul 2015 18:06:18 +0200 Subject: [PATCH] make it so that I can actually run stuff that relies on the solution of exercises --- Makefile | 7 ++++--- src/main.rs | 2 +- src/part08.rs | 11 ++++++++--- src/part10.rs | 21 +++++++-------------- workspace/src/part08.rs | 9 +++++++-- workspace/src/part10.rs | 21 +++++++-------------- 6 files changed, 34 insertions(+), 37 deletions(-) diff --git a/Makefile b/Makefile index f59d575..9b2a818 100644 --- a/Makefile +++ b/Makefile @@ -8,11 +8,12 @@ all: docs workspace crates ## Documentation docs: $(DOCFILES) -.tmp/docs/%.rs: src/%.rs Makefile +.tmp/docs/%.rs: src/%.rs Makefile dup-unimpl.sed @mkdir -p .tmp/docs @echo "$< -> $@" - @# sed-fu: remove the "@" from "//@", and remove trailing "/*@*/". - @sed 's|^\(\s*//\)@|\1|;s|\s*/\*@\*/$$||' $< > $@ + @# sed-fu: remove the "@" from "//@", and remove trailing "/*@*/", replace lines ending in "/*@@*/" by "unimplemented!()". + @# Also coalesce multiple adjacent such lines to one. + @sed 's|^\(\s*//\)@|\1|;s|\s*/\*@\*/$$||;s|\(\s*\)\S.*/\*@@\*/|\1unimplemented!()|' $< | sed -f dup-unimpl.sed > $@ docs/%.html: .tmp/docs/%.rs @./pycco-rs $< diff --git a/src/main.rs b/src/main.rs index 40bb125..d7945a5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -95,7 +95,7 @@ mod part10; // function. fn main() { - part10::main(); + part00::main(); } // Additional material diff --git a/src/part08.rs b/src/part08.rs index feb2abc..2867a58 100644 --- a/src/part08.rs +++ b/src/part08.rs @@ -26,11 +26,13 @@ fn overflowing_add(a: u64, b: u64, carry: bool) -> (u64, bool) { if sum >= a { // The addition did not overflow.
// **Exercise 08.1**: Write the code to handle adding the carry in this case. - unimplemented!() + let sum_total = u64::wrapping_add(sum, if carry { 1 } else { 0 }); /*@@*/ + let had_overflow = sum_total < sum; /*@@*/ + (sum_total, had_overflow) /*@@*/ } else { // Otherwise, the addition *did* overflow. It is impossible for the addition of the carry // to overflow again, as we are just adding 0 or 1. - (sum + if carry { 1 } else { 0 }, true) /*@*/ + (sum + if carry { 1 } else { 0 }, true) /*@*/ } } @@ -82,7 +84,10 @@ impl ops::Add for BigInt { carry = new_carry; /*@*/ } // **Exercise 08.2**: Handle the final `carry`, and return the sum. - unimplemented!() + if carry { /*@@*/ + result_vec.push(1); /*@@*/ + } /*@@*/ + BigInt { data: result_vec } /*@@*/ } } diff --git a/src/part10.rs b/src/part10.rs index 97dd4c1..53fb0b3 100644 --- a/src/part10.rs +++ b/src/part10.rs @@ -28,19 +28,14 @@ impl Action for PrintWithString { } } -fn read_one_line() -> String { - println!("Please enter a line of text."); - let mut stdin = io::stdin(); - let mut prefix = "".to_string(); - stdin.read_line(&mut prefix).unwrap(); - prefix +fn print_with_prefix_v1(b: &BigInt, prefix: String) { + let my_action = PrintWithString { prefix: prefix }; + b.act_v1(my_action); } -pub fn main_v1() { - let prefix = read_one_line(); - let my_action = PrintWithString { prefix: prefix }; +pub fn main() { let bignum = BigInt::new(1 << 63) + BigInt::new(1 << 16) + BigInt::new(1 << 63); - bignum.act_v1(my_action); + print_with_prefix_v1(&bignum, "Digit: ".to_string()); } impl BigInt { @@ -51,10 +46,8 @@ impl BigInt { } } -pub fn main() { - let prefix = read_one_line(); - let bignum = BigInt::new(1 << 63) + BigInt::new(1 << 16) + BigInt::new(1 << 63); - bignum.act(|digit| println!("{}{}", prefix, digit) ); +pub fn print_with_prefix(b: &BigInt, prefix: String) { + b.act(|digit| println!("{}{}", prefix, digit) ); } //@ [index](main.html) | [previous](part08.html) | [next](main.html) diff --git a/workspace/src/part08.rs b/workspace/src/part08.rs index 61f665f..2d3b5d8 100644 --- a/workspace/src/part08.rs +++ b/workspace/src/part08.rs @@ -13,7 +13,9 @@ fn overflowing_add(a: u64, b: u64, carry: bool) -> (u64, bool) { if sum >= a { // The addition did not overflow.
// **Exercise 08.1**: Write the code to handle adding the carry in this case. - unimplemented!() + let sum_total = u64::wrapping_add(sum, if carry { 1 } else { 0 }); /*@@*/ + let had_overflow = sum_total < sum; /*@@*/ + (sum_total, had_overflow) /*@@*/ } else { // Otherwise, the addition *did* overflow. It is impossible for the addition of the carry // to overflow again, as we are just adding 0 or 1. @@ -52,7 +54,10 @@ impl ops::Add for BigInt { unimplemented!() } // **Exercise 08.2**: Handle the final `carry`, and return the sum. - unimplemented!() + if carry { /*@@*/ + result_vec.push(1); /*@@*/ + } /*@@*/ + BigInt { data: result_vec } /*@@*/ } } diff --git a/workspace/src/part10.rs b/workspace/src/part10.rs index ee226c4..570e851 100644 --- a/workspace/src/part10.rs +++ b/workspace/src/part10.rs @@ -28,19 +28,14 @@ impl Action for PrintWithString { } } -fn read_one_line() -> String { - println!("Please enter a line of text."); - let mut stdin = io::stdin(); - let mut prefix = "".to_string(); - stdin.read_line(&mut prefix).unwrap(); - prefix +fn print_with_prefix_v1(b: &BigInt, prefix: String) { + let my_action = PrintWithString { prefix: prefix }; + b.act_v1(my_action); } -pub fn main_v1() { - let prefix = read_one_line(); - let my_action = PrintWithString { prefix: prefix }; +pub fn main() { let bignum = BigInt::new(1 << 63) + BigInt::new(1 << 16) + BigInt::new(1 << 63); - bignum.act_v1(my_action); + print_with_prefix_v1(&bignum, "Digit: ".to_string()); } impl BigInt { @@ -51,9 +46,7 @@ impl BigInt { } } -pub fn main() { - let prefix = read_one_line(); - let bignum = BigInt::new(1 << 63) + BigInt::new(1 << 16) + BigInt::new(1 << 63); - bignum.act(|digit| println!("{}{}", prefix, digit) ); +pub fn print_with_prefix(b: &BigInt, prefix: String) { + b.act(|digit| println!("{}{}", prefix, digit) ); } -- 2.30.2