Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 程序卡在循环中_C - Fatal编程技术网

C 程序卡在循环中

C 程序卡在循环中,c,C,我正试图为我的实验室考试实现一个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; cha

我正试图为我的实验室考试实现一个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");
    }
}
它卡在上面的线上。但正确的程序在这里:

#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"); 
        }
#包括
#包括
int ctoi(字符a)
{ 
返回(a-48);
} 
字符itoc(INTA)
{ 
返回(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编码”);
printf(“\n输入数据字(消息)”);
扫描频率(“%s”,ip);
printf(“\n输入除数”);
scanf(“%s”,div);
strcpy(ipm,ip);
lm=strlen(ipm);
ld=strlen(div);
如果(lm>=ld)
{ 

对于(i=lm,j=0;j而言,问题源于代码中的一个小错误

    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()
不是罪魁祸首!

是的,但我找不到它。我永远不会称它为除数。你可以很好地称它为分母,或者只是反馈多项式。感谢@Roberto Reale的发现。我有一点怀疑-我在
strcpy()之后打印了一些字符串用于调试
函数,但它没有出现。你能告诉我原因吗?这是一个好的观点,并且与C库处理I/O的方式有关。默认情况下,
printf()
使用面向行的方法;也就是说,它实际上只在换行后打印。但是
printf(“CRC解码”)之间没有换行符
printf(“CRC余数是:%s\n”,CRC);
尝试在
printf(“strcpy”)之后添加
fflush(NULL);
就是在
printf(“strcpy”)之后;
换句话说,默认情况下
printf()
在找到新行(
\n
)之前不会打印任何内容。但是
printf(“strcpy”)中没有任何内容;
,因此输出必须等到下一行换行。但是,正如您所知,程序将陷入循环中。
    for (j = 0; crc[j]!='\0'; j++) {
        crc[j] = crc[j+1];
    }