gpt4 book ai didi

c - 删除C中数组中的垃圾值

转载 作者:行者123 更新时间:2023-11-30 18:31:28 25 4
gpt4 key购买 nike

我的 C 数组中有一个垃圾问题,我无法解决,我为此使用了 memset 函数,但这对我来说没有用。我怎么解决这个问题。如果我在代码块或其他 PC 中运行此代码,那么它不会完全运行。

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>

int main() {
clrscr();
int a, b, len = 0, x, i = 0, j, match, misMatch, gapPenalty, sim, m1, m2, m3;
char ch;
char *seq1 = (char *)malloc(100 * sizeof(char));
char *seq2 = (char *)malloc(100 * sizeof(char));
char *s1 = (char *)malloc(100 * sizeof(char));
char *s2 = (char *)malloc(100 * sizeof(char));

/*memset(seq1, 0, strlen(seq1) - 1);
memset(seq2, 0, strlen(seq2) - 1);
memset(s1, 0, strlen(s1) - 1);
memset(s2, 0, strlen(s2) - 1);*/

int **matrix;
int **back;

FILE *inputFile;
inputFile = fopen("in.txt", "r");
printf("Enter Match Point : ");
scanf("%d", &match);

printf("Enter Mismatch Point : ");
scanf("%d", &misMatch);

printf("Enter Gap Point : ");
scanf("%d", &gapPenalty);

while (fscanf(inputFile,"%s\n%s", seq1, seq2) != EOF);

a = strlen(seq1);
b = strlen(seq2);
for (j = 0; j <= strlen(seq2); j++) {
for (i = 0; i <= strlen(seq1); i++) {
if (i == 0 || j == 0) {
if (i == 0) {
matrix[j][i] = j * gapPenalty;
back[j][i] = 0;
}
if (j == 0) {
matrix[j][i] = i * gapPenalty;
back[j][i] = 0;
}
} else {
if (seq1[i - 1] == seq2[j - 1]) {
sim = match;
} else {
sim = misMatch;
}
m1 = matrix[j - 1][i - 1] + sim;
m2 = matrix[j - 1][i] + gapPenalty;
m3 = matrix[j][i - 1] + gapPenalty;
if (m1 > m2) {
if (m1 > m3) {
matrix[j][i] = m1;
back[j][i] = 1;
} else {
matrix[j][i] = m3;
back[j][i] = 3;
}
} else {
if (m2 > m3) {
matrix[j][i] = m2;
back[j][i] = 2;
} else {
matrix[j][i] = m3;
back[j][i] = 3;
}
}
}
}
}
printf("%s", seq1);
printf("\n");
printf("%s", seq2);
printf("\n");

if (a > b) {
len = a;
} else {
len = b;
}

for (x = 0; x < len; x++) {
if (back[b][a] == 1) {
s1[x] = seq1[a - 1];
s2[x] = seq2[b - 1];
a = a - 1;
b = b - 1;
} else if(back[b][a] == 2) {
s1[x] = seq1[a - 1];
s2[x] = '-';
a = a - 1;
} else {
s1[x] = '-';
s2[x] = seq2[b - 1];
b = b - 1;
}
}
for (j = 0; j <= strlen(seq2); j++) {
for (i = 0; i <= strlen(seq1); i++) {
printf("%d ", matrix[j][i]);
}
printf("\n");
}

printf("\n");
for (j = 0; j <= strlen(seq2); j++) {
for (i = 0; i <= strlen(seq1); i++) {
printf("%d ", back[j][i]);
}
printf("\n");
}

printf("\n");
printf("%s", s1);
printf("\n");
printf("%s", s2);
printf("\n");

free(s1);
free(s2);
free(matrix);
free(back);
getch();
return 0;
}

最佳答案

使用calloc()calloc() 将所有分配的内存初始化为 0

// sizeof (char), by definition, is 1
char *seq1 = calloc(100, 1);
char *seq2 = calloc(100, 1);
char *s1 = calloc(100, 1);
char *s2 = calloc(100, 1);
<小时/>

注释代码的直接问题是您无法将 strlen() 应用于未初始化的数组。您应该使用正确的大小(您之前刚刚使用过一些语句)

/*memset(seq1, 0, 100);
memset(seq2, 0, 100);
memset(s1, 0, 100);
memset(s2, 0, 100);*/

关于c - 删除C中数组中的垃圾值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23028087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com