代码现在正式工作了,现在我可以使数组word动态更改,或者使我的程序递归!

#define N 200 //Max number of words
#define M 20 //Max length of a word

struct Word_setup {
    char word[M];
    int count;
} phrase[N]; //Sets the two sub memebers of each array slot

int repeat_word(int marker);
void add_count(int marker);
void highest();
int check_file(FILE *fp, int marker);

int repeat_marker, final_marker;

int main(void)
{
    int i, j, marker;
    FILE *fp;

    fp = fopen("C:\\Users\\Nicolas\\Desktop\\UF\\Semester 2\\COP 3275\\Projects\\Project 4\\input.txt", "r"); //Edit to the location of the file

    marker = 0;
    check_file(fp, marker); //check file to see if its reached the end, also works to create a word!
    add_count(marker);

    for (i = 1; i < N; i++)
    {
        marker = i;

        check_file(fp, marker);

        if (repeat_word(marker))
            add_count(repeat_marker);
        else if (check_file(fp, marker))
            break;
        printf("Test\n");
    }

    fclose(fp);
    highest();

    printf("The word with the highest number of appearances is: %s\n", phrase[final_marker].word);
    printf("Number of appearances: %d", phrase[final_marker].count);
    return 0;
}


char create_word(FILE *fp, int marker);
void clean_word(int marker);

int check_file(FILE *fp, int marker)
{
    if (create_word(fp, marker) == EOF)
        return 1;
    else
        clean_word(marker);
    return 0;
}


char create_word(FILE *fp, int marker) //scans the file, and creates the member array
{
    char ch;

    ch = fscanf(fp, " %s", phrase[marker].word);
    printf("New word"); //Error checking
    return ch;
}


void clean_word(int marker) //Gets rid of any characters that arent either in the alphabet, or an apostrophe
{
    int i;

    for (i = 0; i < M; i++)
    {
        if (phrase[marker].word[i] == 39 || isalpha(phrase[marker].word[i])); //39 is the ASCII value for: '
        else
            phrase[marker].word[i] = '\0';
    }
    printf("Clean: "); //Error checking
    printf("%s\n", phrase[marker].word); //Error checking
}

int repeat_word(int marker)
{
    int i;

    for (i = 0; i < N; i++)
    {
        repeat_marker = i;
        if (strcmp(phrase[marker].word, phrase[i].word) == 0)
            return 1;
    }

    return 0;
}


void add_count(int marker) //Adds a count to the current word if it already exists, obviously not needed, but I though it'd be easier to understand
{
    phrase[marker].count++;
}


void highest() //Determines the max number of appearances, as well as giving the slot in the array which we'll need to call (marker)
{
    int i, max;

    for (i = 0, max = 0; i < N; i++)
    {
        if (phrase[i].count > max)
        {
            final_marker = i;
            max = phrase[i].count;
        }
    }
}

最佳答案

把这条线[cc]改为add_count;就行了。
代码如下:

#define N 200 //Max number of different types of words
#define M 20 //Max length of a word

struct Word_setup {
    char word[M];
    int count;
} phrase[N]; //Sets the two sub memebers of each array slot

int counter;

void create_first_word(FILE *fp);
int repeat_word();
void add_count();
void highest();
int check_file(FILE *fp);


int main(void)
{
    int i, j;
    FILE *fp;

    fp = fopen("C:\\Users\\Nicolas\\Desktop\\UF\\Semester 2\\COP 3275\\Projects\\Project 4\\input.txt", "r"); //Edit to the location of the file

    counter = 0;
    check_file(fp); //check file to see if its reached the end, also works to create a word!
    add_count;

    for (i = 1; i < N; i++)
    {
        counter = i;

        if (repeat_word())
            add_count();
        else if (check_file(fp))
            break;
    }

    fclose(fp);
    highest();

    printf("The word with the highest number of appearances is: %s\n", phrase[counter].word);
    printf("Number of appearances: %d", phrase[counter].count);
    return 0;
}


char create_word(FILE *fp);
void clean_word();

int check_file(FILE *fp)
{
    if (create_word(fp) == EOF)
        return 1;
    else
        clean_word();
    return 0;
}


char create_word(FILE *fp) //scans the file, and creates the member array
{
    char ch;

    ch = fscanf(fp, " %s", phrase[counter].word);

    return ch;
}


void clean_word() //Gets rid of any characters that arent either in the alphabet, or an apostrophe
{
    int i;

    for (i = 0; i < M; i++)
    {
        if (phrase[counter].word[i] == 39 || isalpha(phrase[counter].word[i])); //39 is the ASCII value for: '
        else
            phrase[counter].word[i] = '\0';
    }
    printf("%s", phrase[counter].word);
}


int repeat_word()
{
    int i;

    for (i = 0; i < N; i++)
    {
        if (strcmp(phrase[counter].word, phrase[i].word) == 0)
            return 1;
    }

    return 0;
}


void add_count() //Adds a count to the current word if it already exists, obviously not needed, but I though it'd be easier to understand
{
    phrase[counter].count++;
}


void highest() //Determines the max number of appearances, as well as giving the slot in the array which we'll need to call (counter)
{
    int i, max;

    for (i = 0, max = 0; i < N; i++)
    {
        if (phrase[i].count > max)
        {
            counter = i;
            max = phrase[i].count;
        }
    }
}

关于c - 输出错误[C],我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29737893/

10-12 16:26