Add a solution for lcd

This commit is contained in:
Patricia Aas 2020-11-30 17:06:49 +01:00
parent 457bad18c1
commit 11d5bda4a5
3 changed files with 190 additions and 0 deletions

68
lcd/solution/lcd.cpp Normal file
View file

@ -0,0 +1,68 @@
#include "lcd.hpp"
lcd_grid lcd(const std::string & s1,
const std::string & s2,
const std::string & s3) {
lcd_grid result;
result.push_back(s1);
result.push_back(s2);
result.push_back(s3);
return result;
}
const lcd_grid digits[] =
{
lcd(" _ ",
"| |",
"|_|"
),
lcd(" ",
" |",
" |"
),
lcd(" _ ",
" _|",
"|_ "
),
lcd(" _ ",
" _|",
" _|"
),
lcd(" ",
"|_|",
" |"
),
lcd(" _ ",
"|_ ",
" _|"
),
lcd(" _ ",
"|_ ",
"|_|"
),
lcd(" _ ",
" |",
" |"
),
lcd(" _ ",
"|_|",
"|_|"
),
lcd(" _ ",
"|_|",
" |"
),
};
lcd_grid lcd(int value) {
if (value < 10)
return digits[value];
else {
lcd_grid lhs = lcd(value / 10);
lcd_grid rhs = digits[value % 10];
return lcd(
lhs[0] + ' ' + rhs[0],
lhs[1] + ' ' + rhs[1],
lhs[2] + ' ' + rhs[2]);
}
}

110
lcd/solution/lcd_tests.cpp Normal file
View file

@ -0,0 +1,110 @@
#include <gtest/gtest.h>
#include "lcd.hpp"
#include <iostream>
std::string to_string(const lcd_grid & grid) {
std::stringstream output;
for (const auto & str: grid)
output << str;
return output.str();
}
void lcd_spec(int value, const lcd_grid & grid) {
std::string expected = to_string(grid),
actual = to_string(lcd(value));
if (expected != actual) {
std::cerr
<< "lcd(" << value << ")\n"
<< "expected==\n"
<< expected << '\n'
<< "actual==\n"
<< actual << std::endl;
std::exit(EXIT_FAILURE);
}
}
TEST(LcdTest, Zero) {
lcd_spec(0, lcd(
" _ ",
"| |",
"|_|"
));
}
TEST(LcdTest, One) {
lcd_spec(1, lcd(
" ",
" |",
" |"
));
}
TEST(LcdTest, Two) {
lcd_spec(2, lcd(
" _ ",
" _|",
"|_ "
));
}
TEST(LcdTest, Three) {
lcd_spec(3, lcd(
" _ ",
" _|",
" _|"
));
}
TEST(LcdTest, Four) {
lcd_spec(4, lcd(
" ",
"|_|",
" |"
));
}
TEST(LcdTest, Twelve) {
lcd_spec(12, lcd(
" _ ",
" | _|",
" | |_ "
));
}
TEST(LcdTest, TwentyFive) {
lcd_spec(25, lcd(
" _ _ ",
" _| |_ ",
"|_ _|"
));
}
TEST(LcdTest, SixtyFour) {
lcd_spec(64, lcd(
" _ ",
"|_ |_|",
"|_| |"
));
}
TEST(LcdTest, SeventyFour) {
lcd_spec(74, lcd(
" _ ",
" | |_|",
" | |"
));
}
TEST(LcdTest, EightyNine) {
lcd_spec(89, lcd(
" _ _ ",
"|_| |_|",
"|_| |"
));
}
int main(int argc, char * argv[]) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

12
lcd/solution/main.cpp Normal file
View file

@ -0,0 +1,12 @@
#include <iostream>
#include "lcd.hpp"
static const int MAX = 100;
int main() {
for (int i = 0; i < MAX; i++) {
auto grid = lcd(i);
for (const auto & line: grid)
std::cout << line << "\n";
}
}