あなたが私を助けてくれることを願っています。私はすべてを書き出してコンパイルしましたが、私が望む答えが得られません。
ユーザーが1と0のコードを入力すると、プログラムは正しいシーケンスを見つけます...
ユーザーが 1000110 を入力すると、エラー ビットは 6 になり、修正されたコードは 1100110 になります。
私はもともとcharを使用していましたが、最後に1と0ではなくランダムなものを出力し続けました。
誰でも私を助けることができますか?
必要に応じてさらに説明します...
*編集元のコードを追加しました。int を試してみましたが、うまくいきませんでした。以下のこのコードでは、ユーザーが入力した 1 と 0 が配列に配置されていないか、正しく印刷されていないようです...どうすればよいかわかりません。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
char *code = NULL;
int length, parity,slength;
void param(){
printf("Enter the maximum length:");
scanf("%d",&length);
printf("Enter the parity (0=even, 1=odd):");
scanf("%d",&parity);
code = (char*)malloc(length *sizeof(char));
return;
}
void check(){
int error,p,q=0,j,k,l,u=0,b=0,h,total;
char *hCode = NULL;
char *fCode = NULL;
hCode = (char*)malloc(length *sizeof(char));
fCode = (char*)malloc(length *sizeof(char));
printf("Enter the Hamming code:");
scanf("%lengths",&code);
slength = strlen(code);
// printf("%d",&slength);
// int z;
// for(z=0;z<slength;z++){
// printf("%s\n",&code[z]);
// }
for (p=slength+1;p>=0;p--){
hCode[p] = code[q];
q++;
}
for (j=1;j<=log2(length);j*=2){
int bit;
for (k=j;k<=length;k+=j*2)
for (l=0;l<j||k+l<=length;l++){
if(k+1!=k){
bit^=hCode[k+l];
fCode[k+1] = hCode[k+1];
}
fCode[j] = bit;
}
}
for (u=1;u<=slength+1;u++){
if (hCode[u]!=fCode[u]){
error = u;
b=u;
printf("There is an error in bit: %d\n",error);
}
}
if (b==0)
printf("There is no bit error\n");
b=0;
printf("The corrected Hamming code is: ");
for (h=slength;h>0;h--){
printf("%c",&fCode[h]);
}
printf("\n");
return;
}
int main(){
int choice;
while (choice !=3){
printf("\n");
printf(" Error detection/correction:\n");
printf("------------------------------\n");
printf("1) Enter parameters\n");
printf("2) Check Hamming code\n");
printf("3) Exit\n");
printf("\nEnter selection: ");
scanf("%d",&choice);
printf("\n");
if (choice == 1)
param();
else if (choice == 2)
check();
else{
printf("\n");
printf("Goodbye. Have a nice day");
}
}
return 1;
}