Boa tarde, quis fazer o desafio de forma a deixar cadastrar cpfs no formato xxx.xxx.xxx-xx e xxxxxxxxxxx. Poderiam me dizer se está de acordo, bem feito e eficiente?
//Cpf.hpp
#pragma once
#include <string>
class CPF
{
private:
std::string numero;
bool verificaCpfComCaracteresEspeciais() const;
bool verificaCpfSemCaracteresEspeciais() const;
bool verificaCpf() const;
bool ehNumero(char i) const;
public:
CPF(std::string numero);
std::string get() const;
};
//Cpf.cpp
#include "Cpf.hpp"
#include <iostream>
#include <stack>
CPF::CPF(std::string numero)
:numero(numero)
{
verificaCpf();
}
bool CPF::verificaCpfComCaracteresEspeciais() const {
int contNumeros = 0;
std::stack <char> pilha;
if (numero.size() > 14) {
return false;
}
for (char i : numero) {
if (contNumeros > 11) {
return false;
}
if (ehNumero(i)) {
if (contNumeros==0 || contNumeros % 3 != 0||!ehNumero(pilha.top())) {
contNumeros++;
pilha.push(i);
}
else {
return false;
}
}
else
{
if (i == '.') {
if (contNumeros!=0 && contNumeros % 3 == 0 && contNumeros!=9 && ehNumero(pilha.top())) {
pilha.push(i);
}
else {
return false;
}
}
if (i == '-') {
if (contNumeros == 9 && ehNumero(pilha.top())) {
pilha.push(i);
}
else {
return false;
}
}
}
}
return true;
}
bool CPF::verificaCpfSemCaracteresEspeciais() const {
int contNumeros=0;
if (numero.size() > 11) {
return false;
}
for (char i : numero) {
if (!ehNumero(i)){
return false;;
}
}
return true;
}
bool CPF::verificaCpf() const {
if (!verificaCpfSemCaracteresEspeciais() && !verificaCpfComCaracteresEspeciais()) {
std::cout << "CPF INVALIDO" << std::endl;
exit(1);
}
return true;
}
bool CPF::ehNumero(char i) const {
return (i >= '0') && (i <= '9');
}
std::string CPF::get() const {
return numero;
}