尝试从讲师编译程序时出错(定义行中需要的符号)

问题描述 投票:0回答:1

我从我的讲师那里得到了一个模板,然后将代码修改为给定文本的频率直方图。但是我在尝试编译代码时遇到错误。我相信编译器指出的错误位于代码的开头。错误已被截取并附于下方。提前致谢

错误:

test.c:6:25: error: expected declaration specifiers or '...' before '\x20'
 #define FIRST_PRINTABLE ' '  // Space character code 32, see Etter 2016 text, pp. 418-420
                         ^
test.c:8:30: note: in expansion of macro 'FIRST_PRINTABLE'
 #define NUM_PRINTABLE (int) (FIRST_PRINTABLE-LAST_PRINTABLE+1)
                              ^~~~~~~~~~~~~~~
test.c:11:38: note: in expansion of macro 'NUM_PRINTABLE'
 void init_array(int histogram[], int NUM_PRINTABLE);
                                      ^~~~~~~~~~~~~

码:

/*
 *  Comp120 - Lab 7:  Starter project -- Complete this code
 *  Character Frequency analysis -- read a text file and display frequency 
 *  analysis
 *     for all printable characters.
 *
 *  Author: J. Fall
 *  Date: Feb. 2017
 */

#include <stdlib.h>
#include <stdio.h> 
#include <stdbool.h>

// Definition of printable character set 
#define FIRST_PRINTABLE ' '  // Space character code 32, see Etter 2016 text, pp. 418-420
#define LAST_PRINTABLE '~'
#define NUM_PRINTABLE (int) (FIRST_PRINTABLE-LAST_PRINTABLE+1)

// Function prototypes:
void init_array(int histogram[], int NUM_PRINTABLE);
int sum_array(const int histogram[], int NUM_PRINTABLE);
bool isPrintable(char c);
void compute_frequency(char* filename, int histogram[], int NUM_PRINTABLE);
void write_histogram(int histogram[], int NUM_PRINTABLE);
FILE* openFileRead(char* filename);

int main( int argc, char* argv[] )
{
   if (argc < 2) {
      printf("Usage: freq inputFile \n");
      exit(-1);
   }

   int histogram[NUM_PRINTABLE];  // Array of counters -- one for each printible character

   compute_frequency(argv[1], histogram, NUM_PRINTABLE);

   write_histogram(histogram, NUM_PRINTABLE);

   printf( "Program complete. \n" );

   return 0                           ;
}

/*
 * Initialize the array of integers of given length to all zeros
 */
void init_array(int histogram[], int NUM_PRINTABLE) 
{
    int i = 0;
    for(i=0;i<NUM_PRINTABLE;i++){
        histogram[i]=0;
    }  // TODO: write function to assign 0 too every array element.
}

/*
 * Return the sum of all items in the given array
 */
int sum_array(const int array[], int NUM_PRINTABLE)
{
   int i = 0;
   int sum = 0;
   for(i=0;i<NUM_PRINTABLE;i++){
       sum = sum + histogram[i];
   }
   return sum; // TODO: write function to add up every element in the given array.
}

/*
 * Return true iff the character is PRINTABLE
 */
bool isPrintable(char c)
{
   if (c >= FIRST_PRINTABLE && <= LAST_PRINTABLE){
       return true;
   }
   else
       return false;  // TODO:  write function to return true iff c is a printable character
}

/*
 * Compute the frequency histogram for all PRINTABLE characters in the given file
 */
void compute_frequency(char* filename, int histogram[], int NUM_PRINTABLE)
{
   FILE* inputFile = openFileRead(filename);

   init_array(histogram, NUM_PRINTABLE);

   char c = getc(inputFile);  // priming read -- read first character from file
   while (c != EOF) {
      if(isPrintable(c)){
      int bin = c - FIRST_PRINTABLE;// TODO: write algorithm to count the number of times character c occurs.
      histogram[bin]++;
      }
      // HINT: since array indexes start at zero, map the ASCII code for each
      //       printable charcter onto an index by subtracting FIRST_PRINTABLE



      // After processing previous character, read next character from file to re-prime the loop
      c = getc(inputFile);   
   }
}

/*
 * Write the frequency histogram out to the given file
 */
void write_histogram(int histogram[], int NUM_PRINTABLE)
{
   FILE* outputFile = stdout;  // Simplifictaion:  output is written to the console instead of to an output file.

   int total_count = sum_array(histogram, NUM_PRINTABLE);
   fprintf(outputFile, "Frequency Analysis Results.  Input contained %d printable characters. \n", total_count);
   fprintf(outputFile, "Char | Frequency \n");
   fprintf(outputFile, "____ | _________ \n");
   int i;
   for (i=0; i<NUM_PRINTABLE; i++) {
      char ch = (char) (i + FIRST_PRINTABLE);
      double freq;
      if (histogram[i] > 0) {
         double freq = histogram[i]/(double)total_count * 100;
         fprintf(outputFile, "%3c  | %9.3f%% \n", ch, freq);
      }
      else
         fprintf(outputFile, "%3c  | %9d%% \n", ch, 0);
   }
}

/*
 * Attempt to open the file for read access.
 * Peforms error check and exits if file is not accessible
 */
FILE* openFileRead(char* filename)
{
   FILE* inFile = fopen(filename, "r" );
   if( inFile == NULL) {
      printf( "Error opening input file %s, program terminating!\n", filename);
      exit(-1);
   }
   return inFile;

}
c arrays compiler-errors
1个回答
1
投票
#define NUM_PRINTABLE (int) (FIRST_PRINTABLE-LAST_PRINTABLE+1)

// Function prototypes:
void init_array(int histogram[], int NUM_PRINTABLE);

当这两行都被预处理器扩展时,它会翻译为(你可以在源代码上使用gcc -E来查看它的运行情况):

void init_array(int histogram[], int (int) (' '-'~'+1));

这显然是语法错误。只需将NUM_PRINTABLE用作函数中的常量,而不是参数。

除此之外,宏应该是功能错误的,它应该是

#define NUM_PRINTABLE (LAST_PRINTABLE-FIRST_PRINTABLE+1)

或者价值是负面的。 (而且你不需要施放到int,因为字符文字已经是int

© www.soinside.com 2019 - 2024. All rights reserved.