数据结构-模拟文本编辑操作(栈)-C语言-【读取一行字符串该行字符串是已经过n步编辑操作后的结果。读取已执行操作,在继续编辑,直至结束获得字符串】

模拟文本编辑操作

  • 题目描述
  • 题目分析
  • 实现思路
  • 实现代码

题目描述

数据结构-模拟文本编辑操作(栈)-C语言-【读取一行字符串该行字符串是已经过n步编辑操作后的结果。读取已执行操作,在继续编辑,直至结束获得字符串】_第1张图片

数据结构-模拟文本编辑操作(栈)-C语言-【读取一行字符串该行字符串是已经过n步编辑操作后的结果。读取已执行操作,在继续编辑,直至结束获得字符串】_第2张图片

【输出形式】

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

【样例输入】

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表示编辑操作结束,在屏幕上输出最终编辑后的文本串。

题目分析

数据结构-模拟文本编辑操作(栈)-C语言-【读取一行字符串该行字符串是已经过n步编辑操作后的结果。读取已执行操作,在继续编辑,直至结束获得字符串】_第3张图片
数据结构-模拟文本编辑操作(栈)-C语言-【读取一行字符串该行字符串是已经过n步编辑操作后的结果。读取已执行操作,在继续编辑,直至结束获得字符串】_第4张图片

实现思路

  1. 初始化栈,获得字符、已执行操作数,栈顶top=0
  2. 循环将已执行操作依次入栈并top++,直至n=0 退出循环
  3. 循环执行操作,op=1插入入栈,top++;op=2 删除,top++;op=3 撤销,top–;直至op=-1 退出循环

实现代码

#include
#include
#include
#include
#define Max 520
#define MaxN 10

typedef struct edit {
     
	int op;//编辑操作命令编码:  1表示插入 2表示删除操作 3表示撤销
	int pos;//插入或删除的位置
	char str[Max + 10];//插入或删除的字符串(中间没有空格)
	int num;//插入或删除的字符串的长度
}edit;
edit stack[MaxN];//存放操作
int top;//栈顶
char s[Max + 10];//输入字符串
char str_temp[Max + 10];//临时字符串
//初始化栈
void init_stack() {
      top = -1; }
//判断栈顶是否为空
int is_empty()
{
     
	if (top == -1) return 1;
	else return 0;
}
//入栈
void push(edit temp) {
     
	stack[++top] = temp;
}
//出栈
edit pop() {
     
	edit res = stack[top--];
	return res;
}
//op=1 插入
void insert(int pos, char tmp[], int n) {
     
	char s_temp[Max + 10];
	strcpy(s_temp, s + pos);
	strcpy(s + pos, tmp);
	strcpy(s + pos + n, s_temp);
}
//op=2 删除
void delete(int pos, int n)
{
     
	char s_temp[Max + 10];
	int len = strlen(s);
	if (n + pos > len) n = len - pos;
	strcpy(s_temp, s + pos + n);
	strcpy(s + pos, s_temp);
	s[len - n] = '\0';
}
int main()
{
     
	init_stack();//初始化栈
	gets(s);//获得字符
	int n, op_t;//n:已执行操作数,
	edit temp;
	scanf("%d", &n);//n 步骤操作
	while (n--) {
      // 输出各操作
		scanf("%d %d %s", &temp.op, &temp.pos, temp.str);
		temp.num = strlen(temp.str);
		push(temp);
	}
	while (1) {
     
		scanf("%d", &op_t);
		if (op_t == -1) break;//退出
		if (op_t == 1) {
     //插入字符
			scanf("%d %s", &temp.pos, temp.str);
			temp.op = 1;
			temp.num= strlen(temp.str);
			push(temp);
			insert(temp.pos, temp.str, temp.num);
		}
		else if (op_t == 2) {
     //删除字符
			scanf("%d %d", &temp.pos, &temp.num);
			temp.op = 2;
			push(temp);
			int len = strlen(s);
			for (int i = stack[top].pos; i < len&&i < stack[top].pos + stack[top].num; i++) {
     
				stack[top].str[i - stack[top].pos] = s[i];
			}
			delete(temp.pos, temp.num);
		}
		else if (op_t == 3) {
     //撤销
			if (is_empty()) {
     
				continue;
			}
			temp = pop();
			if (temp.op == 1) {
     
				delete(temp.pos, temp.num);
			}
			else if (temp.op == 2) {
     
				insert(temp.pos, temp.str, temp.num);
			}
		}
	}
	printf("%s\n", s);
	return 0;
}

你可能感兴趣的:(数据结构)