gpt4 book ai didi

c - 字数统计问题

转载 作者:行者123 更新时间:2023-11-30 16:30:54 24 4
gpt4 key购买 nike

我正在尝试完成我的 C 作业问题之一。以下是定义和示例 IO:

描述

给定一篇文章作为输入。您必须计算每个单词的数量,并按字母顺序打印单词列表。

示例输入

It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us, we were all going direct to Heaven, we were all going direct the other way.

示例输出

age 2
all 2
before 2
belief 1
best 1
darkness 1
despair 1
direct 2
epoch 2
everything 1
foolishness 1
going 2
had 2
heaven 1
hope 1
incredulity 1
it 10
light 1
nothing 1
of 10
other 1
season 2
spring 1
the 11
times 2
to 1
us 2
was 10
way 1
we 4
were 2
winter 1
wisdom 1
worst 1

这是我现在的代码:

在main.c中:

#include <stdio.h>
#include <stdlib.h>
#include "function.h"
#include <string.h>

int main()
{
char wordcollected [3100] = {0};
char *word_ptr[100];
int countarray[100];
static char temp[31];
int nth_word = 0;
while(1){
int n = strlen(wordcollected);
word_ptr [nth_word] = wordcollected + strlen(wordcollected);
if(strcpy(temp, fetch_word()) == NULL){
for(n == strlen(wordcollected); n >= 0; n--){
if(wordcollected[n] == ','){
wordcollected[n] = '\0';
}
}
break;
}
strcat((wordcollected), temp);
strcat((wordcollected), ",");
nth_word ++;
}

}

我们的助教已经为我们完成了部分代码:

在函数.c中:

#include "function.h"
#include <stdio.h>

// fetch words from stdin
const char *fetch_word(){
static char skip_symbol[] = " \t\n,.;:?()[]{}\"\"''" ;

static char line_buffer[1024] ;
static char *now = NULL ;

// try to read a line from stdin
for( ;; ){
if( now == NULL)
now = fgets( line_buffer, sizeof(line_buffer), stdin ) ;

// End Of File?
if( now == NULL )
return NULL ;

// skip symbols
for( ; *now ; now++ ){
int size = sizeof( skip_symbol ) ;
int i ;
for( i=0 ; i<size ; i++ ){
if( *now == skip_symbol[i] )
break ;
}

// if not match skip_symbol[]
if( i >= size )
break ;
}

// End Of Line?
if( *now == '\0' ){
now = NULL ;
continue ;
}

char *word = now ;

for( ; *now ; now++ ){
int size = sizeof( skip_symbol ) ;
int i ;
for( i=0 ; i<size ; i++ ){
if( *now == skip_symbol[i] )
break ;
}

// if match skip_symbol[]
if( i < size )
break ;
}

if( *now ){
*now = '\0' ;
now ++ ;
}

return word ;
}

return NULL ;
}

在函数.h中:

#ifndef __FUNCTION_H__
#define __FUNCTION_H__

// fetch words from stdin
const char *fetch_word() ;

#endif

函数 *fetch_word() 在运行时将返回指向 stdin 中每个单词的指针,如果函数已经到达文件结尾,还将返回 NULL。但每次到达 EOF 时,它只是一直说段错误并且系统停止。如何检测 fetch_word() 的返回值,知道何时到达文件结尾,并防止丢失任何单词?

最佳答案

在进行循环之前,您需要在结束标志处中断:

    if(strcpy(temp, fetch_word()) == NULL){
break;
for(n == strlen(wordcollected); n >= 0; n--){
if(wordcollected[n] == ','){
wordcollected[n] = '\0';
}
}
}

但是请采纳我的建议并重构您的代码并使其更具可读性。这样你会节省很多时间。

此外,从算法上来说,对我来说,您似乎可能想要创建一个单词的链接列表,与一个数字配对(实现链接列表数据结构来实现这一点),每当您阅读一个单词时,尝试在其中找到它链接列表,直到到达低于按字母顺序排列的单词或列表末尾或匹配的内容。如果找到匹配项,请在数字上加 1。否则在其相应位置插入值为 1 的单词。

关于c - 字数统计问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50864455/

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