【问题标题】:print the sentences of a file one after another一个接一个地打印文件的句子
【发布时间】:2016-12-03 06:26:04
【问题描述】:

我有一个包含 4 个段落的文本文件 (input.txt),我必须将这些段落的句子一个接一个地写在另一个文件 (output.txt) 中(用换行符分隔)。

原文件中的句子用'.'、'!'分隔和'?'。

我已经完成了,但我的代码有问题。 output.txt文件中有些句子没有换行

我的代码:

while(1) {
    c = fgetc(fp);
    if( feof(fp) ) {
      break;
    }

    c_next = fgetc(fp);

    if( feof(fp) ) {
      fprintf(fp_output, "%c", c);
      break;
    } else {
      if( c=='.' || c=='?' || c=='!' ) {
        fprintf(fp_output, "%c\n", c);
      } else {
        if( c=='\n' ) {
          fprintf(fp_output, "%c", c_next);
        } else if( c_next=='\n' ) {
          fprintf(fp_output, "%c ", c);
        } else {
          fprintf(fp_output, "%c%c", c, c_next);
        }

      }
    }

  }

例如,对于输入文件:

This is the first sentence. The second one contains some more words, other words,
more words, etc. The third sentence has; and more like: this, that, those.

This is the second paragraph. And now a question? Only an exclamative
sentence is missing!

This is the third paragraph. Another component - word - would be this.
The final sentence of the paragraph!

This is the last paragraph.

我的代码得到以下输出:

This is the first sentence.
The second one contains some more words, other words, more words, etc. The third sentence has; and more like: this, that, those.
This is the second paragraph.
And now a question?
Only an exclamative sentence is missing!
This is the third paragraph. Another component - word - would be this.The final sentence of the paragraph!
This is the last paragraph.

问题出在第二行和第六行。每行最多1句。

欢迎任何想法、提示或解决方案。

谢谢

【问题讨论】:

  • 考虑如果c_next 是句号('.'),您的代码会做什么。调试此类问题的方法是使用调试器逐行遍历代码。您对cc_next 的使用只会导致混淆。我建议你可以不用c_next
  • 没有 c_next 怎么办?我如何知道新句子何时开始? @kaylum 或者只是检查字符是否为大写?
  • 好吧,如果有一个句号,那就告诉你句子已经结束了。为什么你需要句号后的下一个字符才能知道句子是否在那里结束?下一个字符无关紧要。句号本身就足够了。
  • 您应该查看proper C formatting。或了解如何thoroughly obfuscate your code
  • @kaylum 我试着按照你告诉我的方式去做,但我做不到。如果您能向我展示代码,我将不胜感激。谢谢

标签: c


【解决方案1】:

以下代码:

  1. 仅经过轻微测试
  2. 干净编译
  3. 忽略多个句尾标记,即使由空格分隔
  4. 忽略换行序列
  5. 忽略句子前的空格

现在,代码

#include <stdio.h>  // fopen(), fclose(), fgetc(), putchar()
#include <ctype.h>  // isalpha()
#include <stdlib.h> // exit(), EXIT_FAILURE

#define PERIOD (',')
#define QUESTION_MARK ('?')
#define EXCLAMATION_MARK ('!')

int main( int argc, char *argv[] )
{
    if( 2 > argc )
    {
        fprintf( stderr, "USAGE: %s <inputFileName>\n", argv[0] );
        exit( EXIT_FAILURE );
    }

    // implied else, command line parameter exists

    FILE *fp = NULL;
    if( NULL == (fp = fopen( argv[1], "r" ) ) )
    {
        perror( "fopen failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, fopen successful

    int inSentence = 0;
    int ch;
    while( EOF != (ch = fgetc( fp ) ) )
    {
        if( inSentence )
        {
            putchar( ch );
            if( PERIOD == ch || EXCLAMATION_MARK == ch || QUESTION_MARK == ch)
            {
                inSentence = 0;
                putchar( '\n' );
            }
        }

        else // if( !inSentence )
        {
            if( isalpha( ch ) )
            { // then not white space nor more punctuation
                inSentence = 1;
                putchar( ch );
            }
        }
    } // end while

    // cleanup
    if( inSentence )
    {
        putchar( '\n' );
    }

    fclose( fp );
} // end function: main

【讨论】:

    【解决方案2】:

    您的代码每次读取 2 个字符,但只检查第一个字符。所以如果 c_next 是 ".!?",它就会失败。

    已更新。参见“if(c=='\n')”部分。我还不能发表评论。

    #include <stdio.h>
    
    int main(){
    
    FILE* fp, *fo;
    int c, flag = -1;
    fp = fopen("input.txt", "r");
    fo = fopen("output.txt", "w");
    while(fp != NULL){
      c = fgetc(fp);
      if(feof(fp)) break;
    
      if(c=='\n'){
        fprintf(fo, " ");
        continue;
      }
    
      if(flag != '.' && flag != '!' && flag != '?'){
        fprintf(fo, "%c", c);
      }
      else{
        fprintf(fo, "\n");
        if(c != ' '){
          fprintf(fo, "%c", c);
        }
      }
      flag = c;
    }
    fclose(fp);
    fclose(fo);
    
    return 0;
    }
    

    【讨论】:

    • 这个工作正常,但它有一个问题,当输入文件中的一个句子被分成两行,然后当两行在 output.txt 文件中连接在一起时,没有空格2字开头的结尾之间。如:这是第二段。现在有一个问题?只少了一个感叹句!加入后变成:只少了一个感叹句! (感叹句之间没有空格)
    猜你喜欢
    • 2016-06-13
    • 2022-09-30
    • 1970-01-01
    • 2017-01-10
    • 1970-01-01
    • 1970-01-01
    • 2021-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多