Witam,
Potrzebuję zrobić program, który obliczał by sumę kontrolną BSD dla wpisanego tekstu. W internecie jest bardzo mało materiałów, ale natknąłem się na algorytm na stronie: http://en.wikipedia.org/wiki/BSD_checksum , który wygląda tak:
FILE *fp; /* The file handle for input data */
int ch; /* Each character read. */
int checksum = 0; /* The checksum mod 2^16. */
while ((ch = getc(fp)) != EOF) {
checksum = (checksum >> 1) + ((checksum & 1) << 15);
checksum += ch;
checksum &= 0xffff; /* Keep it within bounds. */
}
Próbowałem skompilować poniższy kod w Dev C++, ale próba jest nieudana (program się zawiesza) .
#include <iostream>
using namespace std;
int main()
{
FILE *fp; /* The file handle for input data */
int ch; /* Each character read. */
int checksum = 0; /* The checksum mod 2^16. */
while ((ch = getc(fp)) != EOF) {
checksum = (checksum >> 1) + ((checksum & 1) << 15);
checksum += ch;
checksum &= 0xffff; /* Keep it within bounds. */
}
}
Ma ktoś pomysł jak poprawnie zrobić ten program?