课程设计要求的。。。
#include <stdio.h>
#include <alloc.h>
#include <string.h>
#include <math.h>
int pow2[15] = {
1, 2, 4, 8, 16, 32, 64,
128, 256, 512, 1024, 2048, 4096, 8192,
16384
};
int main (){
int n,r;
int cnt,flg;
int i,k,j;
char yn,ch;
char *HM_data;
char *HM_all;
char *HM_temp;
while (((ch != 'q') && (ch != 'Q'))){
///input method//
do{
printf("Input length of check(max 14):\n");
scanf("%d",&r);
printf("Length of check is %d\n",r);
printf("Is it right?(Y or N)\n");
getchar();
yn=getchar();
if (r<3){
printf("r must lager than 3!\n");
yn='\0';
}
} while (((yn!='y') && (yn != 'Y')));
n = pow2[r]-1-r;
HM_data=(char *)malloc(n+1);
HM_temp=(char *)malloc(pow2[r]);
HM_all=(char *)malloc(pow2[r]);
do{
printf("Input HM data:(out of %d will not be read)\n",n);
printf("e.g:10011001010010110(highest is in the left)\n");
for (i=0;i<n;i++){
*(HM_data+i)=getch();
printf("%c",*(HM_data+i));
if ((*(HM_data+i) != '1') && (*(HM_data+i) != '0')){
(*(HM_data+i))='0';
}
}
*(HM_data+n)='\0';
printf("\nyour input: HM_data:\n");
printf("%s\n",HM_data);
printf("Is it right?(Y or N)\n");
getchar();
yn=getchar();
} while (((yn!='y') && (yn != 'Y')));
/start HM//
k=r-1;
j=0;
for (i=0;i<(pow2[r]);i++){
if (i == pow2[r]-1-pow2[k]){
*(HM_temp+i)='1';
k=k-1;
}
else {
*(HM_temp+i)=*(HM_data+j);
j=j+1;
}
}
*(HM_temp+pow2[r]-1)='\0';
for (k=r-1;k>=0;k--){
j=0;flg=1;cnt=0;
for (i=0;i<(pow2[r]);i++){
if ((*(HM_temp+i) != '0') && (flg==1)){
cnt += 1;
}
j++;
if (j > (pow2[k]-1)){flg = 1-flg;j=0;}
}
if ((cnt%2)!=0){
*(HM_temp+pow2[r]-1-pow2[k])='0';
}
}
strcpy(HM_all,HM_temp);
*(HM_all+pow2[r]-1)='\0';
printf("HM_all is \n");
printf("%s\n",HM_all);
free(HM_data);HM_data=NULL;
free(HM_temp);HM_temp=NULL;
free(HM_all);HM_all=NULL;
printf("Continue? Or put Q to exit.");
ch = getch();
}
return 0;
}
转载请注明出处,禁止用于任何商业用途,否则后果自负!