辅导C++编程、讲解C++程序、辅导C++、辅导留学生C++程序
辅导留学生 Statistics统计、回归、迭代|解析Java程序
#include
#include
#include
#include
#include
using namespace std;
const int n=7,k=4,r=3;
class HMCoding{
private:
int H[r][n],**G;
string H_Column[n+1];
int source_num;
string binary_data,coded_data;
char*received_data,**check_code;
void Show_H(int , int);
void Get_G();
void Show_G(int,int);
void Get_H_Column();//
public:
HMCoding( int num, string str)
{
source_num= num;
binary_data=str;
};
~HMCoding()
{
delete[]G;
delete[]received_data;
delete[]check_code;
};
void Initializing();
void Encoding();//
void BSC();
void Checking();//
void Decoding();//
};
//
void HMCoding::Initializing()
{
int h[r][n]={
1,1,1,0,1,0,0,
1,1,0,1,0,1,0,
1,0,1,1,0,0,1};
for(int i=0;i for(int j=0;j H[i][j]=h[i][j];
cout<<" H["< Show_H(n-k,n);
cout<<" G["< Show_G(k,n);
Get_H_Column();//
}
//,
void HMCoding::Get_H_Column()
{
int i,j;
string temp;
for(i=0;i {
temp="";
for(j=0;j {
if(!H[j][i])
temp +=(char)0;
else
temp +=(char)1;
}
H_Column[i]=temp;
}
temp="";
for(j=0;j temp +=(char)0;
H_Column[n]=temp;
}
void HMCoding::Show_H(int x,int y)
{
for(int i=0;i {
for(int j=0;j cout< cout< }
}
void HMCoding::Get_G()
{
G=new int *[k];
for(int i=0;i G[i]= new int[n];
for(int i=0;i for(int j=0;j {
if(i==j)
G[i][j]=1;
else
G[i][j]=0;
}
for(int i=0;i for(int j=0;j G[j][i+k]=H[i][j];
}
void HMCoding::Show_G(int x,int y)
{
Get_G();
for(int i=0;i {
for(int j=0;j cout< cout< }
}
//
void HMCoding::Encoding()
{
cout<<"("< for(int i=0;i cout<<(int)binary_data[i];
cout<
int*X;
X=new int[n+1];
for(int j=0;j X[j]=0;
coded_data="";
for(int i=0;i {
for(int j=0;j {
if(binary_data[i+j]==0) //‘0’
X[j]=0;
else
X[j]=1;
}
int temp;
string partial_str="";
for(int t=0;t {//k,n
temp=0;
for(int j=0;j temp +=X[j]*G[j][t];
if(temp%2==0)
partial_str +=(char)0;//'0'
else
partial_str +=(char)1;//'1'
}
coded_data +=partial_str;
}
delete []X;
cout<<"("< for(int i=0;i cout<<(int)coded_data[i];
cout<}
//
void HMCoding::Checking()
{
int i,j;
check_code=new char*[source_num];
for(i=0;i check_code[i]=new char[n];
for(i=0;i {//n
for(j=0;j {
check_code[i][j]=received_data[i*n+j];
}
}
int temp;
int flag;
string partial_str;
cout<<"("< cout<<" "< for(int t=0;t {
flag=0;
partial_str="";
for(i=0;i {
temp=0;
for(j=0;j temp +=H[i][j]*check_code[t][j];
if(temp%2==0)
partial_str +=(char)0;
else
partial_str +=(char)1;
}
//partial_str
for(i=0;i {
if(H_Column[i]==partial_str)
{
flag=1;
break;
}
}
if(flag&&i {
for(j=0;j cout<<(int)check_code[t][j];
cout<<" "< check_code[t][i]=(check_code[t][i]+1)%2;//10,01
for(j=0;j cout<<(int)check_code[t][j];
}
if(flag&&i==n)//
{
for(j=0;j cout<<(int)check_code[t][j];
cout<<" ";
for(j=0;j cout<<(int)check_code[t][j];
}
cout< }
}
//
void HMCoding::Decoding()
{
int i,j;
int *decode_data=new int[source_num];
cout<<"("< for(i=0;i {
decode_data[i]=0;
for(j=0;j {
cout<<(int)check_code[i][j];
decode_data[i] += check_code[i][j]<<(k-1-j);
}
}
cout<
for (i=0;i cout< cout< delete[]decode_data;
}
//
void HMCoding::BSC()
{
int r_num=source_num*n;
received_data=new char[r_num];
srand((unsigned)time(NULL));
for(int i=0;i if((rand()/(double)RAND_MAX)<0.07) //7%
received_data[i]=(~coded_data[i])&0x01; //,01,10
else
received_data[i]=coded_data[i];
}
int main()
{
int count=0;
string s="";
char filename[80];
cout<<":";
cin>>filename;
int x;
char c;
ifstream ifile(filename,ios::binary);
while(ifile>>c)
{//,,ASCII。
if(c>='0'&&c<='9')
x=c-0x30;
else if(c>='A'&&c<='F')
x=c-0x41+10;
else if(c>='a'&&c<='f')
x=c-0x61+10;
else
{
x=-1;
cout<<"";
}
if(x>=0)
{
cout< count ++;
for(int i=0;i {//0~a4bit,。
int n=x;
n=n>>(k-1-i);
s +=n&0x01;
}
}
}
cout< if(count<=0)
{
cout<<","< return 0;
}
HMCoding hm74(count,s);
hm74.Initializing();
cout<()->、"< hm74.Encoding();
hm74.BSC();
hm74.Checking();
hm74.Decoding();
system ("pause");
return 0;