1

我正在为即将到来的考试学习 C,我正在为此苦苦挣扎。

工作是使用以下规则编辑文本文件:

  • , 之前没有空格。!: ; ?
  • , 之后只有一个空格。!: ; ?
  • 之后的第一个字母。!? 缪斯是大写的

主文件

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

int main()
{
int i=0,size;
char ch,source_file[100],*buffer;
FILE *source, *target;

printf("Enter name of file to copy\n");
gets(source_file);

source = fopen(source_file, "r");

if(!source)
{
    printf("File does not exist !");
    exit(EXIT_FAILURE);
}

target = fopen("test1.txt", "w");

/*
    Edit for rule number 2 and 3
 */
while( ( ch = fgetc(source) ) != EOF )  {
    if(i!=0)    {
        if(ch == ' ')   {
            if(i== 1)   {
                    fputc(ch,target);
                    i=2;
            }
            else if(i==2)   {
                    continue;
            }
        }
        else    {
            if(isalpha(ch)) {
                ch= toupper(ch);    
            }
            fputc(ch,target);
            i=0;
        }
    }
    else    {
        fputc(ch,target);
        if(ch==',' || ch=='.' || ch=='!' || ch==':' || ch==';' || ch=='?'){
            i=1;
        }
        else    {
            i=0;
        }
    }
}

 /**
 * Reverse and put text content into an array.
 * Then read array bottom up,put in source file
 * for requiment number 1 :
 */
fseek(target, 0, SEEK_END);
size = ftell(target);

buffer = (char *) malloc (size+1);
target= freopen("test1.txt","r",target);
for (i=(size-1); i>=0; i--)
{
    buffer[i] = fgetc(target);
}
buffer[size] = 0;

source =freopen(source_file,"w",source);
int state=0;
for(int k=(size-1);k>=0;k--)    {
    if(buffer[k]== EOF) {
        continue;
    }
    if(state== 1)   {
        if(buffer[k]== ' ') {
            continue;   
        }   
        else    {
            fputc(buffer[k],source);
            state= 0;
        }       
    }
    else    {
        if(buffer[k]== ',' || buffer[k]== '.' || buffer[k]== '!' || buffer[k]== ':' || buffer[k]== ';' || buffer[k]== '?')  {
            state= 1;   
        }
        fputc(buffer[k],source);
    }
}
printf("File copied successfully.\n");

  /**
   * Close all file
   */
fclose(source);
fclose(target);
if(remove("test1.txt")== 0) {
    printf("\nDelete successfully");
}
else {
    printf("\n Error");
}
return 0;
    }

我确实解决了规则 2 和 3。

但是使用第一条规则,结果与我的代码不同。在转换为规则 2 和 3 后,我反转内容,然后对其进行编辑,然后再次将其反转并放入 source_file。

我设置了一个状态,1 表示前一个字符是 , 。!: ; ?,0 是普通字符 结果是 , 之后的所有空格。!: ; ? 是删除,但在 , 之前有空格。!: ; ? 仍然存在。

我的代码哪里出了问题,请帮帮我,对不起,长篇大论:(

编辑(工作):

 /* Rules:
  ** NO space before punctuation.
  ** only ONE space after punctuation
  ** after a .?! the next alpha character should be Capitalised
  */

  #include <stdio.h>
  #include <string.h>
  #include <ctype.h>
  #include <stdlib.h>

  #define STATE_INITIAL 0
  #define STATE_WORD 1
  #define STATE_KOMMA 2
  #define STATE_PERIOD 3
  #define STATE_EOF -1

  int main(void)
  {
unsigned spacecount;
int i=0,state;
char ch,source_file[100];
FILE *source, *target;

    spacecount= 0;

printf("Enter name of file to copy\n");
gets(source_file);

source = fopen(source_file, "r");

if(!source)
{
    printf("File does not exist !");
    exit(EXIT_FAILURE);
}

target = fopen("test1.txt", "w");

for (state=STATE_INITIAL; state != STATE_EOF; ) {
    ch = fgetc(source);
    switch(ch) {
    case ':' : case ',' : case ';' :
            spacecount=0;
            state = STATE_KOMMA;
            break;
    case '!' : case '.' : case '?' :
            spacecount=0;
            state = STATE_PERIOD;
            break;
    case ' ': spacecount++; continue;
    case EOF: state = STATE_EOF; continue;
    default:
            if (state == STATE_KOMMA || state == STATE_PERIOD) spacecount = 1;
            for( ;spacecount > 0; spacecount--) { fputc( ' ',target); }
            if (state == 3 && isalpha(ch))  ch = toupper(ch);
            state = STATE_WORD;
            break;
            }
    fputc(ch,target);
    }

  return 0;
  }
4

1 回答 1

3

极简状态机。请注意,不需要缓冲以前的字符,只需记住它们(通过stateand spacecount

/* Rules:
** NO space before punctuation.
** only ONE space after punctuation
** after a .?! the next alpha character should be Capitalised
*/

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define STATE_INITIAL 0
#define STATE_WORD 1
#define STATE_KOMMA 2
#define STATE_PERIOD 3
#define STATE_EOF -1

int main(void)
{
unsigned spacecount;
int state,ch;

spacecount=0;
for (state=STATE_INITIAL; state > STATE_EOF; ) {
        ch = getc(stdin);
        switch(ch) {
        case ':' : case ',' : case ';' :
                spacecount=0;
                state = STATE_KOMMA;
                break;
        case '!' : case '.' : case '?' :
                spacecount=0;
                state = STATE_PERIOD;
                break;
        case ' ': spacecount++; continue;
        case EOF: state = STATE_EOF; continue;
        default:
                if (state == STATE_KOMMA || state == STATE_PERIOD) spacecount = 1;
                for( ;spacecount > 0; spacecount--) { putc( ' ', stdout); }
                if (state == 3 && isalpha(ch))  ch = toupper(ch);
                state = STATE_WORD;
                break;
                }
        putc(ch, stdout);
        }

return 0;
}
于 2013-04-03T18:52:03.737 回答