Delete
Delete
Last edited on
You wrote it wrong.
Write it correctly.
std::stoi() might throw.
Here some hints:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <sstream>
#include <string>
class osoba {
public:
osoba();
osoba(const osoba& orig);
osoba(const std::string&);
virtual ~osoba() = default;
bool setRc(const std::string&);
std::string getRc() const;
std::string datum() const;
int vek() const;
private:
std::string rc;
bool kontrolaRC(const std::string&) const;
int den() const;
int mesic() const;
int rok() const;
};
constexpr char Default_Rc[] { "9999999999" };
osoba::osoba()
: rc { Default_Rc }
{
}
osoba::osoba(const osoba& orig)
{
rc = orig.getRc();
}
osoba::osoba(const std::string& rc_arg)
{
if(kontrolaRC(rc_arg)) {
rc = rc_arg;
}
else {
rc = Default_Rc;
}
}
bool osoba::kontrolaRC(const std::string& rc_arg) const
{
if(rc_arg.length() != 10) {
std::cout << "Check failed: there should be exactly 10 characters.\n";
return false;
}
try {
int prvnich9 = std::stoi( rc_arg.substr(0, 9) );
int kontrolniCislo = prvnich9 % 11;
if(kontrolniCislo == 10 && rc_arg[9] == '0'){
return true;
}
else if( kontrolniCislo == std::stoi(rc_arg.substr(9,1)) ) {
return true;
}
} catch (std::invalid_argument& e) {
std::cout << "osoba::kontrolaRC(): std::invalid_argument exception "
"has been thrown by std::stoi():\n"
<< e.what() << "\nExiting now.\n";
std::exit(EXIT_FAILURE);
} catch (std::out_of_range& e) {
std::cout << "osoba::kontrolaRC(): std::out_of_range exception has "
"been thrown by std::stoi():\n"
<< e.what() << "\nExiting now.\n";
std::exit(EXIT_FAILURE);
}
return false;
}
std::string osoba::getRc() const
{
return rc;
}
bool osoba::setRc(const std::string& rc_arg)
{
if(kontrolaRC(rc_arg)){
rc = rc_arg;
return true;
}
return false;
}
int osoba::rok() const
{
int ret_val;
try {
ret_val = std::stoi(rc.substr(0,2));
if(ret_val < 54) {
return ret_val + 2000;
}
} catch (std::invalid_argument& e) {
std::cout << "osoba::rok(): std::invalid_argument exception has been "
"thrown by std::stoi():\n"
<< e.what() << "\nExiting now.\n";
std::exit(EXIT_FAILURE);
} catch (std::out_of_range& e) {
std::cout << "osoba::rok(): std::out_of_range exception has been "
"thrown by std::stoi():\n"
<< e.what() << "\nExiting now.\n";
std::exit(EXIT_FAILURE);
}
return ret_val + 1900;
}
int osoba::mesic() const
{
int ret_val;
try {
ret_val = std::stoi(rc.substr(2,2));
if(ret_val > 50) {
return ret_val - 50;
}
} catch (std::invalid_argument& e) {
std::cout << "osoba::mesic(): std::invalid_argument exception has been"
" thrown by std::stoi():\n"
<< e.what() << "\nExiting now.\n";
std::exit(EXIT_FAILURE);
} catch (std::out_of_range& e) {
std::cout << "osoba::mesic(): std::out_of_range exception has been "
"thrown by std::stoi():\n"
<< e.what() << "\nExiting now.\n";
std::exit(EXIT_FAILURE);
}
return ret_val;
}
int osoba::den() const
{
int ret_val;
try {
ret_val = std::stoi(rc.substr(4, 2));
} catch (std::invalid_argument& e) {
std::cout << "osoba::den(): std::invalid_argument exception has been "
"thrown by std::stoi():\n"
<< e.what() << "\nExiting now.\n";
std::exit(EXIT_FAILURE);
} catch (std::out_of_range& e) {
std::cout << "osoba::den(): std::out_of_range exception has been "
"thrown by std::stoi():\n"
<< e.what() << "\nExiting now.\n";
std::exit(EXIT_FAILURE);
}
return ret_val;
}
std::string osoba::datum() const
{
std::ostringstream vystup;
vystup << den() << '.' << mesic() << '.' << rok();
return vystup.str();
}
int osoba::vek() const
{
int vek;
std::time_t t = std::time(0); // get time now
std::tm* now = std::localtime(&t);
int sysrok = now->tm_year + 1900;
int sysmesic = now->tm_mon + 1;
int sysden = now->tm_mday;
vek = sysrok - rok();
if(sysmesic < mesic()) {
--vek;
} else if(sysmesic == mesic() && sysden < den()) {
--vek;
}
return vek;
}
int main()
{
osoba o1;
std::cout << "Nekdo ma rc " << o1.getRc()
<< "\nNekdo je narozen " << o1.datum() << '\n';
for(;;) {
std::cout << "Zadej rodne cislo: ";
std::string RC;
std::cin >> RC;
osoba o2;
if(o2.setRc(RC)) {
std::cout << "Nekdo ma RC: " << o2.getRc()
<< "\nNekdo je narozen " << o2.datum()
<< "\nVek " << o2.vek() << " let\n";
} else {
std::cout << "Chybne RC.\n";
}
}
return 0;
}
|
Please, let us know if that can be of any help.
Topic archived. No new replies allowed.