该用户从未签到
|
有谁能帮我把CRC算法实现换个编法啊??帮忙啊,急用
* V+ S+ W4 e: b) I#include
& Z5 O a, Y& ?. c1 _" w& D#include' ^5 u1 h! T* W4 l6 ~; a& X0 b
#include2 h# e+ B& ?4 a* ^/ s
#define CRC 5- A& i: b% v, s2 O2 f, @# K% ^. v. D
char data[5]={'1','1','0','0','1'};, ^$ R/ K8 j' v* Y% L ^4 p* P2 A
class Checkout
+ `3 d* w% ~, U0 l{
8 X. e ~# [3 yprivate:
0 M$ k+ m+ {9 t+ J( t5 P char ax[50];$ L/ L/ H2 |3 h' R4 U
char bx[4];
( R6 ^5 ^* r/ L7 c5 ] int x;
' f4 W4 x2 Q% b! Z( Xpublic:
& ~5 h) ?. u. I- d5 |' v7 `# ] int length; z* o, S- l# g
Checkout()" s- Z) _2 I- W5 P( M" @& Q/ y
{
8 s { k/ Z9 x cin>>ax;
6 u! p2 o4 a2 ~3 | n( s length=strlen(ax);6 H7 o3 r9 ]" Z# ^% g, _
}
3 j/ a; m/ {8 R$ U, G void cxc();9 [3 l/ P" G& ?& k* p9 |
bool qyu();* B& J/ ?& k+ u/ \$ r
void add();7 G% ?& L% \; `+ J& W0 _0 E/ W' {
};
# A! N& D6 q& s8 Q& nvoid Checkout::cxc()$ c& q: Z F! Q! k# S
{
$ Z. h8 ~0 I H+ i- w for(int i=0;i<4;i++){6 c; z' V5 b6 A( Q: b
ax[length+i]='0';( I" r" y9 Y! n
}! }( s" L$ r! e+ m! H
}//将输入序列后加4个0
' o( R! |4 x* ?& Z5 \7 T+ M! N: Lbool Checkout::qyu()" X: J- |0 o$ I& \8 J
{4 T( F% t+ Y k {2 A. G9 q
char c[5];
; d/ ?+ c0 u9 I4 t7 i, N int i,j,n;
, A2 K' M9 P' f, R$ e/ J. x3 V4 \; | int m=5;
# V- c* J0 X# O# r for(i=0;ax=='0';i++);: v3 g2 P& }1 o6 X
if(i==length)1 o) d0 N7 U8 o) A! T
{cout<<"余式是0"<: N# v7 D! W, q0 Z cout<<"发送成功"<- _5 ]) n/ p; N exit(0);
/ V. u1 X# d) T1 T }! p/ p: u: L& M6 h% ?" {
else{
5 `9 ]! D& v9 x" w for(i=0;i<5;i++)
% r& U" L6 f! X; \# T! I c=ax;; ?( v0 I0 R) f+ s! C- h
while(m==CRC)2 b/ {4 ]& G) q/ P" {7 v% X$ u% k9 x
{ for(j=0;j9 j, K# z, ^4 t2 k6 S
{
4 F3 v3 Q- U+ y2 ? if(c[j]==data[j])
. x0 D2 T/ @: {4 q( G c[j]='0';7 n* c+ B3 q S2 A, [) o
else
0 O5 d+ s5 J# Q' u# A3 L c[j]='1';4 k4 @/ A$ U! A) i! ~3 `: a
}( X7 t3 q/ O- S/ d; s
for(j=0;c[j]=='0';j++);
+ J* X. N+ N* W! W3 F n=j;2 L5 N3 q8 a! w/ Q5 v" S5 L
for(m=0;m. ]6 [7 Z2 U6 c0 C- }
{
/ ^9 n7 z% o+ ^* _5 b c[m]=c[j];9 b( \. R- S4 |2 U5 x$ J# v
j++;1 Z2 M1 t! n+ D, r
}
0 [" Z A( I$ m/ O4 X while(i4 q2 y8 `: X2 Z" \ {2 i1 c9 ?! `+ R7 j! l' D5 E
c[m]=ax[i++]; # `) f, W* r& B X! H; d+ n
m++;
" Y" T$ V2 M6 o1 J2 M% G9 b) A& k9 r }
7 U. Y2 d4 |( X8 ^ }
: i7 k# ~8 ?! v" h& [}
" O* x7 l; E9 e2 i- [& |; b8 X g& D- [ int k;
9 F4 S+ p: T3 f* A& k for(k=0;k8 y0 ^% K: G/ l bx[k]=c[k];
4 E7 \$ u7 k4 n U# T for(j=0;bx[j]=='0';j++);
6 z N: o" Z& O% p' |5 Q+ v if(j==m)
" C' B" d# e* G c& P {cout<<'0'<, V! e% F! P' b: }4 K% @/ g0 T
x=0;* d$ b [" ]4 z7 E* ^
return true;6 u9 M& a& q o- m
}" W# x1 ]9 V6 H& A( G* o1 ^1 |
else8 a9 E; I. G9 t8 a- n) Y' d
{for(n=j;n% F, A2 g: G- |* ?2 ~ cout<( \2 Y# B& K3 A! T
x=m-j;
0 q0 G, h6 p+ O ` cout<6 N# C5 c! c- t/ o return false;
% _+ n9 }; \* \9 ] }
" c7 d; w9 C* q- V& M8 z5 F}, q0 g3 H7 v# A6 D ^7 P. U. f
void Checkout::add(); [7 l9 | s* u% W
{8 V2 C: E" k6 b) f
int j=0;# \( U J! p: {! p# q' n
for(int i=length-x;i: J5 E1 Y0 Z% N, x. c# t
ax=bx[j++];
! y6 Z, ~& `% G8 O cout<<"发送的序列加上余数后变成:";- ^7 @( Y. `7 o/ Z$ @$ N
for(i=0;i/ B1 k1 h5 X& l* [ cout<;- D8 m7 ]: H9 ?) W
cout<* j# T: l' P$ h6 v8 {2 _1 [}
( D. K2 u( R! |, W1 D9 m" hvoid main()7 g9 ^9 O2 T1 G I# s! P6 B2 q
{
9 _+ u' q; Q4 b. S$ f8 E cout<<"本次实验模拟CRC校验,其生成多项式的比特序列是11001"<$ N l. U3 s2 I9 _ cout<<"请输入发送数据比特序列:"<" F5 Y$ L& I: x Checkout test;9 G( V* T6 S# i$ F4 [) h/ `
test.cxc();- I6 \7 f# }: U( s" y& a
cout<<"余式比特序列是:";2 q- q1 B+ H' h# d
test.qyu();
2 k n5 |# [) n+ ^ test.add();* @# V7 g1 n) b1 c
cout<<"试试这次是否能被整除."<* C: H; }. ]3 D
cout<<"新余数是:";$ M2 a0 d5 Y6 B9 K5 q) Y& H! C
if(test.qyu())2 B) x, T2 b2 \
cout<<"已发送成功!"<! M6 U- u( w8 |) X
else
- I& G6 H- S5 P# q cout<<"未发送,失败!"<! T3 _! T; A, U+ R, R
} |
|