gpt4 book ai didi

c - 当我编译这个时,我遇到了一些奇怪的错误

转载 作者:行者123 更新时间:2023-11-30 18:20:40 24 4
gpt4 key购买 nike

这是我第一次使用 stackoverflow,所以我希望我做得对。我的一门类(class)中的 1 个程序遇到了一些问题,我已经完成了另外两个程序。这是问题3,如果你 go to this pdf向下滚动到问题 3,将会有我用来完成这个程序的信息(但我必须说它写得很糟糕。)无论如何,我在编译时遇到了这些非常奇怪的错误,有人有任何原因吗?为什么我的代码无法编译/无法工作?该程序的重​​点是读取 DNA 输入样本并将其与其他 2 个样本进行比较。我已经制作了一个示例文件,就像它所说的 in the instructions 一样。 。感谢您的时间!

我遇到了什么错误:

     dna.c: In function ‘main’:
dna.c:12: error: expected ‘]’ before ‘;’ token
dna.c:13: error: expected ‘]’ before ‘;’ token
dna.c:14: error: expected ‘]’ before ‘;’ token
dna.c:15: error: expected ‘]’ before ‘;’ token
dna.c:21: error: ‘first’ undeclared (first use in this function)
dna.c:21: error: (Each undeclared identifier is reported only once
dna.c:21: error: for each function it appears in.)
dna.c:22: error: ‘second’ undeclared (first use in this function)
dna.c:23: error: ‘third’ undeclared (first use in this function)
dna.c:25: error: ‘last’ undeclared (first use in this function)
dna.c: In function ‘read_DNA’:
dna.c:43: error: expected ‘)’ before ‘;’ token
dna.c:43: error: too few arguments to function ‘fgets’
dna.c:45: error: expected expression before ‘;’ token
dna.c: In function ‘print_DNA’:
dna.c:76: error: expected ‘)’ before ‘;’ token

这是我的代码

#include <stdio.h>
#define MAX_IN_LENGTH 241;
#define OUT_LENGTH 60;
FILE *input;
FILE *output;
int read_DNA(char sequence[]);
double compare_DNA(char seq1[], char seq2[], char seq3[], int n);
void print_DNA(char seq1[], char seq2[], char seq3[], int n);

int main()
{
char first[MAX_IN_LENGTH];
char second[MAX_IN_LENGTH];
char third[MAX_IN_LENGTH];
int last[MAX_IN_LENGTH];
int i;
int length;
double percent;
input = fopen("dna_input.dat", "r");
output = fopen("dna_output.dat", "w");
length =read_DNA(first);
length =read_DNA(second);
length =read_DNA(third);
fprintf(output,"Comparison between sequence # 1 and sequence #2:\n");
percent=compare_DNA(first, second, last,length);
print_DNA(first, second, last, length);
fprintf(output,"The overlap percentage is %.1f%%\n",percent*100);
fprintf(output,"Comparison between sequence # 1 and sequence #3:\n");
print_DNA(first, third, last,length);
fprintf(output,"The overlap percentage is %.1f%%\n",percent*100);
fprintf(output,"Comparison between sequence # 2 and sequence #3:\n");
percent=compare_DNA(second,third,last,length);
print_DNA(second, third, last, length);
fprintf(output,"The overlap percentage is %.1f%%\n",percent*100);
fclose(input);
fclose(output);
return 0;
}


int read_DNA(char sequence[])
{
fgets(sequence, MAX_IN_LENGTH, input);
int i;
for(i=0; i<MAX_IN_LENGTH; i++)
{
if(sequence[i]=='\0')
return i;

}
return MAX_IN_LENGTH;
}

double compare_DNA(char seq1[], char seq2[], char seq3[], int n)
{
int i,count=0;
for(i=0;i<n-1;i++)
{
if(seq1[i]==seq2[i])
{
seq3[i]=seq1[i];
count++;
}
else
seq3[i]=' ';
}
return (double)count/(n-1);
}


void print_DNA(char seq1[], char seq2[], char seq3[], int n)
{
int i=0;
int lines, j, start, stop;
lines =n/OUT_LENGTH;
if( n%OUT_LENGTH!=0)
lines++;
for(j=0;j<lines;j++)
{
start=j*OUT_LENGTH;
stop=(j+1)*OUT_LENGTH;
if(stop>=n)
stop=n-1;
for(i=start;i<stop;i++)
fprintf(output,"%c",seq1[i]);
fprintf(output,"\n");
for(i=start;i<stop;i++)
{
fprintf(output,"%c",seq3[i]);
if(seq3[i]==' ')
fprintf(output,"");
}
fprintf(output,"\n");
for(i=start;i<stop;i++)
{
fprintf(output,"%c",seq2[i]);}
fprintf(output,"\n");
}
}

最佳答案

因为#define是一个预处理器指令,它的工作原理如下

#define foo bar 将所有出现的 foo 替换为 bar 前提是 foo 没有出现引号中(例如 printf 等函数中的 "foo" 不会被替换)。 换行字符分隔两个预处理器指令,NOT分号。

所以很自然地你的分号也被替换了,所以,你的数组声明现在变成了

char first [241;]; 由于编译器首先看到分号,因此它假定您因未关闭方括号而犯了错误。

此错误未声明您的第一、第二、第三变量,因此错误随之而来。

OUT_LENGTH 的情况也是如此

关于c - 当我编译这个时,我遇到了一些奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15182013/

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