UVa 128 Software CRC: Modulus calculation and CRC cyclic redundancy check Codes

Source: Internet
Author: User
Tags modulus numeric value strlen

128-software crctime limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_ Problem&problem=64

You are work for a company which uses lots of personal computers. Your boss, Dr Penny Pincher, has wanted to link the computers together for some time but has been unwilling to spend any m Oney on the Ethernet boards for you have recommended. You, unwittingly, have pointed out of this each of the PCs has come to the vendor with a asynchronous port at no ex Tra cost. Dr Pincher, of course, recognizes her opportunity and assigns your task of writing the software necessary to allow Comm Unication between PCs.

' ve read a bit about communications and know that every transmission are subject to error and that typical solution To this problem be to append some error checking information to the "End of". This information allows the receiving program to detect when a transmission error has occurred (in most). So, from the go to the library, borrow the "biggest book" on communications for you can find and spend the your weekend (unpaid overt IME) reading about error checking.

Finally You decide so CRC (cyclic redundancy check) is the best error checking for your situation and write a note to Dr Pincher detailing the proposed error checking mechanism noted.

CRC Generation

The message to was transmitted is viewed as a long positive binary number. The the message is treated as the most significant byte of the binary number. The second byte is the next most significant, etc. This binary number is called ' m ' (for message). Instead of transmitting ' m ' ' you'll transmit a message, ' M2 ', consisting of ' ' m ' followed by a two-byte CRC value.

The CRC value is chosen so "m2" when divided by a certain 16-bit value ' g ' leaves a remainder of 0. This is makes it easy for the receiving program to determine whether the message has been corrupted by transmission errors. It simply divides any message received by ' G '. If the remainder of the division is zero, it's assumed that no error has occurred.

You are notice that most of the suggested values of "G" in the book are odd, but don ' t Lect the value 34943 for ' G ' (the generator value).

Input and Output

You are are to devise a algorithm for calculating the CRC value corresponding to any message that might is sent. To the test this algorithm you would write a program which reads lines (each line being all characters up to, but not including The end of line character) as input, and for each line calculates the CRC value for the "message contained in", an D writes the numeric value of the CRC bytes (in hexadecimal notation) on a output line. Each input line would contain no more than 1024 ASCII characters. The "input is terminated by a" that contains a # in column 1. Note This each CRC printed should is in the range 0 to 34942 (decimal).

Sample Input

This is a test

a
#

Sample Output

FD
0C 86

A key place in the title is not clear, is to turn each character into an ASCII code, so m is equivalent to a strlen (s) bit of 256, M2 equivalent to a strlen (s) + 2-bit 256-digit, and m2 decimal representation can be divisible by 34942.

We are asking for these two 256 numbers, separated by a space in the middle.

See the code for the specific procedure.

Complete code:

/*0.105s*/
  
#include <cstdio>
#include <cstring>
  
int len;
unsigned int ans;
Char s[1030], output[4];
  
int main (void)
{
    while (gets (s), s[0]!= ' # ')
    {
        len = strlen (s);
        Ans = 0;
        for (int i = 0; i < len; i++)
            ans = (ans << 8) + s[i])% 34943;
        Ans = (ans <<)% 34943;
        Ans = (ans. 34943-ans:0);
        sprintf (output, "%04x\n", ans);///16, the length is not 4 to fill 0 (no 0 to fill the space) for
        (int i = 0; i < 4; i++)
            if (output[i) >= ' a ' && Output[i] <= ' z ')
                output[i] = output[i]-' a ' + ' a ';
        printf ("%c%c%c%c\n", output[0], output[1], output[2], output[3]);
    return 0;
}

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.