Add some error handling
This commit is contained in:
parent
77725500b9
commit
3e6d9b6490
4 changed files with 17 additions and 4 deletions
|
@ -1,4 +1,8 @@
|
|||
# lychrel
|
||||
Iterate through numbers to rule out that they are lychrel numbers
|
||||
|
||||
compile with: g++ -o lychrel lychrel.cpp lychrel.hpp main.cpp
|
||||
|
||||
run with: ./lychrel <a number you want to check>
|
||||
|
||||
https://en.wikipedia.org/wiki/Lychrel_number
|
||||
|
|
BIN
lychrel
Executable file
BIN
lychrel
Executable file
Binary file not shown.
|
@ -1,5 +1,6 @@
|
|||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <climit>
|
||||
#include "lychrel.hpp"
|
||||
|
||||
long long get_len (string a, string b) {
|
||||
|
@ -14,7 +15,7 @@ string add (string a, string b) {
|
|||
|
||||
char result[len + 2] = { '0' };
|
||||
|
||||
if(len == -1) {
|
||||
if(len == -1 or len == LLONG_MAX) {
|
||||
return "-1";
|
||||
} else {
|
||||
int reg = 0;
|
||||
|
|
14
main.cpp
14
main.cpp
|
@ -1,5 +1,6 @@
|
|||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <climits>
|
||||
#include "lychrel.hpp"
|
||||
|
||||
|
||||
|
@ -8,16 +9,23 @@ int main(int argc, char **argv)
|
|||
string a = argv[1];
|
||||
string b(a.rbegin(),a.rend());
|
||||
string added = add(a,b);
|
||||
if(added == "-1") {
|
||||
cerr << "ERROR: adding error detected, we might have reached the end of our datatype or some how the two numbers don't have the same legth\n";
|
||||
return 1;
|
||||
}
|
||||
long long it = 0;
|
||||
do {
|
||||
cout << "Iteration " << ++it << " currently working on " << added << endl;
|
||||
a = added;
|
||||
b = string(a.rbegin(),a.rend());
|
||||
added = add(a,b);
|
||||
} while(! is_palindrome(added) );
|
||||
if(added == "-1") {
|
||||
cerr << "ERROR: adding error detected, we might have reached the end of our datatype or some how the two numbers don't have the same legth\n";
|
||||
return 1;
|
||||
}
|
||||
} while(! is_palindrome(added));
|
||||
cout << "Iteration " << ++it << " currently working on " << added << endl;
|
||||
cout << "That consists of\n" << a << "\n+\n" << b << "\nand that it is a palindrome." << endl;
|
||||
|
||||
cout << "That consists of\n" << a << "\n+\n" << b << "\nand that it " << (is_palindrome(added) ? "is" : "is not") << " a palindrome." << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue