ZOJ 1164 Software CRC

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 34943
int 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;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黎曼猜想·

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值