Created
January 27, 2017 18:36
-
-
Save trueskawka/7b831a9a10d77fc54759864536050efc to your computer and use it in GitHub Desktop.
checksum in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <utility> | |
int get_sum(int num) | |
{ | |
num *= 2; | |
int sum = 0; | |
// if (num < 10) sum = num; else sum = num % 10 + 1; | |
for (sum = 0; num > 0;) | |
{ | |
sum += (num % 10); | |
num /= 10; | |
} | |
return sum; | |
} | |
int main() | |
{ | |
int even = 0; | |
int odd = 0; | |
// int i = 1; | |
FILE *fp = stdin; | |
// int ch = 0; | |
int &left = even; | |
int &right = odd; | |
while(( int ch = getc(fp)) >= '0' && ch <= '9') | |
{ | |
ch = ch - '0'; | |
// even += i * ch + (i^1) * get_sum(ch); | |
// odd += (i^1) * ch + i * get_sum(ch); | |
left += ch; | |
right += get_sum(ch); | |
// even %= 10; | |
// odd %= 10; | |
// i = i^1; | |
left %= 10; | |
right %= 10; | |
std::swap(left,right); | |
} | |
printf("result %d\n", (10-left) % 10); | |
// if (i) printf("%d",(10-even)%10); else printf("%d",(10-odd)%10); | |
return (10-left)%10; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int doubleDigitValue(int digit) | |
{ | |
int doubledDigit = digit * 2; | |
int sum; | |
if (doubledDigit > 10) sum = 1 + doubledDigit % 10; else sum = doubledDigit; | |
return sum; | |
} | |
int main() | |
{ | |
// checking if a checksum is divisible by 10 | |
char digit; | |
int oddLengthChecksum = 0; | |
int evenLengthChecksum = 0; | |
int position = 1; | |
cout << "Enter a number: "; | |
digit = cin.get(); | |
// this is waiting for `Enter` | |
// not taking into account other options | |
while (digit != 10) { | |
if (position % 2 == 0) | |
{ | |
// none of these sums is using % 10 | |
// which means it can overflow the datatype size | |
oddLengthChecksum += doubleDigitValue(digit - '0'); | |
evenLengthChecksum += digit - '0'; | |
} else { | |
oddLengthChecksum += digit - '0'; | |
evenLengthChecksum += doubleDigitValue(digit - '0'); | |
} | |
digit = cin.get(); | |
// this is summing up the number length | |
// instead of simply having a binary value | |
// for odd/even numbers | |
position++; | |
} | |
int checksum; | |
if ((position - 1) % 2 == 0) checksum = evenLengthChecksum; | |
else checksum = oddLengthChecksum; | |
cout << "Checksum is " << checksum << ". \n"; | |
if (checksum % 10 == 0) | |
{ | |
cout << "Checksum is divisible by 10. Valid. \n"; | |
} else { | |
cout << "Checksum is not divisible by 10. Invalid. \n"; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment