C I';当我编译这个的时候,我遇到了一些奇怪的错误
这是我第一次使用stackoverflow,所以我希望我做得对。我的一门课的一个程序有点问题,我已经完成了另外两个。这是问题3,如果你向下滚动到问题3,将会有我用来完成这个程序的信息(但我必须说它写得很糟糕。)不管怎样,我在编译时遇到了这些非常奇怪的错误,有人知道我的代码不能编译/不能工作的原因吗?该程序的要点是读取一个样本dna输入,并将其与其他两个样本进行比较。我已经做了一个样本文件,就像上面说的那样。谢谢你抽出时间 我遇到了哪些错误:C I';当我编译这个的时候,我遇到了一些奇怪的错误,c,compiler-errors,C,Compiler Errors,这是我第一次使用stackoverflow,所以我希望我做得对。我的一门课的一个程序有点问题,我已经完成了另外两个。这是问题3,如果你向下滚动到问题3,将会有我用来完成这个程序的信息(但我必须说它写得很糟糕。)不管怎样,我在编译时遇到了这些非常奇怪的错误,有人知道我的代码不能编译/不能工作的原因吗?该程序的要点是读取一个样本dna输入,并将其与其他两个样本进行比较。我已经做了一个样本文件,就像上面说的那样。谢谢你抽出时间 我遇到了哪些错误: dna.c: In function ‘ma
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");
}
}
#包括
#定义最大长度241;
#定义出长度60;
文件*输入;
文件*输出;
int-read_-DNA(字符序列[]);
双重比较DNA(字符序列1[],字符序列2[],字符序列3[],int n);
无效打印DNA(字符序列1[],字符序列2[],字符序列3[],内部n);
int main()
{
char first[最大长度];
字符秒[最大长度];
第三字符[最大长度];
int last[最大长度];
int i;
整数长度;
百分之二;
输入=fopen(“dna_input.dat”,“r”);
输出=fopen(“dna_output.dat”,“w”);
长度=读取DNA(第一个);
长度=读取DNA(秒);
长度=读取DNA(第三个);
fprintf(输出,“序列1和序列2之间的比较:\n”);
百分比=比较DNA(第一个、第二个、最后一个、长度);
打印DNA(第一、第二、最后、长度);
fprintf(输出,“重叠百分比为%.1f%%\n”,百分比*100);
fprintf(输出,“序列1和序列3之间的比较:\n”);
打印DNA(第一个、第三个、最后一个、长度);
fprintf(输出,“重叠百分比为%.1f%%\n”,百分比*100);
fprintf(输出,“序列2和序列3之间的比较:\n”);
百分比=比较DNA(第二、第三、最后、长度);
打印DNA(第二、第三、最后、长度);
fprintf(输出,“重叠百分比为%.1f%%\n”,百分比*100);
fclose(输入);
fclose(输出);
返回0;
}
int read_DNA(字符序列[])
{
fgets(序列、最大长度、输入);
int i;
对于(i=0;i不要在#define
语句中加分号。因为#define
是一个预处理器指令,它是这样工作的
#define foo bar
将所有出现的foo
替换为bar
,前提是foo
不带引号(如printf
等函数中的“foo”
不被替换).和一个换行符分隔两个预处理器指令,非分隔分号
因此,分号自然也被替换,因此,数组声明现在变为
char first[241;];
由于编译器首先看到分号,因此它假定您未关闭方括号而出错
此错误不会声明您的第一、第二、第三个变量,因此会出现错误
同样的情况也发生在OUT_LENGTH
很酷,谢谢!现在我只得到了一种错误类型,但它是相同的错误dna。c:在函数'main':dna中。c:25:警告:从不兼容的指针类型dna传递'compare_dna'的参数3。c:26:警告:从不兼容的指针类型dna传递'print_dna'的参数3。c:29:警告:从不兼容指针类型DNA传递“print_DNA”的参数3。c:32:警告:从不兼容指针类型DNA传递“compare_DNA”的参数3。c:33:警告:从不兼容指针类型传递“print_DNA”的参数3,如-a-boss-5:assignment03 abbas$在print_DNA
和com中的第三个参数pare_dna
的类型为char
,您正在传递int
参数!!而且fgets
的调用不正确。您需要一个文件指针(打开文件后)