BUAA文本编辑操作模拟(简)a 您所在的位置:网站首页 在powerpoint中对已做过的有限次编辑操作 BUAA文本编辑操作模拟(简)a

BUAA文本编辑操作模拟(简)a

2023-06-06 21:05| 来源: 网络整理| 查看: 265

【问题描述】

编写一程序模拟文本编辑操作。首先从标准输入读取一行字符串(字符个数不超过512),该行字符串是已经过n(大于0,小于等于10)步编辑操作后的结果。然后从下一行读取n,以及已发生过的n步编辑操作,编辑操作分行输入,输入格式为:

op pos str

其中op为编辑操作命令编码(在此只有插入和删除操作,1表示插入或2表示删除操作);pos表示插入或删除的位置;str表示已经插入或删除的字符串(中间没有空格)。各数据间以一个空格分隔。

然后在空一行后,再分行输入当前将要进行的编辑操作,包括如下四种操作(操作编码分别为:1表示插入,2表示删除操作,3表示撤销(即undo操作),-1表示结束):

1 pos str

表示将在pos位置插入字符串str(中间没有空格),各数据间以一个空格分隔;

2 pos n

表示将从pos位置开始删除n个字符(各数据间以一个空格分隔),若要删除的字符个数多于已有字符个数(即在文本中从pos开始的字符个数小于n),则按实际字符数删除即可。(提示:为了能够撤销删除操作,应按“2 pos str”形式保存命令。)

3

表示撤销最近执行的插入或删除操作,可以进行多次撤销操作,注意:也可以撤销之前已经发生过的n步编辑操作中的操作。

-1

表示退出编辑操作,在屏幕上输出最终编辑后的文本。

要求:

1、上述所有输入的编辑操作中的字符串str都不包含空白字符(空格符、制表符或换行符);

2、插入操作中的位置pos大于等于0,并且小于等于当前文本的字符个数;0位置表示文本第一个字符的位置;若pos为当前文本的字符个数,则表示在文本最后插入字符串;

3、删除操作中的位置pos大于等于0,并且小于当前文字的字符个数;

4、若已无操作可撤销,则再进行撤销操作无效;

5、文本在编辑过程中,总字符个数不会超过512。

【输入形式】

先从键盘输入一行字符串,表示已经经过n步编辑操作后的文本串,然后在下一行输入一个正整数n,并分行输入n步插入或删除操作(表示按时间先后顺序已进行的操作),格式如上所述。随后空一行,再分行输入将要进行的编辑操作,格式如上所述。直到输入-1操作为止。

【输出形式】

在屏幕上输出最终编辑后的文本内容。

【样例输入】

A Stack is a container of objects that are inserted and removed according to the last-in first-out (LIFO) principle.???

4

1 20 ainer

2 0 ???

1 85 -

1 99 (LIFO)

3

2 110 10

1 110 Objects

2 98 1

2 0 1

2 108 10

3

3

3

-1

【样例输出】

A Stack is a container of objects that are inserted and removed according to the last-in first-out principle.Objects

【样例说明】

第一行输入的文本串是先后经过下面4次编辑操作后得到的:先在20位置插入了字符串ainer,然后删除了开始位置的字符串???,随后在85位置插入了一个字符-,最后在99位置插入了字符串(LIFO)。

随后输入了撤销操作,即撤销先前最后进行的“1 99 (LIFO)”操作,也就是将99位置的6个字符删除;

2 110 10:将文本串最后的字符串???删除;

1 110 Objects:在文本串末尾插入字符串Objects;

随后执行了三次删除操作,又执行了三次撤销操作,最后输入的-1表示编辑操作结束,在屏幕上输出最终编辑后的文本串。

#include #include #define MAX 202 struct one { int type; int pos; int length; char a[514]; } procd[300]; struct opstack { struct one procd[300]; int top; }; int i, j, n, num, type1, pos1, line; char str1[514], str2[514], buffer[514]; typedef struct opstack stack; stack s; void initstack(stack *s) { s->top = -1; } void op(stack *s, int type, int pos2, int num, char str[], char buffer[]) { if (type == 1) { int k = strlen(buffer) - pos2 + 1; for (i = 0; i for (i = 0; i s->procd[++s->top].type = c; s->procd[s->top].pos = line; s->procd[s->top].length = length1; strcpy(s->procd[s->top].a, str); if (c == 1) { op(s, 1, line, s->procd[s->top].length, s->procd[s->top].a, buffer); } else if (c == 2) { op(s, 2, line, s->procd[s->top].length, s->procd[s->top].a, buffer); } } void pop_stack(stack *s, char buffer[]) { if (s->procd[s->top].type == 1) { op(s, 2, s->procd[s->top].pos, s->procd[s->top].length, "\0", buffer); } else if (s->procd[s->top].type == 2) { op(s, 1, s->procd[s->top].pos, strlen(s->procd[s->top].a), s->procd[s->top].a, buffer); } s->procd[s->top].type = 0; s->procd[s->top].pos = 0; strcpy(s->procd[s->top].a, "\0"); s->top--; } void push_stack1(stack *s, int c, int line, int length1, char str[]) { s->procd[++s->top].type = c; s->procd[s->top].pos = line; s->procd[s->top].length = length1; strcpy(s->procd[s->top].a, str); } int main(void) { initstack(&s); fgets(buffer, 513, stdin); buffer[strlen(buffer) - 1] = '\0'; scanf("%d", &n); for (i = 0; i if (s.top == -1) break; else pop_stack(&s, buffer); } else { scanf("%d %s", &pos1, str1); push_stack1(&s, type1, pos1, strlen(str1), str1); } } while (type1 != -1) { scanf("%d", &type1); if (type1 == 3) { if (s.top == -1) continue; else pop_stack(&s, buffer); } else if (type1 == 1) { scanf("%d %s", &pos1, str1); push_stack(&s, type1, pos1, strlen(str1), str1, buffer); } else if (type1 == 2) { scanf("%d %d", &pos1, &num); push_stack(&s, type1, pos1, num, "\0", buffer); } } printf("%s", buffer); }

