Last active
August 25, 2020 14:59
-
-
Save taretmch/e5eec334e6b713fc04936521da88a1d8 to your computer and use it in GitHub Desktop.
全加算器と半加算器を使った加算器の実装
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Adder of two numbers | |
*/ | |
object Adder { | |
val BINARY_REGEX = "(^[01]+$)".r | |
val DECIMAL_REGEX = "(^[0-9]+$)".r | |
/** | |
* Arguments must be decimal numbers or binary numbers | |
* *************************************************** | |
* Usage: | |
* `runMain calculator.Adder 3 5` | |
* `runMain calculator.Adder 11 101` | |
* *************************************************** | |
*/ | |
def main(args: Array[String]): Unit = | |
args.length == 2 match { | |
case true => { | |
val num1 = args(0) | |
val num2 = args(1) | |
println(s"$num1 + $num2 = ???") | |
(num1, num2) match { | |
case (BINARY_REGEX(b1), BINARY_REGEX(b2)) => { | |
val result = addBinary(b1, b2) | |
println(s"result: $result") | |
} | |
case (DECIMAL_REGEX(d1), DECIMAL_REGEX(d2)) => { | |
val result = add(d1.toInt, d2.toInt) | |
println(s"result: $result") | |
} | |
case _ => throw new IllegalArgumentException | |
} | |
} | |
case false => | |
throw new IllegalArgumentException | |
} | |
/** | |
* Calculate sum of two decimal numbers | |
*/ | |
def add(num1: Int, num2: Int): Int = | |
Integer.parseInt( | |
addBinary(num1.toBinaryString, num2.toBinaryString), 2 | |
) | |
/** | |
* Calculate sum of two binary numbers | |
*/ | |
def addBinary(bin1: String, bin2: String): String = { | |
println(s"binary addition: ${bin1} + ${bin2} = ???") | |
val length = bin1.length.max(bin2.length) | |
val revBin1 = bin1.reverse | |
val revBin2 = bin2.reverse | |
(0 to length).foldLeft(("", 0)) { | |
(acc, index) => { | |
val bit1 = scala.util.Try(revBin1(index).toInt - '0').getOrElse(0) | |
val bit2 = scala.util.Try(revBin2(index).toInt - '0').getOrElse(0) | |
val bitSum = FullAdderBinary.sum (bit1, bit2, acc._2) | |
val bitCarry = FullAdderBinary.carry(bit1, bit2, acc._2) | |
println(s"bit: $index) input: num1: $bit1, num2: $bit2, carry: ${acc._2} | output: sum: $bitSum, carry: $bitCarry") | |
(bitSum.toString + acc._1, bitCarry) | |
} | |
}._1 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Binary full adder | |
*/ | |
object FullAdderBinary { | |
/** | |
* Calculate sum | |
*/ | |
def sum(num1: Int, num2: Int, carry: Int): Int = | |
HalfAdderBinary.sum( | |
HalfAdderBinary.sum(num1, num2), | |
carry | |
) | |
/** | |
* Calculate carry | |
*/ | |
def carry(num1: Int, num2: Int, carry: Int): Int = | |
HalfAdderBinary.carry(num1, num2) | | |
HalfAdderBinary.sum(num1, num2) & carry | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Binary half adder | |
*/ | |
object HalfAdderBinary { | |
/** | |
* Calculate sum | |
*/ | |
def sum(num1: Int, num2: Int): Int = | |
num1 ^ num2 | |
/** | |
* Calculate carry | |
*/ | |
def carry(num1: Int, num2: Int): Int = | |
num1 & num2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment