0

我需要帮助确定该 C 代码有什么问题,我想测试线性反馈移位寄存器 (LFSR) 的最大长度,所以首先我获取原始寄存器的副本然后我移动副本然后测试它与原来的相等,依此类推,直到得到最大长度。

下面是代码:

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>


void shiftLFSR (unsigned char* LFSR, unsigned char ntabs, unsigned char* tabs, unsigned char  length);
void test_the_equality (unsigned char* LFSR, unsigned char* original_LFSR, unsigned char length, unsigned char count, unsigned char main_flag);

int main(){


    unsigned char length;
    unsigned char *LFSR;
    unsigned char *original_LFSR;
    unsigned char i;
    unsigned char ntabs;
    unsigned char *tabs;
    unsigned char count;
    unsigned char main_flag;
    count = 1;
    main_flag = 1;
    printf("enter the length of the LFSR\n");
    scanf("%d", &length);
    printf("enter the number of Tabs\n");
    scanf("%d", &ntabs);
    LFSR = (unsigned char *) malloc(length * sizeof(unsigned char));
    original_LFSR = (unsigned char *) malloc(length * sizeof(unsigned char));   
    tabs = (unsigned char *) malloc(ntabs * sizeof(unsigned char));



    for (i = 0; i < ntabs; i++){
    printf("enter the position of Tab[%d]\n",i);
    scanf("%d", &tabs[i]);
    }

    original_LFSR[0] = 0;
    printf("LFSR[0] = %d\n",original_LFSR[0]);

    for (i = 1; i < length; i++){
        original_LFSR[i] =  original_LFSR[i-1] ^ 1;
            printf("LFSR[%d] = %d\n",i, original_LFSR[i]);
    }
    for ( i = 0; i < length; i++)
            LFSR [i] = original_LFSR[i];

    while (main_flag == 1){     
    shiftLFSR (LFSR, ntabs, tabs, length);
    test_the_equality(LFSR, original_LFSR, length, &count, &main_flag);         
    }

    printf("the length of period = %d", count);
    free (LFSR);
    return 0;
}





void shiftLFSR (unsigned char LFSR[], unsigned char ntabs, unsigned char tabs[], unsigned char  length){
    unsigned char feedback_value;
    unsigned char *Temp_LFSR;
    unsigned char i;

    feedback_value = 0;
    Temp_LFSR = (unsigned char *) malloc(length * sizeof(unsigned char));
    for (i = 0; i < ntabs; i++)
            feedback_value ^= LFSR[tabs[i]];

    for (i = 0; i < (length - 1); i++)
        Temp_LFSR [i+1] = LFSR[i];

    Temp_LFSR[0] = feedback_value;

    for ( i = 0; i < length; i++)
        LFSR [i] = Temp_LFSR[i];
    free (Temp_LFSR);
} 



void test_the_equality (unsigned char LFSR[], unsigned char original_LFSR[], unsigned char length, unsigned char *count, unsigned char *main_flag){
    unsigned char i;
    i = 0;
    while (i < length){

        if (LFSR[i] == original_LFSR[i])

            i++;
        else{
            *count = *count +1;
            printf("the length of the first period = %d", *count);
            break;

        }

    }
    if (i >= length - 1)
        *main_flag = 0;

}
4

0 回答 0