ZOJ 1164 Software CRC

來源:互聯網
上載者:User

You 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 money on the Ethernet boards you have recommended. You, unwittingly, have pointed out that each of the PCs has come from the vendor with an asynchronous serial port at no extra cost. Dr Pincher, of course, recognizes her opportunity and assigns you the task of writing the software necessary to allow communication between PCs.

You've read a bit about communications and know that every transmission is subject to error and that the typical solution to this problem is to append some error checking information to the end of each message. This information allows the receiving program to detect when a transmission error has occurred (in most cases). So, off you go to the library, borrow the biggest book on communications you can find and spend your weekend (unpaid overtime) reading about error checking.

Finally you decide that 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 below.

The message to be transmitted is viewed as a long positive binary number. The first byte of 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 will be called "m" (for message). Instead of transmitting "m" you will transmit a message, "m2", consisting of "m" followed by a two-byte CRC value.

The CRC value is chosen so that "m2" when divided by a certain 16-bit value "g" leaves a remainder of 0. This 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 is assumed that no error has occurred.

You notice that most of the suggested values of "g" in the book are odd, but don't see any other similarities, so you select the value 34943 for "g" (the generator value).


Input

You are to devise an algorithm for calculating the CRC value corresponding to any message that might be sent. To test this algorithm you will 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 the line, and writes the numeric value of the CRC bytes (in hexadecimal notation) on an output line. Each input line will contain no more than 1024 ASCII characters. The input is terminated by a line that contains a # in column 1.


Output

CRC in hex format, one on each line. Note that each CRC printed should be in the range 0 to 34942 (decimal).


Sample Input

this is a test

A
#


Sample Output

77 FD
00 00

0C 86

題目大意:設計一個演算法,計算與可能發送的任何訊息對應的CRC值。為了測試這個演算法,您將編寫一個程式,讀取輸入行(每個字元都是字元,但不包括分行符號)作為輸入,每行計算該行中包含的訊息的CRC值,並在輸出行上寫入CRC位元組(十六進位記數法)的數值。

分析:計算一行所有字元,除以g的餘數

然後計算CRC,若前面為0,則CRC為0。否則,RCR=g-L;

AC代碼:

#include <stdio.h>#define g 34943int main(){    unsigned char s,flag;    int cnt;//一行中的字元數    //CRC的資料結構    union    {    unsigned int L;    struct    {        unsigned char b1;        unsigned char b2;        unsigned char b3;        unsigned char b4;    }crc;    }data;    while(1)    {        cnt=0;        data.L=0;        while(scanf("%c",&s)&&s!='\n')        {            cnt++;            flag=s;            data.L=((data.L<<8)+s)%g;        }        //將發送資訊的餘數,移到整數最高 和次高位元組        data.L=(data.L<<16)%g;        if(data.L!=0)            data.L=g-data.L;        if(cnt==1&&flag=='#')            break;        printf("%02X %02X\n",(int)data.crc.b2,(int)data.crc.b1);    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.