今天上午对昨晚的原版本的改进。主要是改进了查找分段标志的算法,使分段逻辑显得更合理。
- /*
- Name: 小说自动分段器
- Copyright: Copyright ? 2011 Geek_Soledad All Rights Reserved
- Author: Geek_Soledad
- Date: 01-04-11 22:42
- Description: 对网络上一些没有分段的小说进行自动分段,
- 条件是原文中每段开头均有四个空格以上
- */
- /*
- 版本:0.1.1
- 功能更新:改进了查找段标志的算法,及调试的代码。
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define READSIZE 10240
- //#define _debug
- FILE * fsave = NULL;
- void devide ( char *buffer); /* 该函数是自动分段的具体实现 */
- char* findFlag ( char* temp, char* temp2, char* present);
- /* 该函数用于寻找作为分段标志的四个连贯的空格 */
- char* findFlag ( char* temp, char* temp2, char* present)
- {
- if ( NULL != temp ) {
- temp2 = temp;
- while ( ' ' == *temp2 && *temp2 ){
- temp2++;
- }
- return strstr( temp2, " ");
- } else {
- return strstr( present + 1, " ");
- }
- }
- /* 该函数是自动分段的具体实现 */
- void devide ( char *buffer)
- {
- char *temp = NULL;
- char *present = buffer;
- char *temp2 = NULL;
- temp = strstr( present, " ");
- temp2 = findFlag( temp, temp2, present);
- while ( temp && temp2 && *present){
- #ifdef _debug
- puts("");
- getch();
- #else
- fputc( '/n', fsave);
- #endif
- for( ; present < temp2 && *present != '/0'; present++){
- #ifdef _debug
- putchar( *present);
- #else
- fputc(*present, fsave);
- #endif
- }
- temp = strstr( present, " ");
- // temp2 = strstr( present+4, " ");
- temp2 = findFlag( temp, temp2, present);
- }
- while (*present) {
- #ifdef _debug
- putchar( *present);
- #else
- fputc( *present, fsave);
- #endif
- present++;
- }
- }
- int main(int argc, char *argv[])
- {
- FILE * fload = NULL;
- char buffer[READSIZE] = "";
- char *isRead = NULL;
- printf("请将要转换的文件更名为test.txt,并与本程序存放在同一目录下。/n");
- system("PAUSE");
- clock_t start = clock();
- clock_t end ;
- fload = fopen( "test.txt", "r");
- if ( NULL == fload) {
- printf("找不到文件/n");
- system("PAUSE");
- return EXIT_FAILURE;
- }
- fsave = fopen( "save1.txt", "a");
- if ( NULL == fsave) {
- printf("无法建立存档文件/n");
- system("PAUSE");
- return EXIT_FAILURE;
- }
- while( NULL != fgets( buffer, READSIZE, fload) ){
- devide ( buffer);
- memset ( buffer, 0, sizeof(buffer));
- // isRead = fgets( buffer, READSIZE, fload);
- }
- fclose(fload);
- fclose(fsave);
- end = clock();
- printf("转换完成,共耗时%f秒/n", (double)( end - start) / CLK_TCK);
- system("PAUSE");
- return 0;
- }