-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
91 lines (79 loc) · 1.55 KB
/
main.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include<iostream>
#include<stdexcept>
#include<cmath>
#include<string>
unsigned int GetBitsNum(unsigned int x)
{
unsigned int result=0;
while(result++,x>>=1);
return result;
}
unsigned int GetMask(std::size_t len)
{
unsigned int mask=0x00;
while(len!=0)
{
mask=(mask<<1)+1;
len--;
}
return mask;
}
unsigned int BtoD(const std::string &src)
{
unsigned int result=0;
auto count=src.size()-1;
for(auto &c:src)
{
if(c!='0'&&c!='1')
throw std::runtime_error("the data is not binary");
result=result+std::pow(2,count)*(c-48);
count--;
}
return result;
}
bool CRCcheck(const std::string &Checksum,const std::string &Src)
{
if(Checksum.size()>Src.size())
throw std::runtime_error("Checksum's length is short than srouce data");
unsigned int check=BtoD(Checksum);
unsigned int src=BtoD(Src);
auto checkSize=Checksum.size();
auto srcSize=Src.size();
auto mask=GetMask(checkSize);
while(srcSize>=checkSize)
{
auto sizeDiff=srcSize-checkSize;
auto temp=check^(src>>sizeDiff);
src=temp<<sizeDiff|(src&~(mask<<sizeDiff));
srcSize=GetBitsNum(src);
}
if(src!=0)
return false;
else
return true;
}
int main()
{
std::string Checksum;
std::cout<<"please enter the checksum"<<std::endl;
std::cin>>Checksum;
std::string Src;
std::cout<<"please enter the data you received"<<std::endl;
std::cin>>Src;
try
{
if(CRCcheck(Checksum,Src))
{
std::cout<<"data is right"<<std::endl;
}
else
{
std::cout<<"data error"<<std::endl;
}
}
catch(std::runtime_error err)
{
std::cerr<<err.what()<<std::endl;
}
return 0;
}