方法二

#include #include #include #include #include #define clearstr memset(str,0,600); char stack[500][600],str[600],before[20][600]; int steps[20][2]; void sdelete (int pos,int n,char *str,char *dest) { int i,j; strncpy(dest,str,pos); j=pos+n; for(i=pos;str[i]!='\0';i++,j++) dest[i]=str[j]; } void add (int pos,char *ori,char *str,char *dest) { strncpy(dest,ori,pos); strcat(dest,str); int i,j; j=strlen(dest); for(i=pos;ori[i]!='\0';i++) dest[j++]=ori[i]; } int main() { gets(str); int n,i,op,pos,cnt; scanf("%d",&n); strcpy(stack[n],str); clearstr for(i=0;i=0;i--) { if(steps[i][0]==1) sdelete(steps[i][1],strlen(before[i]),stack[i+1],stack[i]); else add(steps[i][1],stack[i+1],before[i],stack[i]); } while(scanf("%d",&op)&&op!=-1) { if(op==1) { scanf("%d%s",&pos,str); add(pos,stack[n],str,stack[n+1]); clearstr n++; } else if(op==2) { scanf("%d%d",&pos,&cnt); sdelete(pos,cnt,stack[n],stack[n+1]); clearstr n++; } else { if(n==0) continue; memset(stack[n],0,600); n--; } } printf("%s",stack[n]); return 0; }

方法三

#include #include #include struct node{ int type; int pos; int l; char a[1000]; }; struct st{ struct node p[1000]; int t; }; typedef struct st stack; int i,j,n,num,type1,pos1,ln; char str1[1000],str2[1000],buffer[1000]; stack s; void init(stack *s) { s->t=-1; } void op(stack *s,int type,int pos2,int num,char str[],char buffer[]) { if(type==1) { int k=strlen(buffer)-pos2+1; for(i=0;i for(i=0;i if(buffer[i]!=1) { buffer[j++]=buffer[i]; } } buffer[j]='\0'; } } void push(stack *s,int c,int ln,int l1,char str[],char buffer[]) { s->p[++s->t].type=c; s->p[s->t].pos=ln; s->p[s->t].l=l1; strcpy(s->p[s->t].a,str); if(c==1) { op(s,1,ln,s->p[s->t].l,s->p[s->t].a,buffer); }else if(c==2) { op(s,2,ln,s->p[s->t].l,s->p[s->t].a,buffer); } } void pop(stack *s,char buffer[]) { if(s->p[s->t].type==1) { op(s,2,s->p[s->t].pos,s->p[s->t].l,"\0",buffer) ; }else if(s->p[s->t].type==2) { op(s,1,s->p[s->t].pos,strlen(s->p[s->t].a),s->p[s->t].a,buffer); } s->p[s->t].type=0; s->p[s->t].pos=0; strcpy(s->p[s->t].a,"\0"); s->t--; } void push1(stack *s, int c, int ln, int l1, char str[]) { s->p[++s->t].type = c; s->p[s->t].pos = ln; s->p[s->t].l = l1; strcpy(s->p[s->t].a, str); } int main() { init(&s); fgets(buffer,513,stdin); buffer[strlen(buffer)-1]='\0'; scanf("%d",&n); for(i=0;i if(s.t==-1)break; else pop(&s,buffer); }else { scanf("%d %s",&pos1,str1); push1(&s,type1,pos1,strlen(str1),str1); } } while(type1!=-1) { scanf("%d",&type1); if(type1==3) { if(s.t==-1)continue; else pop(&s,buffer); } else if(type1==1) { scanf("%d %s",&pos1,str1); push(&s,type1,pos1,strlen(str1),str1,buffer); }else if(type1==2) { scanf("%d %d",&pos1,&num); push(&s,type1,pos1,num,"\0",buffer); } } printf("%s",buffer); return 0; }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有