c - 围绕变量 per/temp 进行堆栈

标签 c text-files

当我运行以下代码时,它卡在变量 per 周围,我不明白为什么。 该函数接收一个数据库(一个封闭的txt文件)、患者和一个指向大小的指针。

关于功能: 该函数返回一个人数组,其中包含潜在捐赠者的所有详细信息,且与患者至少有“min_match”个相同基因。

如果捐赠者和患者之间没有完全匹配,可以通过部分匹配进行移植。通过将 min_match 设置为 3,数组将包含具有 3 个或更多匹配基因的所有潜在供体作为患者。匹配基因是与患者的同一基因具有完全相同序列的基因。 数组排序如下;第一个标准:根据匹配基因的数量。 第二个标准:如果两个潜在供体之间的匹配基因数量相同,则优先考虑每个基因错配最少的供体(基因同一位置上的DNA序列差异)。 第三个标准:如果两个捐赠者具有完全相同的基因特征,则按字母顺序优先于电话簿中出现较早的捐赠者。

结构体和函数定义如下:

typedef struct person_{
    char name[31];
    char id[10];
    char genes[5][22];
} person;

person* getPotentialDonors(char* database, person patient, int min_match,int *size){
    FILE *db;
    person * matches,per,temp;
    int count,i,j=0,count_per=0,index,big=0,k,countS,countJ,MismatchS,MissmatchJ, minMissS, minMissJ,ind;

    db=fopen(database,"r");
    if(!db) exit(1);

    while(fscanf(db,"%30[^$] %9[^$] %21[^$] %21[^$] %21[^$] %21[^$] %21[^$]%*c",per.name,per.id,per.genes[0],per.genes[1],per.genes[2],per.genes[3],per.genes[4])==7)
    {
        count=0;
        for(i=0;i<5;i++)
            if(!strcmp(patient.genes[i],per.genes[0])||!strcmp(patient.genes[i],per.genes[1])||!strcmp(patient.genes[i],per.genes[2])||!strcmp(patient.genes[i],per.genes[3])||!strcmp(patient.genes[i],per.genes[4])) count++;

        if(count>=min_match) count_per++;
    }


    matches=(person*)malloc(sizeof(person)*count_per);

    rewind(db);

    while(fscanf(db,"%30[^$] %9[^$] %21[^$] %21[^$] %21[^$] %21[^$] %21[^$]%*c",per.name,per.id,per.genes[0],per.genes[1],per.genes[2],per.genes[3],per.genes[4])==7)
    {
        count=0;
        for(i=0;i<5;i++)
            if(!strcmp(patient.genes[i],per.genes[i])) count++;

        if(count>=min_match) 
        {
            strcpy(matches[j].name,per.name);
            strcpy(matches[j].id,per.id);
            strcpy(matches[j].genes[0],per.genes[0]);
            strcpy(matches[j].genes[1],per.genes[1]);
            strcpy(matches[j].genes[2],per.genes[2]);
            strcpy(matches[j].genes[3],per.genes[3]);
            strcpy(matches[j].genes[4],per.genes[4]);
            j++;
        }
    }

    for(index=0;index<count_per;index++){
        big = index;
        for(j=index+1;j<count_per;j++){
            MismatchS = 0;
            MissmatchJ = 0;
            minMissJ =minMissS =21;
            countS=countJ=0;
            for(k=0;k<5;k++){
                MismatchS = 0;
                if(!strcmp(matches[big].genes[k],patient.genes[k])) countS++;
                else
                {
                    for(i=0;i<22;i++){
                        if(matches[big].genes[k][i]!=patient.genes[k][i]) MismatchS++;
                    }
                    if(MismatchS<minMissS) minMissS=MismatchS; 
                }
                if(!strcmp(matches[j].genes[k],patient.genes[k])) countJ++;
                else
                {
                    for(i=0;i<22;i++){
                        if(matches[j].genes[k][i]!=patient.genes[k][i]) MissmatchJ++;
                    }
                    if(MissmatchJ<minMissJ) minMissJ=MissmatchJ; 
                }
            }
            if(countS < countJ)
                big = j;
            else if (countS == countJ &&  minMissJ<minMissS) big=j;
            else if (countS == countJ &&  minMissJ==minMissS && strcmp(matches[j].name,matches[big].name)<0)    big=j;
        }

        strcpy(temp.name,matches[index].name);//Index->temp
        strcpy(temp.id,matches[index].id);
        for(ind=0;ind<5;ind++)
            strcpy( temp.genes[ind],matches[index].genes[ind]);


        strcpy(matches[index].name,matches[big].name);//big->index
        strcpy(matches[index].id,matches[big].id);
        for(ind=0;ind<5;ind++)
            strcpy( matches[index].genes[ind],matches[big].genes[ind]);


        strcpy(matches[big].name,temp.name);//temp->big
        strcpy(matches[big].id,temp.id);
        for(ind=0;ind<5;ind++)
            strcpy(matches[big].genes[ind],temp.genes[ind]);
    }

    fclose(db);
    *size = count_per;

    return matches;
}

最佳答案

也许不是问题的答案,但我的对称检测器被混淆了

for(i=0;i<5;i++)
if(!strcmp(patient.genes[i],per.genes[0]) ||!strcmp(patient.genes[i],per.genes[1])||!strcmp(patient.genes[i],per.genes[2]) ||!strcmp(patient.genes[i],per.genes[3])||!strcmp(patient.genes[i],per.genes[4])) count++;
//do something with count

for(i=0;i<5;i++)
    if(!strcmp(patient.genes[i],per.genes[i])) count++;
// do something with count that is supposed to be the same as above

关于c - 围绕变量 per/temp 进行堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21060909/

相关文章:

c - 如何在C中读写tar文件?

c - 如何获取此代码中的 Days 值?

c - 在 VS .NET 2003 中识别线程

Java从带有撇号和连字符的文件中读取单词并将它们计为2个单词

IOS:从数组中将字符串写入文件txt

C 编程中二元运算符错误

c++ - 此代码如何在没有任何循环语句或 'goto' 或递归的情况下循环?

python - 使用列表和文本文件的字符串索引超出范围错误(使用Python)

C++删除txt文件中的最后一个字符

Java 从文本文件中提取值