robot_name: iteration 1
This commit is contained in:
parent
a882941d58
commit
7339e6a8bc
2 changed files with 72 additions and 0 deletions
47
cpp/robot-name/robot_name.cpp
Normal file
47
cpp/robot-name/robot_name.cpp
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
#include "robot_name.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
|
||||||
|
namespace robot_name {
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
const size_t alpha_part_size = 2;
|
||||||
|
const size_t digit_part_size = 3;
|
||||||
|
|
||||||
|
string random_string(size_t size, char from_char, char to_char)
|
||||||
|
{
|
||||||
|
auto random_char = [from_char, to_char]() -> char {
|
||||||
|
return from_char + rand() % (to_char - from_char + 1);
|
||||||
|
};
|
||||||
|
|
||||||
|
string result(size, 0);
|
||||||
|
generate_n(result.begin(), size, random_char);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
robot::robot()
|
||||||
|
{
|
||||||
|
reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
void robot::reset()
|
||||||
|
{
|
||||||
|
name_ = random_string(alpha_part_size, 'A', 'Z')
|
||||||
|
+ random_string(digit_part_size, '0', '9');
|
||||||
|
}
|
||||||
|
|
||||||
|
const name_t &robot::name() const
|
||||||
|
{
|
||||||
|
return name_;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
25
cpp/robot-name/robot_name.h
Normal file
25
cpp/robot-name/robot_name.h
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#define EXERCISM_RUN_ALL_TESTS
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace robot_name {
|
||||||
|
|
||||||
|
typedef std::string name_t;
|
||||||
|
|
||||||
|
|
||||||
|
class robot
|
||||||
|
{
|
||||||
|
name_t name_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
robot();
|
||||||
|
|
||||||
|
void reset();
|
||||||
|
|
||||||
|
const name_t &name() const;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue