2013-03-03 226 views
-1

这是我第一次使用stackoverflow,所以我希望我能做到这一点。我有一门课程遇到麻烦,我已经完成了另外两门课程。这是一个问题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"); 
    } 
} 

回答

4

不要把分号在#define语句。

3

因为#define预处理指令,它是这样工作的

#define foo bar取代的foo所有出现的bar前提是foo不进来引号(如"foo"printf功能不代替)。和一个换行字符分隔两个预处理指令,和不是一个分号。

所以很自然的分号也被取代,所以,您的数组声明现在变成

char first [241;];由于编译器看到的第一个分号它假定您已经通过不关闭的方括号犯了一个错误。

此错误不会声明您的first, second, third变量,因此会出现错误。

OUT_LENGTH相同

+0

很酷谢谢你!现在我只是遇到一种类型的错误,但它是相同的错误 – 2013-03-03 17:10:13

+0

dna.c:在函数'main'中: dna.c:25:warning:从不兼容的指针类型 dna中传递'compare_DNA'的参数3。 c:26:warning:从不兼容的指针类型中传递参数3'print_DNA' dna.c:29:warning:从不兼容的指针类型中传递'print_DNA'的参数3 dna.c:32:warning:来自不兼容指针类型的'compare_DNA' dna.c:33:warning:从不兼容指针类型传递'print_DNA'的参数3 Like-a-boss-5:assignment03 abbas $ – 2013-03-03 17:10:35

+0

'print_DNA'和'compare_dna '是'char'类型的,你传递一个'int'参数! 另外'fgets'被错误地调用。你需要一个文件指针(打开文件后) – cipher 2013-03-03 17:28:34