phone_number: iteration 1
This commit is contained in:
parent
c5ea194574
commit
1024285d52
2 changed files with 29 additions and 5 deletions
|
|
@ -1,13 +1,33 @@
|
||||||
#include "phone_number.h"
|
#include "phone_number.h"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <ctype>
|
#include <cctype>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
static const int number_size = 10;
|
||||||
|
static const int area_code_size = 3;
|
||||||
|
static const int first_part_size = 3;
|
||||||
|
|
||||||
|
static const char invalid_number[] = "0000000000";
|
||||||
|
|
||||||
|
|
||||||
phone_number::phone_number(const string &str)
|
phone_number::phone_number(const string &str)
|
||||||
{
|
{
|
||||||
static const string invalid_number = "0000000000";
|
number_.resize(str.size());
|
||||||
|
string::iterator iter = copy_if(str.cbegin(), str.cend(),
|
||||||
|
number_.begin(), static_cast<int (*)(int)>(isdigit));
|
||||||
|
number_.resize(distance(number_.begin(), iter));
|
||||||
|
|
||||||
|
if (number_.size() == number_size + 1 && number_.front() == '1') {
|
||||||
|
|
||||||
|
number_.erase(number_.begin());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (number_.size() != number_size) {
|
||||||
|
|
||||||
|
number_ = invalid_number;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
string phone_number::number() const
|
string phone_number::number() const
|
||||||
|
|
@ -17,10 +37,13 @@ string phone_number::number() const
|
||||||
|
|
||||||
string phone_number::area_code() const
|
string phone_number::area_code() const
|
||||||
{
|
{
|
||||||
return area_code_;
|
return number_.substr(0, area_code_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
phone_number::operator string() const
|
phone_number::operator string() const
|
||||||
{
|
{
|
||||||
return number_;
|
return string("(") + area_code() + ") "
|
||||||
|
+ number_.substr(area_code_size, first_part_size)
|
||||||
|
+ '-'
|
||||||
|
+ number_.substr(area_code_size + first_part_size, string::npos);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#define EXERCISM_RUN_ALL_TESTS
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
class phone_number
|
class phone_number
|
||||||
{
|
{
|
||||||
std::string area_code_;
|
|
||||||
std::string number_;
|
std::string number_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue