-1

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

我得到了什么错误:

     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

2 回答 2

3

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

#define foo bar替换所有出现的foobar前提是foo不带引号(就像"foo"在函数中一样printf不被替换)。换行符分隔两个预处理器指令,而不是分号

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

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

此错误未声明您的first, second, third变量,因此错误随之而来。

情况也是如此OUT_LENGTH

于 2013-03-03T05:47:08.957 回答
2

不要在#define语句中使用分号。

于 2013-03-03T03:12:21.323 回答