2013-03-03 224 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