今天上午對昨晚的原版本的改進。主要是改進了查找分段标志的算法,使分段邏輯顯得更合理。
Code:
/*
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++){
putchar( *present);
fputc(*present, fsave);
temp = strstr( present, " ");
// temp2 = strstr( present+4, " ");
temp2 = findFlag( temp, temp2, present);
while (*present) {
#ifdef _debug
putchar( *present);
fputc( *present, fsave);
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");
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);
return 0;