gpt4 book ai didi

c - 尝试编译讲师的程序时出错(定义行中预期的符号)

转载 作者:行者123 更新时间:2023-11-30 14:50:14 25 4
gpt4 key购买 nike

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

错误:

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;

}

最佳答案

#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,因为字 rune 字已经是 int)

关于c - 尝试编译讲师的程序时出错(定义行中预期的符号),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49092803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com