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

题目描述

在这里插入图片描述

在这里插入图片描述

【输出形式】

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

【样例输入】

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

题目分析

在这里插入图片描述
在这里插入图片描述

实现思路

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

实现代码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#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;
}
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

eeenkidu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值