我正在尝试为我的实验室考试实施CRC的C程序。我已经写了以下代码。但是,它与主要源代码相同,但它被卡在strcpy()中。我无法弄明白。这是源代码:
#include <stdio.h>
#include <string.h>
int ctoi(char a) {
return (a-48);
}
char itoc(int a) {
return (a+48);
}
void main() {
int ld, lm, i, j;
char div[20], ip[100], ipm[100], crc[10], snd[100]={0}, rcv[100];
printf("\t\t\tCRC Encoding\n");
printf("Enter the codeword: ");
scanf("%s",ip);
printf("Enter the divisor: ");
scanf("%s",div);
strcpy(ipm, ip);
lm = strlen(ipm);
ld = strlen(div);
if(lm>=ld) {
//padding
for(i=lm, j=0; j<ld-1; j++)
ipm[i++] = '0';
ipm[i] = '\0';
printf("Data word after appending zeroes: %s\n", ipm);
for(i=0;i<ld;i++)
crc[i] = ipm[i];
crc[i] = '\0';
for(;i<strlen(ipm);i++) {
if(crc[0] == '1') {
for(j=0; j<ld; j++)
crc[j] = itoc((ctoi(crc[j])) ^ (ctoi(div[j])));
}
crc[ld] = ipm[i];
crc[ld+1] = '\0';
for(j=0;crc[j]!='\0';j++)
crc[j] = crc[j+1];
crc[j] = '\0';
}
for(j=0;crc[j]!='\0';j++)
crc[j] = crc[j+1];
crc[j] = '\0';
printf("CRC remainder is: %s\n",crc);
strcat(snd, ip);
strcat(snd, crc);
printf("Sent codeword: %s\n",snd);
printf("CRC Decoding\n");
strcpy(rcv, snd);
printf("after strcpy");
printf("Received codeword: %s", rcv);
for(i=0;i<ld;i++) {
crc[i] = rcv[i];
}
crc[i] = '\0';
for( ;i<strlen(rcv);i++) {
if(crc[0]=='1') {
for(j=0;j<ld;j++) {
crc[j] = itoc((ctoi(crc[j])) ^ (ctoi(div[j])));
}
}
crc[ld] = rcv[i];
crc[ld+1] = '\0';
for (j = 0; crc[j]!='\0'; i++) {
crc[j] = crc[j+1];
}
crc[j] = '\0';
for(j=0; crc[j]!='\0'; j++) {
if(crc[j]!='0')
break;
}
printf("CRC remainder is: %s\n",crc);
if(j==strlen(crc)) {
printf("Received message is error free\n");
} else {
printf("Received message contains errors\n");
}
}
} else {
printf("Enter a proper divisor");
}
}
输出屏幕为:
CRC Encoding
Enter the codeword: 10110
Enter the divisor: 110
Data word after appending zeroes: 1011000
CRC remainder is: 00
Sent codeword: 1011000
CRC Decoding
它停留在上面一行。但正确的计划在这里:
#include<stdio.h>
#include<string.h>
int ctoi(char a)
{
return(a-48);
}
char itoc(int a)
{
return(a+48);
}
void main()
{
char ip[100],ipm[100],div[20],crc[10],sent[100]={0},rec[100];
int i,lm,ld,j;
printf("\nCRC encoding");
printf("\nEnter data word(message)");
scanf("%s",ip);
printf("\nEnter the divisor");
scanf("%s",div);
strcpy(ipm,ip);
lm=strlen(ipm);
ld=strlen(div);
if(lm>=ld)
{
for(i=lm,j=0;j<ld-1;j++)
ipm[i++]='0';
ipm[i]='\0';
printf("\nData word after appending zeros:%s",ipm);
for(i=0;i<ld;i++)
crc[i]=ipm[i];
crc[i]='\0';
for(;i<strlen(ipm);i++)
{
if(crc[0]=='1')
{ for(j=0;j<ld;j++)
{
crc[j]=itoc((ctoi(crc[j]))^(ctoi(div[j])));
}
}
crc[ld]=ipm[i];
crc[ld+1]='\0';
for(j=0;crc[j]!='\0';j++)
crc[j]=crc[j+1];
crc[j]='\0';
}
for(j=0;crc[j]!='\0';j++)
crc[j]=crc[j+1];
crc[j]='\0';
printf("\nCRC remainder is:%s",crc);
strcat(sent,ip);
strcat(sent,crc);
printf("\nCode word in sender side is:%s",sent);
printf("\nCRC decoing");
strcpy(rec,sent);
//rec[2]='1';
printf("\nReceived message in receiver side is :%s",rec);
for(i=0;i<ld;i++)
crc[i]=rec[i];
crc[i]='\0';
for(;i<strlen(rec);i++)
{
if(crc[0]=='1')
{
for(j=0;j<ld;j++)
{
crc[j]=itoc((ctoi(crc[j]))^(ctoi(div[j])));
}
}crc[ld]=rec[i];
crc[ld+1]='\0';
for(j=0;crc[j]!='\0';j++)
crc[j]=crc[j+1];
crc[j]='\0'; }
for(j=0;crc[j+1]!='\0';j++)
crc[j]=crc[j+1];
crc[j]='\0';
for(j=0;crc[j]!='\0';j++)
{
if(crc[j]!='0')
break;
}
printf("\nCRC remainder is:%s",crc);
if(j==strlen(crc))
printf("\nReceived message is error free!\n\n");
else
printf("\nError in received message!!!\n\n");
}
else
printf("\nEnter proper divisor");
}
它运行完美。即使我逐行完成,我也无法找到这两个程序之间的区别。你能告诉我第一个程序有什么问题吗?
答案 0 :(得分:4)
问题源于代码中的小错字。你写了
for (j = 0; crc[j]!='\0'; i++) {
crc[j] = crc[j+1];
}
但应该是
for (j = 0; crc[j]!='\0'; j++) {
crc[j] = crc[j+1];
}
注意j++
。当然,对strcpy()
的呼吁不是罪魁祸首!