ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# nicolemon's solution

## to Leap in the C++ Track

Published at Sep 10 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

Given a year, report if it is a leap year.

The tricky thing here is that a leap year in the Gregorian calendar occurs:

``````on every year that is evenly divisible by 4
except every year that is evenly divisible by 100
unless the year is also evenly divisible by 400
``````

For example, 1997 is not a leap year, but 1996 is. 1900 is not a leap year, but 2000 is.

If your language provides a method in the standard library that does this look-up, pretend it doesn't exist and implement it yourself.

## Notes

Though our exercise adopts some very simple rules, there is more to learn!

For a delightful, four minute explanation of the whole leap year phenomenon, go watch this youtube video.

## Getting Started

Make sure you have read the C++ page on 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. 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.

``````#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 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.

## Source

JavaRanch Cattle Drive, exercise 3 http://www.javaranch.com/leap.jsp

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### leap_test.cpp

``````#include "leap.h"
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE(a_known_leap_year)
{
BOOST_REQUIRE(leap::is_leap_year(1996));
}

#if defined(EXERCISM_RUN_ALL_TESTS)
BOOST_AUTO_TEST_CASE(any_old_year)
{
BOOST_REQUIRE(!leap::is_leap_year(1997));
}

BOOST_AUTO_TEST_CASE(turn_of_the_20th_century)
{
BOOST_REQUIRE(!leap::is_leap_year(1900));
}

BOOST_AUTO_TEST_CASE(turn_of_the_21st_century)
{
BOOST_REQUIRE(leap::is_leap_year(2000));
}

BOOST_AUTO_TEST_CASE(turn_of_the_25th_century)
{
BOOST_REQUIRE(leap::is_leap_year(2400));
}
#endif``````
``````#include "leap.h"
using namespace std;

/* contents of leap.h

#ifndef LEAP_H
#define LEAP_H

namespace leap {
bool is_leap_year(int);
}

#endif

**/

bool leap::is_leap_year(int year) {

bool a, b, c;

a = (year % 4 == 0);
b = (year % 100 == 0);
c = (year % 400 == 0);

// c implies b
// b implies a
// c implies a (transitive)

bool result = c || (a && !b);
return result;

}``````