binary: initial commit
This commit is contained in:
parent
f478d6feca
commit
da6af4a9b8
3 changed files with 174 additions and 0 deletions
65
cpp/binary/README.md
Normal file
65
cpp/binary/README.md
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
# Binary
|
||||
|
||||
Write a program that will convert a binary number, represented as a string (e.g. '101010'), to its decimal equivalent using first principles
|
||||
|
||||
Implement binary to decimal conversion. Given a binary input
|
||||
string, your program should produce a decimal output. The
|
||||
program should handle invalid inputs.
|
||||
|
||||
## Note
|
||||
- Implement the conversion yourself.
|
||||
Do not use something else to perform the conversion for you.
|
||||
|
||||
## About Binary (Base-2)
|
||||
Decimal is a base-10 system.
|
||||
|
||||
A number 23 in base 10 notation can be understood
|
||||
as a linear combination of powers of 10:
|
||||
|
||||
- The rightmost digit gets multiplied by 10^0 = 1
|
||||
- The next number gets multiplied by 10^1 = 10
|
||||
- ...
|
||||
- The *n*th number gets multiplied by 10^*(n-1)*.
|
||||
- All these values are summed.
|
||||
|
||||
So: `23 => 2*10^1 + 3*10^0 => 2*10 + 3*1 = 23 base 10`
|
||||
|
||||
Binary is similar, but uses powers of 2 rather than powers of 10.
|
||||
|
||||
So: `101 => 1*2^2 + 0*2^1 + 1*2^0 => 1*4 + 0*2 + 1*1 => 4 + 1 => 5 base 10`.
|
||||
|
||||
## Getting Started
|
||||
|
||||
Make sure you have read the [getting started with C++](http://help.exercism.io/getting-started-with-cpp.html)
|
||||
page on the [exercism help site](http://help.exercism.io/). This covers
|
||||
the basic information on setting up the development environment expected
|
||||
by the exercises.
|
||||
|
||||
## Passing the Tests
|
||||
|
||||
Get the first test compiling, linking and passing by following the [three
|
||||
rules of test-driven development](http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd).
|
||||
Create just enough structure by declaring namespaces, functions, classes,
|
||||
etc., to satisfy any compiler errors and get the test to fail. Then write
|
||||
just enough code to get the test to pass. Once you've done that,
|
||||
uncomment the next test by moving the following line past the next test.
|
||||
|
||||
```C++
|
||||
#if defined(EXERCISM_RUN_ALL_TESTS)
|
||||
```
|
||||
|
||||
This may result in compile errors as new constructs may be invoked that
|
||||
you haven't yet declared or defined. Again, fix the compile errors minimally
|
||||
to get a failing test, then change the code minimally to pass the test,
|
||||
refactor your implementation for readability and expressiveness and then
|
||||
go on to the next test.
|
||||
|
||||
Try to use standard C++11 facilities in preference to writing your own
|
||||
low-level algorithms or facilities by hand. [CppReference](http://en.cppreference.com/)
|
||||
is a wiki reference to the C++ language and standard library. If you
|
||||
are new to C++, but have programmed in C, beware of
|
||||
[C traps and pitfalls](http://www.slideshare.net/LegalizeAdulthood/c-traps-and-pitfalls-for-c-programmers).
|
||||
|
||||
## Source
|
||||
|
||||
All of Computer Science [view source](http://www.wolframalpha.com/input/?i=binary&a=*C.binary-_*MathWorld-)
|
||||
Loading…
Add table
Add a link
Reference in a new issue